shellphone.app/app/features/settings/components/phone/twilio-connect.tsx

85 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-05-14 22:35:51 +00:00
import { useState } from "react";
import { Form, useActionData, useLoaderData, useTransition } from "@remix-run/react";
2022-05-14 22:35:51 +00:00
import { IoHelpCircle } from "react-icons/io5";
import type { PhoneSettingsLoaderData } from "~/features/settings/loaders/phone";
import type { SetTwilioCredentialsActionData } from "~/features/settings/actions/phone";
2022-05-14 22:35:51 +00:00
import HelpModal from "./help-modal";
import SettingsSection from "../settings-section";
import useSession from "~/features/core/hooks/use-session";
import Alert from "~/features/core/components/alert";
import LabeledTextField from "~/features/core/components/labeled-text-field";
import Button from "~/features/settings/components/button";
2022-05-14 22:35:51 +00:00
export default function TwilioConnect() {
const { twilio } = useSession();
2022-05-14 22:35:51 +00:00
const [isHelpModalOpen, setIsHelpModalOpen] = useState(false);
const transition = useTransition();
const actionData = useActionData<SetTwilioCredentialsActionData>()?.setTwilioCredentials;
const { accountSid, authToken } = useLoaderData<PhoneSettingsLoaderData>();
const topErrorMessage = actionData?.errors?.general;
const isError = typeof topErrorMessage !== "undefined";
const isCurrentFormTransition = transition.submission?.formData.get("_action") === "changePassword";
const isSubmitting = isCurrentFormTransition && transition.state === "submitting";
2022-05-14 22:35:51 +00:00
return (
<>
<Form method="post">
<SettingsSection
className="flex flex-col relative"
footer={
<div className="px-4 py-3 bg-gray-50 text-right text-sm font-medium sm:px-6">
<Button tabIndex={3} variant="default" type="submit" isDisabled={isSubmitting}>
Save
</Button>
</div>
}
>
2022-05-14 22:35:51 +00:00
<button onClick={() => setIsHelpModalOpen(true)} className="absolute top-2 right-2">
<IoHelpCircle className="w-6 h-6 text-primary-700" />
</button>
<article className="mb-6">
Shellphone needs some informations about your Twilio account to securely use your phone numbers.
2022-05-14 22:35:51 +00:00
</article>
{twilio !== null ? (
2022-05-14 22:35:51 +00:00
<p className="text-green-700"> Your Twilio account is connected to Shellphone.</p>
) : null}
{isError ? (
<div className="mb-8">
<Alert title="Oops, there was an issue" message={topErrorMessage} variant="error" />
</div>
) : null}
<LabeledTextField
name="twilioAccountSid"
label="Account SID"
type="text"
tabIndex={1}
error={actionData?.errors?.twilioAccountSid}
disabled={isSubmitting}
defaultValue={accountSid}
/>
<LabeledTextField
name="twilioAuthToken"
label="Auth Token"
type="password"
tabIndex={2}
error={actionData?.errors?.twilioAuthToken}
disabled={isSubmitting}
autoComplete="off"
defaultValue={authToken}
/>
<input type="hidden" name="_action" value="setTwilioCredentials" />
</SettingsSection>
</Form>
2022-05-14 22:35:51 +00:00
<HelpModal closeModal={() => setIsHelpModalOpen(false)} isHelpModalOpen={isHelpModalOpen} />
</>
);
}