|
| 1 | +import { useRouter } from "next/navigation"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { useForm } from "react-hook-form"; |
| 4 | +import { Toaster } from "sonner"; |
| 5 | + |
| 6 | +import { useLocale } from "@calcom/lib/hooks/useLocale"; |
| 7 | +import { Alert } from "@calcom/ui/components/alert"; |
| 8 | +import { Button } from "@calcom/ui/components/button"; |
| 9 | +import { Form, TextField } from "@calcom/ui/components/form"; |
| 10 | +import { PlusIcon, TrashIcon } from "@coss/ui/icons"; |
| 11 | + |
| 12 | +export default function ProtonCalendarSetup() { |
| 13 | + const { t } = useLocale(); |
| 14 | + const router = useRouter(); |
| 15 | + const form = useForm({ |
| 16 | + defaultValues: {}, |
| 17 | + }); |
| 18 | + |
| 19 | + const [urls, setUrls] = useState<string[]>([""]); |
| 20 | + const [errorMessage, setErrorMessage] = useState(""); |
| 21 | + |
| 22 | + return ( |
| 23 | + <div className="bg-emphasis flex h-screen"> |
| 24 | + <div className="bg-default m-auto rounded p-5 md:w-[560px] md:p-10"> |
| 25 | + <div className="flex flex-col stack-y-5 md:flex-row md:space-x-5 md:stack-y-0"> |
| 26 | + <div> |
| 27 | + {/* eslint-disable @next/next/no-img-element */} |
| 28 | + <img |
| 29 | + src="/api/app-store/protoncalendar/icon.svg" |
| 30 | + alt="Proton Calendar" |
| 31 | + className="h-12 w-12 max-w-2xl" |
| 32 | + /> |
| 33 | + </div> |
| 34 | + <div className="flex w-10/12 flex-col"> |
| 35 | + <h1 className="text-default">{t("connect_proton_calendar")}</h1> |
| 36 | + <div className="mt-1 text-sm">{t("credentials_stored_encrypted")}</div> |
| 37 | + <div className="my-2 mt-3"> |
| 38 | + <Form |
| 39 | + form={form} |
| 40 | + handleSubmit={async (_) => { |
| 41 | + setErrorMessage(""); |
| 42 | + |
| 43 | + try { |
| 44 | + const res = await fetch("/api/integrations/protoncalendar/add", { |
| 45 | + method: "POST", |
| 46 | + body: JSON.stringify({ urls }), |
| 47 | + headers: { |
| 48 | + "Content-Type": "application/json", |
| 49 | + }, |
| 50 | + }); |
| 51 | + const json = await res.json().catch(() => ({})); |
| 52 | + |
| 53 | + if (!res.ok) { |
| 54 | + setErrorMessage(json?.message || t("something_went_wrong")); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + router.push(json.url); |
| 59 | + } catch { |
| 60 | + setErrorMessage(t("something_went_wrong")); |
| 61 | + } |
| 62 | + }}> |
| 63 | + <fieldset className="stack-y-2" disabled={form.formState.isSubmitting}> |
| 64 | + {urls.map((url, i) => ( |
| 65 | + <div key={i} className="flex w-full items-center gap-2"> |
| 66 | + <TextField |
| 67 | + required |
| 68 | + type="text" |
| 69 | + label={t("calendar_url")} |
| 70 | + value={url} |
| 71 | + containerClassName={`w-full ${i === 0 ? "mr-6" : ""}`} |
| 72 | + onChange={(e) => { |
| 73 | + const newVal = e.target.value as string; |
| 74 | + setUrls((urls) => urls.map((x, ii) => (ii === i ? newVal : x))); |
| 75 | + }} |
| 76 | + placeholder="webcal://calendar.proton.me/api/calendar/v1/url/..." |
| 77 | + /> |
| 78 | + {i !== 0 ? ( |
| 79 | + <button |
| 80 | + type="button" |
| 81 | + aria-label={t("remove")} |
| 82 | + className="mb-2 h-min text-sm" |
| 83 | + onClick={() => setUrls((urls) => urls.filter((_, ii) => i !== ii))}> |
| 84 | + <TrashIcon size={16} /> |
| 85 | + </button> |
| 86 | + ) : null} |
| 87 | + </div> |
| 88 | + ))} |
| 89 | + </fieldset> |
| 90 | + |
| 91 | + <button |
| 92 | + className="text-sm" |
| 93 | + type="button" |
| 94 | + onClick={() => { |
| 95 | + setUrls((urls) => urls.concat("")); |
| 96 | + }}> |
| 97 | + {t("add")} <PlusIcon className="inline" size={16} /> |
| 98 | + </button> |
| 99 | + |
| 100 | + {errorMessage && <Alert severity="error" title={errorMessage} className="my-4" />} |
| 101 | + |
| 102 | + <div className="mt-5 justify-end space-x-2 rtl:space-x-reverse sm:mt-4 sm:flex"> |
| 103 | + <Button type="button" color="secondary" onClick={() => router.back()}> |
| 104 | + {t("cancel")} |
| 105 | + </Button> |
| 106 | + <Button type="submit" loading={form.formState.isSubmitting}> |
| 107 | + {t("save")} |
| 108 | + </Button> |
| 109 | + </div> |
| 110 | + </Form> |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + <Toaster position="bottom-right" /> |
| 116 | + </div> |
| 117 | + ); |
| 118 | +} |
0 commit comments