shellphone.app/app/phone-calls/pages/keypad.tsx

89 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-07-31 17:22:48 +00:00
import type { BlitzPage } from "blitz";
import { Link, Routes } from "blitz";
2021-08-01 04:29:41 +00:00
import { useRef } from "react";
2021-07-31 17:22:48 +00:00
import { atom, useAtom } from "jotai";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faBackspace, faPhoneAlt as faPhone } from "@fortawesome/pro-solid-svg-icons";
import Layout from "../../core/layouts/layout";
2021-08-08 11:22:34 +00:00
import Keypad from "../components/keypad";
2021-07-31 17:22:48 +00:00
import useRequireOnboarding from "../../core/hooks/use-require-onboarding";
2021-08-08 11:22:34 +00:00
const KeypadPage: BlitzPage = () => {
2021-07-31 17:22:48 +00:00
useRequireOnboarding();
const [phoneNumber, setPhoneNumber] = useAtom(phoneNumberAtom);
2021-07-31 17:22:48 +00:00
const pressBackspace = useAtom(pressBackspaceAtom)[1];
2021-08-01 04:29:41 +00:00
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
2021-07-31 17:22:48 +00:00
const pressDigit = useAtom(pressDigitAtom)[1];
2021-08-01 04:29:41 +00:00
const longPressDigit = useAtom(longPressDigitAtom)[1];
2021-08-08 11:22:34 +00:00
const onZeroPressProps = {
2021-08-01 04:29:41 +00:00
onPressStart() {
pressDigit("0");
timeoutRef.current = setTimeout(() => {
longPressDigit("+");
}, 750);
},
onPressEnd() {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
},
2021-08-08 11:22:34 +00:00
};
const onDigitPressProps = (digit: string) => ({
onPress() {
pressDigit(digit);
},
2021-08-01 04:29:41 +00:00
});
2021-07-31 17:22:48 +00:00
return (
2021-08-08 11:22:34 +00:00
<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>{phoneNumber}</span>
</div>
2021-07-31 17:22:48 +00:00
2021-08-08 11:22:34 +00:00
<Keypad onDigitPressProps={onDigitPressProps} onZeroPressProps={onZeroPressProps}>
<Link href={Routes.OutgoingCall({ recipient: encodeURI(phoneNumber) })}>
<a
onClick={() => setPhoneNumber("")}
className="cursor-pointer select-none col-start-2 h-12 w-12 flex justify-center items-center mx-auto bg-green-800 rounded-full"
>
2021-08-08 11:22:34 +00:00
<FontAwesomeIcon className="w-6 h-6" icon={faPhone} color="white" size="lg" />
</a>
</Link>
<div className="cursor-pointer select-none m-auto" onClick={pressBackspace}>
<FontAwesomeIcon className="w-6 h-6" icon={faBackspace} size="lg" />
</div>
</Keypad>
2021-07-31 17:22:48 +00:00
</div>
);
};
const phoneNumberAtom = atom("");
2021-08-01 04:29:41 +00:00
const pressDigitAtom = atom(null, (get, set, digit: string) => {
2021-07-31 17:22:48 +00:00
if (get(phoneNumberAtom).length > 17) {
return;
}
set(phoneNumberAtom, (prevState) => prevState + digit);
});
2021-08-01 04:29:41 +00:00
const longPressDigitAtom = atom(null, (get, set, replaceWith: string) => {
if (get(phoneNumberAtom).length > 17) {
return;
}
set(phoneNumberAtom, (prevState) => prevState.slice(0, -1) + replaceWith);
});
2021-07-31 17:22:48 +00:00
const pressBackspaceAtom = atom(null, (get, set) => {
if (get(phoneNumberAtom).length === 0) {
return;
}
set(phoneNumberAtom, (prevState) => prevState.slice(0, -1));
});
2021-08-08 11:22:34 +00:00
KeypadPage.getLayout = (page) => <Layout title="Keypad">{page}</Layout>;
2021-08-01 03:05:40 +00:00
2021-08-08 11:22:34 +00:00
KeypadPage.authenticate = { redirectTo: Routes.SignIn() };
2021-08-01 03:05:40 +00:00
2021-08-08 11:22:34 +00:00
export default KeypadPage;