From 3b54bc29df01fc06a604930425efd090d56a15dc Mon Sep 17 00:00:00 2001 From: Ben Furber Date: Wed, 14 Aug 2024 13:58:14 +0100 Subject: [PATCH] feat: show unsubscribed on notification setting --- src/models/user.models.tsx | 2 +- .../SettingsPageNotifications.test.tsx | 19 +++++++++++++ .../SettingsPageNotifications.tsx | 24 ++++++++++++++--- .../sections/EmailNotifications.section.tsx | 2 +- src/stores/User/user.store.test.tsx | 27 +++++++++++++++++++ src/stores/User/user.store.ts | 6 +++++ 6 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/models/user.models.tsx b/src/models/user.models.tsx index ba69c2c334..7142e0ce8d 100644 --- a/src/models/user.models.tsx +++ b/src/models/user.models.tsx @@ -50,7 +50,7 @@ export interface IUser { profileCreated?: ISODateString profileCreationTrigger?: string // Used to generate an encrypted unsubscribe url in emails - unsubscribeToken?: string + unsubscribeToken?: string | null impact?: IUserImpact isBlockedFromMessaging?: boolean isContactableByPublic?: boolean diff --git a/src/pages/UserSettings/SettingsPageNotifications.test.tsx b/src/pages/UserSettings/SettingsPageNotifications.test.tsx index a27f71322a..69910190ff 100644 --- a/src/pages/UserSettings/SettingsPageNotifications.test.tsx +++ b/src/pages/UserSettings/SettingsPageNotifications.test.tsx @@ -40,4 +40,23 @@ describe('SettingsPageNotifications', () => { expect(wrapper.queryByText('Weekly')).toBeNull() }) }) + + it('renders the option as never when a unsubscribe token is present', async () => { + mockUser = FactoryUser({ + notification_settings: { + emailFrequency: EmailNotificationFrequency.MONTHLY, + }, + unsubscribeToken: 'something', + }) + // Act + let wrapper + act(() => { + wrapper = FormProvider(mockUser, ) + }) + + await waitFor(() => { + expect(wrapper.getAllByText('Never', { exact: false })).toHaveLength(1) + expect(wrapper.queryByText('Weekly')).toBeNull() + }) + }) }) diff --git a/src/pages/UserSettings/SettingsPageNotifications.tsx b/src/pages/UserSettings/SettingsPageNotifications.tsx index 3bc3a12cf3..8bb2c90675 100644 --- a/src/pages/UserSettings/SettingsPageNotifications.tsx +++ b/src/pages/UserSettings/SettingsPageNotifications.tsx @@ -1,6 +1,7 @@ import { useState } from 'react' import { Form } from 'react-final-form' import { Loader } from 'oa-components' +import { EmailNotificationFrequency } from 'oa-shared' import { useCommonStores } from 'src/common/hooks/useCommonStores' import { buttons, @@ -33,7 +34,8 @@ export const SettingsPageNotifications = () => { ...user, notification_settings, } - await userStore.updateUserNotificationSettings(updatingUser) + const updatedUser = + await userStore.updateUserNotificationSettings(updatingUser) setNotification({ message: notificationForm.succesfulSave, @@ -41,6 +43,12 @@ export const SettingsPageNotifications = () => { show: true, variant: 'success', }) + setInitialValues({ + notification_settings: { + ...updatedUser.notification_settings, + emailFrequency, + }, + }) } catch (error) { setNotification({ message: `Save Failed - ${error.message} `, @@ -54,9 +62,17 @@ export const SettingsPageNotifications = () => { if (!user) return null - const initialValues = { - notification_settings: user.notification_settings || undefined, - } + const isUnsubscribed = !!user.unsubscribeToken + const emailFrequency = isUnsubscribed + ? EmailNotificationFrequency.NEVER + : user.notification_settings?.emailFrequency + + const [initialValues, setInitialValues] = useState({ + notification_settings: { + ...user.notification_settings, + emailFrequency, + }, + }) return ( { ) }) + it('clears the unsubscribe token', async () => { + const userProfile = FactoryUser({ + _id: 'my-user-profile', + notification_settings: { + emailFrequency: EmailNotificationFrequency.NEVER, + }, + unsubscribeToken: 'anything', + }) + store.activeUser = userProfile + + const notification_settings = { + emailFrequency: EmailNotificationFrequency.DAILY, + } + const updateValues = { + _id: userProfile._id, + notification_settings, + } + await store.updateUserNotificationSettings(updateValues) + + expect(store.db.update).toHaveBeenCalledWith( + expect.objectContaining({ + notification_settings, + unsubscribeToken: null, + }), + ) + }) + it('throws an error is no user id is provided', async () => { const values = {} diff --git a/src/stores/User/user.store.ts b/src/stores/User/user.store.ts index e88050fb08..2809370b58 100644 --- a/src/stores/User/user.store.ts +++ b/src/stores/User/user.store.ts @@ -257,8 +257,14 @@ export class UserStore extends ModuleStore { throw new Error('notification_settings not found') } + const unsubscribeToken = + notification_settings.emailFrequency === EmailNotificationFrequency.NEVER + ? this.activeUser.unsubscribeToken + : null + await this._updateUserRequest(_id, { notification_settings, + unsubscribeToken, }) await this.refreshActiveUserDetails()