1 import { useState } from 'react';
3 import { userSettingsActions } from '@proton/account';
4 import { useUser } from '@proton/account/user/hooks';
5 import { useUserSettings } from '@proton/account/userSettings/hooks';
7 type EmailSubscription,
10 getUpdateNotification,
11 } from '@proton/components/containers/account/constants/email-subscriptions';
12 import useApi from '@proton/components/hooks/useApi';
13 import useNotifications from '@proton/components/hooks/useNotifications';
14 import { useDispatch } from '@proton/redux-shared-store';
15 import { patchNews } from '@proton/shared/lib/api/settings';
16 import { type NewsletterSubscriptionUpdateData, getUpdatedNewsBitmap } from '@proton/shared/lib/helpers/newsletter';
17 import type { UserSettings } from '@proton/shared/lib/interfaces';
19 import { EmailSubscriptionToggleWithHeader } from './EmailSubscriptionToggles';
21 const EditEmailSubscription = () => {
22 const [user] = useUser();
23 const [userSettings] = useUserSettings();
24 const { createNotification } = useNotifications();
25 const dispatch = useDispatch();
27 const [loadingMap, setLoadingMap] = useState<{ [key: string]: boolean }>({});
29 const run = async (data: NewsletterSubscriptionUpdateData) => {
30 dispatch(userSettingsActions.update({ UserSettings: { News: getUpdatedNewsBitmap(userSettings.News, data) } }));
31 await api<{ UserSettings: UserSettings }>(patchNews(data));
32 createNotification({ text: getUpdateNotification(data) });
35 const setLoadingMapDiff = (data: NewsletterSubscriptionUpdateData, value: boolean) => {
36 setLoadingMap((oldValue) => ({
38 ...Object.fromEntries(Object.entries(data).map(([key]) => [key, value])),
42 const handleChange = async (data: NewsletterSubscriptionUpdateData) => {
43 setLoadingMapDiff(data, true);
44 run(data).finally(() => {
45 setLoadingMapDiff(data, false);
49 const filter = (emailSubscription: EmailSubscription) =>
56 const { general, product, notifications } = getEmailSubscriptions(filter);
59 onChange: handleChange,
61 News: userSettings.News,
65 <div className="flex flex-column gap-4">
66 <EmailSubscriptionToggleWithHeader title={general.title} subscriptions={general.toggles} {...sharedProps} />
67 <EmailSubscriptionToggleWithHeader title={product.title} subscriptions={product.toggles} {...sharedProps} />
68 <EmailSubscriptionToggleWithHeader
69 title={notifications.title}
70 subscriptions={notifications.toggles}
77 export default EditEmailSubscription;