Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / components / hooks / docs / useDocsEmailNotificationsEnabled.tsx
blobef7618e82387e9059d12dfe0b7fc3e566e862a0c
1 import { useEffect, useState } from 'react';
3 import { c } from 'ttag';
5 import { useLoading } from '@proton/hooks';
6 import { queryUpdateUserSettings, queryUserSettings } from '@proton/shared/lib/api/docs/user';
7 import type { UserSettingsResponse } from '@proton/shared/lib/interfaces/docs/userSettings';
9 import useApi from '../useApi';
10 import useNotifications from '../useNotifications';
12 export const useDocsEmailNotificationsEnabled = () => {
13     const api = useApi();
14     const [isLoading, withLoading] = useLoading();
15     const [isSubmitting, withSubmitLoading] = useLoading();
16     const { createNotification } = useNotifications();
18     const [emailNotificationsEnabled, setEmailNotificationsEnabled] = useState<boolean>(true);
20     useEffect(() => {
21         void withLoading(
22             api<UserSettingsResponse>(queryUserSettings()).then(({ UserSettings, Defaults }) => {
23                 setEmailNotificationsEnabled(
24                     UserSettings.DocsCommentsNotificationsEnabled || Defaults.DocsCommentsNotificationsEnabled
25                 );
26             })
27         );
28     }, []);
30     const handleChange = (value: boolean) => {
31         return withSubmitLoading(
32             api<UserSettingsResponse>(
33                 queryUpdateUserSettings({
34                     DocsCommentsNotificationsEnabled: value,
35                 })
36             )
37         )
38             .then(() => {
39                 createNotification({
40                     text: c('Info').t`Settings updated`,
41                 });
42                 setEmailNotificationsEnabled(value);
43             })
44             .catch((err) => {
45                 createNotification({
46                     type: 'error',
47                     text: c('Info').t`Settings update failed`,
48                 });
49                 // eslint-disable-next-line no-console
50                 console.error('Settings update failed.', err);
51             });
52     };
54     return {
55         emailNotificationsEnabled,
56         isLoading,
57         isSubmitting,
58         handleChange,
59     };