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 = () => {
14 const [isLoading, withLoading] = useLoading();
15 const [isSubmitting, withSubmitLoading] = useLoading();
16 const { createNotification } = useNotifications();
18 const [emailNotificationsEnabled, setEmailNotificationsEnabled] = useState<boolean>(true);
22 api<UserSettingsResponse>(queryUserSettings()).then(({ UserSettings, Defaults }) => {
23 setEmailNotificationsEnabled(
24 UserSettings.DocsCommentsNotificationsEnabled || Defaults.DocsCommentsNotificationsEnabled
30 const handleChange = (value: boolean) => {
31 return withSubmitLoading(
32 api<UserSettingsResponse>(
33 queryUpdateUserSettings({
34 DocsCommentsNotificationsEnabled: value,
40 text: c('Info').t`Settings updated`,
42 setEmailNotificationsEnabled(value);
47 text: c('Info').t`Settings update failed`,
49 // eslint-disable-next-line no-console
50 console.error('Settings update failed.', err);
55 emailNotificationsEnabled,