How to check if user has picked up call in twilio in react?

41 Views Asked by At

I have a react app and want to add a timer in call menu. When receiver picks up the call i want that timer to start.
Here is some of my code

  useEffect(() => {
    const device = new Device();
    device.setup(token, { debug: true });
    device.on("ready", () => {
      setDevice(device);
      setReady(true);
    });
    device.on("connect", (connection) => {
      setConn(connection);
          });
    device.on("accept", () => {
      console.log("Call is answered");
      // This function doesn't work here
    });
    device.on("disconnect", () => {
      setConn(null);
      navigate("/finish");
    });
    device.on("incoming", (connection) => {
      setConn(connection);
      connection.on("reject", () => {
        setConn(null);
      });
    });
    device.on("cancel", () => {
      setConn(null);
    });
    device.on("reject", () => {
      setConn(null);
    });

    return () => {
      device.destroy();
      setDevice(null);
    };
  }, [token]);

  useEffect(() => {
    if (ready) {
      handleCall();
    }
  }, [ready]);

  const handleCall = () => {
    device.connect({ To: number });
    setIsActive(true);
  };

Is there a way to know when a call is picked up by the receiver so I can start a timer.

0

There are 0 best solutions below