2022-09-14 16:09:00 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { useState } from "react";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
import { Toaster } from "react-hot-toast";
|
|
|
|
import z from "zod";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { Alert, Button, EmailField, Form, PasswordField, SelectField, Switch, TextField } from "@calcom/ui";
|
2022-09-14 16:09:00 +00:00
|
|
|
|
2023-04-17 14:21:14 +00:00
|
|
|
import { ExchangeAuthentication, ExchangeVersion } from "../../enums";
|
2022-09-14 16:09:00 +00:00
|
|
|
|
|
|
|
interface IFormData {
|
|
|
|
url: string;
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
authenticationMethod: ExchangeAuthentication;
|
2023-04-17 14:21:14 +00:00
|
|
|
exchangeVersion: ExchangeVersion;
|
2022-09-14 16:09:00 +00:00
|
|
|
useCompression: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const schema = z
|
|
|
|
.object({
|
|
|
|
url: z.string().url(),
|
|
|
|
username: z.string().email(),
|
|
|
|
password: z.string(),
|
|
|
|
authenticationMethod: z.number().default(ExchangeAuthentication.STANDARD),
|
2023-04-17 14:21:14 +00:00
|
|
|
exchangeVersion: z.number().default(ExchangeVersion.Exchange2016),
|
2022-09-14 16:09:00 +00:00
|
|
|
useCompression: z.boolean().default(false),
|
|
|
|
})
|
|
|
|
.strict();
|
|
|
|
|
|
|
|
export default function ExchangeSetup() {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const router = useRouter();
|
|
|
|
const [errorMessage, setErrorMessage] = useState("");
|
|
|
|
const form = useForm<IFormData>({
|
2023-04-17 14:21:14 +00:00
|
|
|
defaultValues: {
|
|
|
|
authenticationMethod: ExchangeAuthentication.STANDARD,
|
|
|
|
exchangeVersion: ExchangeVersion.Exchange2016,
|
|
|
|
},
|
2022-09-14 16:09:00 +00:00
|
|
|
resolver: zodResolver(schema),
|
|
|
|
});
|
|
|
|
const authenticationMethods = [
|
|
|
|
{ value: ExchangeAuthentication.STANDARD, label: t("exchange_authentication_standard") },
|
|
|
|
{ value: ExchangeAuthentication.NTLM, label: t("exchange_authentication_ntlm") },
|
|
|
|
];
|
2023-04-17 14:21:14 +00:00
|
|
|
const exchangeVersions = [
|
|
|
|
{ value: ExchangeVersion.Exchange2007_SP1, label: t("exchange_version_2007_SP1") },
|
|
|
|
{ value: ExchangeVersion.Exchange2010, label: t("exchange_version_2010") },
|
|
|
|
{ value: ExchangeVersion.Exchange2010_SP1, label: t("exchange_version_2010_SP1") },
|
|
|
|
{ value: ExchangeVersion.Exchange2010_SP2, label: t("exchange_version_2010_SP2") },
|
|
|
|
{ value: ExchangeVersion.Exchange2013, label: t("exchange_version_2013") },
|
|
|
|
{ value: ExchangeVersion.Exchange2013_SP1, label: t("exchange_version_2013_SP1") },
|
|
|
|
{ value: ExchangeVersion.Exchange2015, label: t("exchange_version_2015") },
|
|
|
|
{ value: ExchangeVersion.Exchange2016, label: t("exchange_version_2016") },
|
|
|
|
];
|
|
|
|
|
2022-09-14 16:09:00 +00:00
|
|
|
return (
|
|
|
|
<>
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="bg-emphasis flex h-screen">
|
|
|
|
<div className="bg-default m-auto rounded p-5 md:w-[560px] md:p-10">
|
2023-06-22 22:25:37 +00:00
|
|
|
<div className="flex flex-col space-y-5 md:flex-row md:space-x-5 md:space-y-0">
|
2022-10-27 05:50:16 +00:00
|
|
|
<div>
|
|
|
|
{/* eslint-disable @next/next/no-img-element */}
|
2022-09-14 16:09:00 +00:00
|
|
|
<img
|
|
|
|
src="/api/app-store/exchangecalendar/icon.svg"
|
2022-10-27 05:50:16 +00:00
|
|
|
alt="Microsoft Exchange"
|
2022-09-14 16:09:00 +00:00
|
|
|
className="h-12 w-12 max-w-2xl"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="grow">
|
2023-04-05 18:14:46 +00:00
|
|
|
<h1 className="text-default">{t("exchange_add")}</h1>
|
2022-09-14 16:09:00 +00:00
|
|
|
<div className="text-sm">{t("credentials_stored_encrypted")}</div>
|
|
|
|
<div className="my-2 mt-5">
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={async (values) => {
|
|
|
|
setErrorMessage("");
|
|
|
|
const res = await fetch("/api/integrations/exchangecalendar/add", {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(values),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const json = await res.json();
|
|
|
|
if (!res.ok) {
|
|
|
|
setErrorMessage(json?.message || t("something_went_wrong"));
|
|
|
|
} else {
|
|
|
|
router.push(json.url);
|
|
|
|
}
|
|
|
|
}}>
|
|
|
|
<fieldset className="space-y-4" disabled={form.formState.isSubmitting}>
|
|
|
|
<TextField
|
|
|
|
required
|
|
|
|
type="url"
|
|
|
|
{...form.register("url")}
|
|
|
|
label={t("url")}
|
|
|
|
placeholder="https://example.com/Ews/Exchange.asmx"
|
|
|
|
inputMode="url"
|
|
|
|
/>
|
|
|
|
<EmailField
|
|
|
|
required
|
|
|
|
{...form.register("username")}
|
|
|
|
label={t("email_address")}
|
|
|
|
placeholder="john.doe@example.com"
|
|
|
|
/>
|
|
|
|
<PasswordField
|
|
|
|
required
|
|
|
|
{...form.register("password")}
|
|
|
|
label={t("password")}
|
|
|
|
autoComplete="password"
|
|
|
|
/>
|
2022-10-27 05:50:16 +00:00
|
|
|
<Controller
|
|
|
|
name="authenticationMethod"
|
|
|
|
control={form.control}
|
|
|
|
render={({ field: { onChange } }) => (
|
|
|
|
<SelectField
|
|
|
|
label={t("exchange_authentication")}
|
|
|
|
options={authenticationMethods}
|
|
|
|
defaultValue={authenticationMethods[0]}
|
|
|
|
onChange={async (authentication) => {
|
2023-06-06 11:59:57 +00:00
|
|
|
if (authentication) {
|
|
|
|
onChange(authentication.value);
|
|
|
|
form.setValue("authenticationMethod", authentication.value);
|
|
|
|
}
|
2022-10-27 05:50:16 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2023-04-17 14:21:14 +00:00
|
|
|
<Controller
|
|
|
|
name="exchangeVersion"
|
|
|
|
control={form.control}
|
|
|
|
render={({ field: { onChange } }) => (
|
|
|
|
<SelectField
|
|
|
|
label={t("exchange_version")}
|
|
|
|
options={exchangeVersions}
|
|
|
|
defaultValue={exchangeVersions[7]}
|
|
|
|
onChange={async (version) => {
|
|
|
|
onChange(version?.value);
|
2023-06-06 11:59:57 +00:00
|
|
|
if (version) {
|
|
|
|
form.setValue("exchangeVersion", version.value);
|
|
|
|
}
|
2023-04-17 14:21:14 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2022-09-14 16:09:00 +00:00
|
|
|
<Switch
|
|
|
|
label={t("exchange_compression")}
|
|
|
|
name="useCompression"
|
|
|
|
onCheckedChange={async (alt) => {
|
|
|
|
form.setValue("useCompression", alt);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</fieldset>
|
|
|
|
{errorMessage && <Alert severity="error" title={errorMessage} className="my-4" />}
|
2023-01-04 07:38:45 +00:00
|
|
|
<div className="mt-4 flex justify-end space-x-2 rtl:space-x-reverse">
|
2022-09-14 16:09:00 +00:00
|
|
|
<Button type="button" color="secondary" onClick={() => router.back()}>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
<Button type="submit" loading={form.formState.isSubmitting}>
|
|
|
|
{t("save")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<Toaster position="bottom-right" />
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|