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

94 lines
3.2 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();
console.log("twilio", twilio);
2022-05-14 22:35:51 +00:00
const [isHelpModalOpen, setIsHelpModalOpen] = useState(false);
const transition = useTransition();
const actionData = useActionData<any>()
?.setTwilioCredentials as SetTwilioCredentialsActionData["setTwilioCredentials"];
const { accountSid, authToken } = useLoaderData<PhoneSettingsLoaderData>();
const topErrorMessage = actionData?.errors?.general;
const isError = typeof topErrorMessage !== "undefined";
2022-06-11 00:09:37 +00:00
const isCurrentFormTransition = transition.submission?.formData.get("_action") === "setTwilioCredentials";
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">
2022-06-26 11:52:36 +00:00
<p>
To connect your Twilio Account you will need to provide us your Twilio account SID and auth
token.
</p>
<p>
Both values can be found on your{" "}
<a href="https://www.twilio.com/user/account/settings">Twilio account page</a>.
</p>
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} />
</>
);
}