2022-12-07 20:53:44 +00:00
|
|
|
import { GetServerSidePropsContext } from "next";
|
2022-10-19 09:33:40 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Form,
|
|
|
|
getSettingsLayout as getLayout,
|
|
|
|
Label,
|
|
|
|
Meta,
|
|
|
|
showToast,
|
|
|
|
Skeleton,
|
|
|
|
Switch,
|
|
|
|
} from "@calcom/ui";
|
2022-10-19 09:33:40 +00:00
|
|
|
|
2022-12-07 20:53:44 +00:00
|
|
|
import { ssrInit } from "@server/lib/ssr";
|
|
|
|
|
2022-10-19 09:33:40 +00:00
|
|
|
const ProfileImpersonationView = () => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const utils = trpc.useContext();
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data: user } = trpc.viewer.me.useQuery();
|
|
|
|
const mutation = trpc.viewer.updateProfile.useMutation({
|
2022-10-19 09:33:40 +00:00
|
|
|
onSuccess: () => {
|
|
|
|
showToast(t("profile_updated_successfully"), "success");
|
2022-12-20 23:13:31 +00:00
|
|
|
reset(getValues());
|
2022-10-19 09:33:40 +00:00
|
|
|
},
|
2022-12-20 23:13:31 +00:00
|
|
|
onSettled: () => {
|
|
|
|
utils.viewer.me.invalidate();
|
|
|
|
},
|
|
|
|
onMutate: async ({ disableImpersonation }) => {
|
|
|
|
await utils.viewer.me.cancel();
|
|
|
|
const previousValue = utils.viewer.me.getData();
|
|
|
|
|
|
|
|
if (previousValue && disableImpersonation) {
|
|
|
|
utils.viewer.me.setData(undefined, { ...previousValue, disableImpersonation });
|
|
|
|
}
|
|
|
|
return { previousValue };
|
|
|
|
},
|
|
|
|
onError: (error, variables, context) => {
|
|
|
|
if (context?.previousValue) {
|
|
|
|
utils.viewer.me.setData(undefined, context.previousValue);
|
|
|
|
}
|
2022-10-19 09:33:40 +00:00
|
|
|
showToast(`${t("error")}, ${error.message}`, "error");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const formMethods = useForm<{ disableImpersonation: boolean }>({
|
|
|
|
defaultValues: {
|
|
|
|
disableImpersonation: user?.disableImpersonation,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const {
|
2022-12-20 23:13:31 +00:00
|
|
|
formState: { isSubmitting, isDirty },
|
2022-10-19 09:33:40 +00:00
|
|
|
setValue,
|
2022-12-20 23:13:31 +00:00
|
|
|
reset,
|
|
|
|
getValues,
|
2022-10-19 09:33:40 +00:00
|
|
|
} = formMethods;
|
|
|
|
|
2022-12-20 23:13:31 +00:00
|
|
|
const isDisabled = isSubmitting || !isDirty;
|
2022-10-19 09:33:40 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-12-07 20:53:44 +00:00
|
|
|
<Meta title={t("impersonation")} description={t("impersonation_description")} />
|
2022-10-19 09:33:40 +00:00
|
|
|
<Form
|
|
|
|
form={formMethods}
|
|
|
|
handleSubmit={({ disableImpersonation }) => {
|
|
|
|
mutation.mutate({ disableImpersonation });
|
|
|
|
}}>
|
|
|
|
<div className="flex space-x-3">
|
|
|
|
<Switch
|
|
|
|
defaultChecked={!user?.disableImpersonation}
|
|
|
|
onCheckedChange={(e) => {
|
2022-12-20 23:13:31 +00:00
|
|
|
setValue("disableImpersonation", !e, { shouldDirty: true });
|
2022-10-19 09:33:40 +00:00
|
|
|
}}
|
|
|
|
fitToHeight={true}
|
|
|
|
/>
|
|
|
|
<div className="flex flex-col">
|
|
|
|
<Skeleton as={Label} className="text-sm font-semibold leading-none text-black">
|
|
|
|
{t("user_impersonation_heading")}
|
|
|
|
</Skeleton>
|
|
|
|
<Skeleton as="p" className="-mt-2 text-sm leading-normal text-gray-600">
|
|
|
|
{t("user_impersonation_description")}
|
|
|
|
</Skeleton>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-12-20 23:13:31 +00:00
|
|
|
<Button color="primary" className="mt-8" type="submit" disabled={isDisabled}>
|
2022-10-19 09:33:40 +00:00
|
|
|
{t("update")}
|
|
|
|
</Button>
|
|
|
|
</Form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ProfileImpersonationView.getLayout = getLayout;
|
|
|
|
|
2022-12-07 20:53:44 +00:00
|
|
|
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
|
|
|
|
const ssr = await ssrInit(context);
|
2022-12-20 23:13:31 +00:00
|
|
|
await ssr.viewer.me.prefetch();
|
2022-12-07 20:53:44 +00:00
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
trpcState: ssr.dehydrate(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-19 09:33:40 +00:00
|
|
|
export default ProfileImpersonationView;
|