From 800ff5ab99674fc5f6b3a1f95bd03b93f45841f1 Mon Sep 17 00:00:00 2001 From: m5r Date: Fri, 13 Aug 2021 23:52:38 +0800 Subject: [PATCH] cleanup call making --- app/phone-calls/hooks/use-make-call.ts | 20 +++++----- .../pages/outgoing-call/[recipient].tsx | 40 +++++++++---------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/app/phone-calls/hooks/use-make-call.ts b/app/phone-calls/hooks/use-make-call.ts index 99ec76b..317382a 100644 --- a/app/phone-calls/hooks/use-make-call.ts +++ b/app/phone-calls/hooks/use-make-call.ts @@ -4,7 +4,12 @@ import { Call, Device, TwilioError } from "@twilio/voice-sdk"; import getToken from "../mutations/get-token"; -export default function useMakeCall(recipient: string) { +type Params = { + recipient: string; + onHangUp?: () => void; +}; + +export default function useMakeCall({ recipient, onHangUp }: Params) { const [outgoingConnection, setOutgoingConnection] = useState(null); const [device, setDevice] = useState(null); const [getTokenMutation] = useMutation(getToken); @@ -80,15 +85,10 @@ export default function useMakeCall(recipient: string) { function hangUp() { setState("call_ending"); - - if (outgoingConnection) { - outgoingConnection.reject(); - } - - if (device) { - device.disconnectAll(); - device.destroy(); - } + outgoingConnection?.reject(); + device?.disconnectAll(); + device?.destroy(); + onHangUp?.(); } function onDeviceError(error: TwilioError.TwilioError, call?: Call) { diff --git a/app/phone-calls/pages/outgoing-call/[recipient].tsx b/app/phone-calls/pages/outgoing-call/[recipient].tsx index 46bb20d..004d176 100644 --- a/app/phone-calls/pages/outgoing-call/[recipient].tsx +++ b/app/phone-calls/pages/outgoing-call/[recipient].tsx @@ -10,19 +10,20 @@ import Keypad from "../../components/keypad"; import useMakeCall from "../../hooks/use-make-call"; const OutgoingCall: BlitzPage = () => { - const router = useRouter(); - const recipient = decodeURIComponent(router.params.recipient); - const call = useMakeCall(recipient); - - useEffect(() => { - console.log("call.state", call.state); - if (call.state === "ready") { - call.makeCall(); - } - }, [call.state]); - useRequireOnboarding(); const [phoneNumber, setPhoneNumber] = useAtom(phoneNumberAtom); + const router = useRouter(); + const recipient = decodeURIComponent(router.params.recipient); + const onHangUp = useMemo( + () => () => { + setPhoneNumber(""); + + // return router.replace(Routes.KeypadPage()); + return router.push(Routes.KeypadPage()); + }, + [router, setPhoneNumber], + ); + const call = useMakeCall({ recipient, onHangUp }); const pressDigit = useAtom(pressDigitAtom)[1]; const onDigitPressProps = useMemo( () => (digit: string) => ({ @@ -34,16 +35,13 @@ const OutgoingCall: BlitzPage = () => { }), [call, pressDigit], ); - const hangUp = useMemo( - () => () => { - call.hangUp(); - setPhoneNumber(""); - // return router.replace(Routes.KeypadPage()); - return router.push(Routes.KeypadPage()); - }, - [call, router, setPhoneNumber], - ); + useEffect(() => { + console.log("call.state", call.state); + if (call.state === "ready") { + call.makeCall(); + } + }, [call.state]); return (
@@ -58,7 +56,7 @@ const OutgoingCall: BlitzPage = () => {