Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / hooks / docs / useDocsNotificationsSettings.tsx
blob1d93260809a76b61d084e813090e533f9362caa3
1 import { useState } from 'react';
3 import { c } from 'ttag';
5 import { useEffectOnce, 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 useDocsNotificationsSettings = () => {
13     const api = useApi();
14     const [isReady, setIsReady] = useState(false);
15     const [isLoading, withLoading] = useLoading();
16     const [isSubmitting, withSubmitLoading] = useLoading();
17     const { createNotification } = useNotifications();
19     const [emailNotificationsEnabled, setEmailNotificationsEnabled] = useState<boolean | null>(null);
20     const [emailTitleEnabled, setEmailTitleEnabled] = useState<boolean | null>(null);
22     useEffectOnce(() => {
23         void withLoading(
24             api<UserSettingsResponse>(queryUserSettings()).then(({ UserSettings }) => {
25                 setEmailNotificationsEnabled(UserSettings.DocsCommentsNotificationsEnabled);
26                 setEmailTitleEnabled(UserSettings.DocsCommentsNotificationsIncludeDocumentName);
27                 setIsReady(true);
28             })
29         );
30     });
32     const updateNotificationSettings = ({
33         notificationsEnabled,
34         includeTitleEnabled,
35     }: {
36         notificationsEnabled: boolean;
37         includeTitleEnabled: boolean;
38     }) => {
39         return withSubmitLoading(
40             api<UserSettingsResponse>(
41                 queryUpdateUserSettings({
42                     DocsCommentsNotificationsEnabled: notificationsEnabled,
43                     DocsCommentsNotificationsIncludeDocumentName: includeTitleEnabled,
44                 })
45             )
46         )
47             .then(() => {
48                 createNotification({
49                     text: c('Info').t`Settings updated`,
50                 });
51                 setEmailNotificationsEnabled(notificationsEnabled);
52                 setEmailTitleEnabled(includeTitleEnabled);
53             })
54             .catch((err) => {
55                 createNotification({
56                     type: 'error',
57                     text: c('Info').t`Settings update failed`,
58                 });
59                 // eslint-disable-next-line no-console
60                 console.error('Settings update failed.', err);
61             });
62     };
64     const changeEmailNotificationsEnabledValue = (value: boolean) => {
65         return updateNotificationSettings({
66             notificationsEnabled: value,
67             includeTitleEnabled: emailTitleEnabled ?? false,
68         });
69     };
71     const changeDocumentTitleEnabledValue = (value: boolean) => {
72         return updateNotificationSettings({
73             notificationsEnabled: emailNotificationsEnabled ?? false,
74             includeTitleEnabled: value,
75         });
76     };
78     return {
79         emailNotificationsEnabled,
80         emailTitleEnabled,
81         isLoading,
82         isSubmitting,
83         isReady,
84         changeEmailNotificationsEnabledValue,
85         changeDocumentTitleEnabledValue,
86         updateNotificationSettings,
87     };