2021-08-08 06:37:53 +00:00
|
|
|
import type { BlitzPage } from "blitz";
|
2021-08-13 04:43:57 +00:00
|
|
|
import { Routes, useRouter } from "blitz";
|
|
|
|
import { useEffect, useMemo } from "react";
|
2021-08-08 06:37:53 +00:00
|
|
|
import { atom, useAtom } from "jotai";
|
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
|
|
import { faPhoneAlt as faPhone } from "@fortawesome/pro-solid-svg-icons";
|
|
|
|
|
|
|
|
import useRequireOnboarding from "../../../core/hooks/use-require-onboarding";
|
2021-08-08 11:46:32 +00:00
|
|
|
import Keypad from "../../components/keypad";
|
2021-08-13 04:43:57 +00:00
|
|
|
import useMakeCall from "../../hooks/use-make-call";
|
2021-08-08 06:37:53 +00:00
|
|
|
|
|
|
|
const OutgoingCall: BlitzPage = () => {
|
|
|
|
const router = useRouter();
|
|
|
|
const recipient = decodeURIComponent(router.params.recipient);
|
2021-08-13 04:43:57 +00:00
|
|
|
const call = useMakeCall(recipient);
|
2021-08-08 06:37:53 +00:00
|
|
|
|
2021-08-08 11:46:32 +00:00
|
|
|
useEffect(() => {
|
2021-08-13 04:43:57 +00:00
|
|
|
console.log("call.state", call.state);
|
|
|
|
if (call.state === "ready") {
|
|
|
|
call.makeCall();
|
2021-08-08 06:37:53 +00:00
|
|
|
}
|
2021-08-13 04:43:57 +00:00
|
|
|
}, [call.state]);
|
2021-08-08 06:37:53 +00:00
|
|
|
|
|
|
|
useRequireOnboarding();
|
|
|
|
const phoneNumber = useAtom(phoneNumberAtom)[0];
|
2021-08-08 11:46:32 +00:00
|
|
|
const pressDigit = useAtom(pressDigitAtom)[1];
|
|
|
|
const onDigitPressProps = useMemo(
|
|
|
|
() => (digit: string) => ({
|
|
|
|
onPress() {
|
|
|
|
pressDigit(digit);
|
|
|
|
|
2021-08-13 04:43:57 +00:00
|
|
|
call.sendDigits(digit);
|
2021-08-08 11:46:32 +00:00
|
|
|
},
|
|
|
|
}),
|
2021-08-13 04:43:57 +00:00
|
|
|
[call, pressDigit],
|
2021-08-08 06:37:53 +00:00
|
|
|
);
|
2021-08-08 11:46:32 +00:00
|
|
|
const hangUp = useMemo(
|
|
|
|
() => () => {
|
2021-08-13 04:43:57 +00:00
|
|
|
call.hangUp();
|
2021-08-08 06:37:53 +00:00
|
|
|
|
2021-08-08 11:46:32 +00:00
|
|
|
// return router.replace(Routes.KeypadPage());
|
|
|
|
return router.push(Routes.KeypadPage());
|
|
|
|
},
|
2021-08-13 04:43:57 +00:00
|
|
|
[call, router],
|
2021-08-08 06:37:53 +00:00
|
|
|
);
|
|
|
|
|
2021-08-08 11:46:32 +00:00
|
|
|
return (
|
|
|
|
<div className="w-96 h-full flex flex-col justify-around py-5 mx-auto text-center text-black bg-white">
|
|
|
|
<div className="h-16 text-3xl text-gray-700">
|
|
|
|
<span>{recipient}</span>
|
|
|
|
</div>
|
2021-08-08 06:37:53 +00:00
|
|
|
|
2021-08-13 04:43:57 +00:00
|
|
|
<div className="mb-4 text-gray-600">
|
|
|
|
<div className="h-8 text-2xl">{phoneNumber}</div>
|
|
|
|
<div className="h-8 text-lg">{translateState(call.state)}</div>
|
2021-08-08 11:46:32 +00:00
|
|
|
</div>
|
2021-08-08 06:37:53 +00:00
|
|
|
|
2021-08-08 11:46:32 +00:00
|
|
|
<Keypad onDigitPressProps={onDigitPressProps} onZeroPressProps={onDigitPressProps("0")}>
|
|
|
|
<div
|
|
|
|
onClick={hangUp}
|
|
|
|
className="cursor-pointer select-none col-start-2 h-12 w-12 flex justify-center items-center mx-auto bg-red-800 rounded-full"
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon className="w-6 h-6" icon={faPhone} color="white" size="lg" />
|
|
|
|
</div>
|
|
|
|
</Keypad>
|
2021-08-08 06:37:53 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2021-08-13 04:43:57 +00:00
|
|
|
function translateState(state: typeof call.state): string {
|
|
|
|
switch (state) {
|
|
|
|
case "initial":
|
|
|
|
case "ready":
|
|
|
|
return "Connecting...";
|
|
|
|
case "calling":
|
|
|
|
return "Calling...";
|
|
|
|
case "call_in_progress":
|
|
|
|
return ""; // TODO display time elapsed
|
|
|
|
case "call_ending":
|
|
|
|
return "Call ending...";
|
|
|
|
case "call_ended":
|
|
|
|
return "Call ended";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-08-08 06:37:53 +00:00
|
|
|
|
|
|
|
const phoneNumberAtom = atom("");
|
|
|
|
const pressDigitAtom = atom(null, (get, set, digit: string) => {
|
|
|
|
if (get(phoneNumberAtom).length > 17) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
set(phoneNumberAtom, (prevState) => prevState + digit);
|
|
|
|
});
|
|
|
|
|
|
|
|
OutgoingCall.authenticate = { redirectTo: Routes.SignIn() };
|
|
|
|
|
|
|
|
export default OutgoingCall;
|