2022-05-22 10:59:53 +00:00
|
|
|
import { Fragment } from "react";
|
2022-05-22 11:06:43 +00:00
|
|
|
import type { MetaFunction } from "@remix-run/node";
|
2022-05-14 10:22:06 +00:00
|
|
|
import { useNavigate } from "@remix-run/react";
|
2022-05-22 11:06:43 +00:00
|
|
|
import { useLoaderData } from "superjson-remix";
|
2021-08-29 23:53:22 +00:00
|
|
|
import { Transition } from "@headlessui/react";
|
2021-09-07 20:59:38 +00:00
|
|
|
import { IoBackspace, IoCall } from "react-icons/io5";
|
2022-05-14 10:22:06 +00:00
|
|
|
|
2022-05-22 11:06:43 +00:00
|
|
|
import keypadLoader, { type KeypadLoaderData } from "~/features/keypad/loaders/keypad";
|
2022-05-14 10:22:06 +00:00
|
|
|
import useKeyPress from "~/features/keypad/hooks/use-key-press";
|
2022-05-22 10:59:53 +00:00
|
|
|
import useOnBackspacePress from "~/features/keypad/hooks/use-on-backspace-press";
|
|
|
|
import Keypad from "~/features/keypad/components/keypad";
|
|
|
|
import BlurredKeypad from "~/features/keypad/components/blurred-keypad";
|
|
|
|
import MissingTwilioCredentials from "~/features/core/components/missing-twilio-credentials";
|
2022-05-14 10:22:06 +00:00
|
|
|
import InactiveSubscription from "~/features/core/components/inactive-subscription";
|
2022-05-22 10:59:53 +00:00
|
|
|
import { getSeoMeta } from "~/utils/seo";
|
|
|
|
import { usePhoneNumber, usePressDigit, useRemoveDigit } from "~/features/keypad/hooks/atoms";
|
|
|
|
|
|
|
|
export const meta: MetaFunction = () => ({
|
|
|
|
...getSeoMeta({ title: "Keypad" }),
|
|
|
|
});
|
|
|
|
|
2022-05-22 11:06:43 +00:00
|
|
|
export const loader = keypadLoader;
|
2022-05-14 10:22:06 +00:00
|
|
|
|
2022-05-21 19:33:23 +00:00
|
|
|
export default function KeypadPage() {
|
2022-05-22 10:59:53 +00:00
|
|
|
const { hasOngoingSubscription, hasPhoneNumber, lastRecipientCalled } = useLoaderData<KeypadLoaderData>();
|
2022-05-14 10:22:06 +00:00
|
|
|
const navigate = useNavigate();
|
2022-05-22 10:59:53 +00:00
|
|
|
const [phoneNumber, setPhoneNumber] = usePhoneNumber();
|
|
|
|
const removeDigit = useRemoveDigit();
|
|
|
|
const pressDigit = usePressDigit();
|
|
|
|
const onBackspacePress = useOnBackspacePress();
|
2021-08-31 22:42:39 +00:00
|
|
|
useKeyPress((key) => {
|
2021-10-24 23:00:59 +00:00
|
|
|
if (!hasOngoingSubscription) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-31 22:42:39 +00:00
|
|
|
if (key === "Backspace") {
|
|
|
|
return removeDigit();
|
|
|
|
}
|
|
|
|
|
|
|
|
pressDigit(key);
|
|
|
|
});
|
2021-08-29 23:53:22 +00:00
|
|
|
|
2022-05-22 10:59:53 +00:00
|
|
|
if (!hasPhoneNumber) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<MissingTwilioCredentials />
|
|
|
|
<BlurredKeypad />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2021-07-31 17:22:48 +00:00
|
|
|
|
2021-10-24 23:00:59 +00:00
|
|
|
if (!hasOngoingSubscription) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<InactiveSubscription />
|
2022-05-22 10:59:53 +00:00
|
|
|
<BlurredKeypad />
|
2021-10-24 23:00:59 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-31 17:22:48 +00:00
|
|
|
return (
|
2021-10-15 22:24:28 +00:00
|
|
|
<>
|
|
|
|
<div className="w-96 h-full flex flex-col justify-around py-5 mx-auto text-center text-black">
|
|
|
|
<div className="h-16 text-3xl text-gray-700">
|
|
|
|
<span>{phoneNumber}</span>
|
|
|
|
</div>
|
|
|
|
|
2022-05-22 10:59:53 +00:00
|
|
|
<Keypad>
|
2021-10-15 22:24:28 +00:00
|
|
|
<button
|
|
|
|
onClick={async () => {
|
2022-05-22 10:59:53 +00:00
|
|
|
if (!hasPhoneNumber || !hasOngoingSubscription) {
|
2021-10-20 22:01:03 +00:00
|
|
|
return;
|
2021-10-15 22:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (phoneNumber === "") {
|
2022-05-22 10:59:53 +00:00
|
|
|
if (lastRecipientCalled) {
|
|
|
|
setPhoneNumber(lastRecipientCalled);
|
2021-10-15 22:24:28 +00:00
|
|
|
}
|
2021-07-31 17:22:48 +00:00
|
|
|
|
2021-10-15 22:24:28 +00:00
|
|
|
return;
|
2021-08-13 11:55:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 10:22:06 +00:00
|
|
|
await navigate(`/outgoing-call/${encodeURI(phoneNumber)}`);
|
2021-10-15 22:24:28 +00:00
|
|
|
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"
|
|
|
|
>
|
|
|
|
<IoCall className="w-6 h-6 text-white" />
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<Transition
|
|
|
|
as={Fragment}
|
|
|
|
show={phoneNumber.length > 0}
|
|
|
|
enter="transition duration-300 ease-in-out"
|
|
|
|
enterFrom="transform scale-95 opacity-0"
|
|
|
|
enterTo="transform scale-100 opacity-100"
|
|
|
|
leave="transition duration-100 ease-out"
|
|
|
|
leaveFrom="transform scale-100 opacity-100"
|
|
|
|
leaveTo="transform scale-95 opacity-0"
|
|
|
|
>
|
|
|
|
<div {...onBackspacePress} className="cursor-pointer select-none m-auto">
|
|
|
|
<IoBackspace className="w-6 h-6" />
|
|
|
|
</div>
|
|
|
|
</Transition>
|
|
|
|
</Keypad>
|
|
|
|
</div>
|
|
|
|
</>
|
2021-07-31 17:22:48 +00:00
|
|
|
);
|
2022-05-14 10:22:06 +00:00
|
|
|
}
|