Merge branch 'DRVDOC-1260' into 'main'
[ProtonMail-WebClient.git] / packages / components / containers / security / KTToggle.tsx
blob880066dc008c2c13d43e941ad99b69c93bd6eec8
1 import type { ChangeEvent } from 'react';
3 import { c } from 'ttag';
5 import Toggle from '@proton/components/components/toggle/Toggle';
6 import useApi from '@proton/components/hooks/useApi';
7 import useEventManager from '@proton/components/hooks/useEventManager';
8 import useNotifications from '@proton/components/hooks/useNotifications';
9 import useLoading from '@proton/hooks/useLoading';
10 import { useMailSettings } from '@proton/mail/mailSettings/hooks';
11 import { updateKT } from '@proton/shared/lib/api/mailSettings';
12 import { DEFAULT_MAILSETTINGS } from '@proton/shared/lib/mail/mailSettings';
14 interface Props {
15     id?: string;
18 const KTToggle = ({ id }: Props) => {
19     const { createNotification } = useNotifications();
20     const { call } = useEventManager();
21     const api = useApi();
22     const [loading, withLoading] = useLoading();
23     const [{ KT } = DEFAULT_MAILSETTINGS] = useMailSettings();
25     const handleChange = async ({ target }: ChangeEvent<HTMLInputElement>) => {
26         await api(updateKT(+target.checked));
27         await call();
28         createNotification({ text: c('Success').t`Preference saved` });
29     };
31     return <Toggle id={id} loading={loading} checked={!!KT} onChange={(e) => withLoading(handleChange(e))} />;
34 export default KTToggle;