File size: 5,057 Bytes
ec4551b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | 'use client';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { useFetch } from '@gitroom/helpers/utils/custom.fetch';
import useSWR from 'swr';
import { Slider } from '@gitroom/react/form/slider';
import { useToaster } from '@gitroom/react/toaster/toaster';
import { useT } from '@gitroom/react/translation/get.transation.service.client';
interface EmailNotifications {
sendSuccessEmails: boolean;
sendFailureEmails: boolean;
sendStreakEmails: boolean;
}
export const useEmailNotifications = () => {
const fetch = useFetch();
const load = useCallback(async () => {
return (await fetch('/user/email-notifications')).json();
}, []);
return useSWR<EmailNotifications>('email-notifications', load, {
revalidateOnFocus: false,
revalidateOnReconnect: false,
revalidateIfStale: false,
revalidateOnMount: true,
refreshWhenHidden: false,
refreshWhenOffline: false,
});
};
const EmailNotificationsComponent = () => {
const t = useT();
const fetch = useFetch();
const toaster = useToaster();
const { data, isLoading } = useEmailNotifications();
const [localSettings, setLocalSettings] = useState<EmailNotifications>({
sendSuccessEmails: true,
sendFailureEmails: true,
sendStreakEmails: true,
});
// Keep a ref to always have the latest state
const settingsRef = useRef(localSettings);
settingsRef.current = localSettings;
// Sync local state with fetched data
useEffect(() => {
if (data) {
setLocalSettings(data);
}
}, [data]);
const updateSetting = useCallback(
async (key: keyof EmailNotifications, value: boolean) => {
// Use ref to get the latest state
const currentSettings = settingsRef.current;
const newData = {
...currentSettings,
[key]: value,
};
// Update local state immediately
setLocalSettings(newData);
await fetch('/user/email-notifications', {
method: 'POST',
body: JSON.stringify(newData),
});
toaster.show(t('settings_updated', 'Settings updated'), 'success');
},
[]
);
const handleSuccessEmailsChange = useCallback(
(value: 'on' | 'off') => {
updateSetting('sendSuccessEmails', value === 'on');
},
[updateSetting]
);
const handleFailureEmailsChange = useCallback(
(value: 'on' | 'off') => {
updateSetting('sendFailureEmails', value === 'on');
},
[updateSetting]
);
const handleStreakEmailsChange = useCallback(
(value: 'on' | 'off') => {
updateSetting('sendStreakEmails', value === 'on');
},
[updateSetting]
);
if (isLoading) {
return (
<div className="my-[16px] mt-[16px] bg-sixth border-fifth border rounded-[4px] p-[24px]">
<div className="animate-pulse">
{t('loading', 'Loading...')}
</div>
</div>
);
}
return (
<div className="my-[16px] mt-[16px] bg-sixth border-fifth border rounded-[4px] p-[24px] flex flex-col gap-[24px]">
<div className="mt-[4px]">
{t('email_notifications', 'Email Notifications')}
</div>
<div className="flex items-center justify-between">
<div className="flex flex-col">
<div className="text-[14px]">
{t('success_emails', 'Success Emails')}
</div>
<div className="text-[12px] text-customColor18">
{t(
'success_emails_description',
'Receive email notifications when posts are published successfully'
)}
</div>
</div>
<Slider
value={localSettings.sendSuccessEmails ? 'on' : 'off'}
onChange={handleSuccessEmailsChange}
fill={true}
/>
</div>
<div className="flex items-center justify-between">
<div className="flex flex-col">
<div className="text-[14px]">
{t('failure_emails', 'Failure Emails')}
</div>
<div className="text-[12px] text-customColor18">
{t(
'failure_emails_description',
'Receive email notifications when posts fail to publish'
)}
</div>
</div>
<Slider
value={localSettings.sendFailureEmails ? 'on' : 'off'}
onChange={handleFailureEmailsChange}
fill={true}
/>
</div>
<div className="flex items-center justify-between">
<div className="flex flex-col">
<div className="text-[14px]">
{t('streak_emails', 'Streak Reminder Emails')}
</div>
<div className="text-[12px] text-customColor18">
{t(
'streak_emails_description',
'Receive email reminders when your posting streak is about to end'
)}
</div>
</div>
<Slider
value={localSettings.sendStreakEmails ? 'on' : 'off'}
onChange={handleStreakEmailsChange}
fill={true}
/>
</div>
</div>
);
};
export default EmailNotificationsComponent;
|