feat: show unsubscribed on notification setting

This commit is contained in:
Ben Furber
2024-08-14 13:58:14 +01:00
committed by benfurber
parent 0ef54b6a7a
commit 3b54bc29df
6 changed files with 74 additions and 6 deletions

View File

@@ -50,7 +50,7 @@ export interface IUser {
profileCreated?: ISODateString profileCreated?: ISODateString
profileCreationTrigger?: string profileCreationTrigger?: string
// Used to generate an encrypted unsubscribe url in emails // Used to generate an encrypted unsubscribe url in emails
unsubscribeToken?: string unsubscribeToken?: string | null
impact?: IUserImpact impact?: IUserImpact
isBlockedFromMessaging?: boolean isBlockedFromMessaging?: boolean
isContactableByPublic?: boolean isContactableByPublic?: boolean

View File

@@ -40,4 +40,23 @@ describe('SettingsPageNotifications', () => {
expect(wrapper.queryByText('Weekly')).toBeNull() 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, <SettingsPageNotifications />)
})
await waitFor(() => {
expect(wrapper.getAllByText('Never', { exact: false })).toHaveLength(1)
expect(wrapper.queryByText('Weekly')).toBeNull()
})
})
}) })

View File

@@ -1,6 +1,7 @@
import { useState } from 'react' import { useState } from 'react'
import { Form } from 'react-final-form' import { Form } from 'react-final-form'
import { Loader } from 'oa-components' import { Loader } from 'oa-components'
import { EmailNotificationFrequency } from 'oa-shared'
import { useCommonStores } from 'src/common/hooks/useCommonStores' import { useCommonStores } from 'src/common/hooks/useCommonStores'
import { import {
buttons, buttons,
@@ -33,6 +34,7 @@ export const SettingsPageNotifications = () => {
...user, ...user,
notification_settings, notification_settings,
} }
const updatedUser =
await userStore.updateUserNotificationSettings(updatingUser) await userStore.updateUserNotificationSettings(updatingUser)
setNotification({ setNotification({
@@ -41,6 +43,12 @@ export const SettingsPageNotifications = () => {
show: true, show: true,
variant: 'success', variant: 'success',
}) })
setInitialValues({
notification_settings: {
...updatedUser.notification_settings,
emailFrequency,
},
})
} catch (error) { } catch (error) {
setNotification({ setNotification({
message: `Save Failed - ${error.message} `, message: `Save Failed - ${error.message} `,
@@ -54,9 +62,17 @@ export const SettingsPageNotifications = () => {
if (!user) return null if (!user) return null
const initialValues = { const isUnsubscribed = !!user.unsubscribeToken
notification_settings: user.notification_settings || undefined, const emailFrequency = isUnsubscribed
} ? EmailNotificationFrequency.NEVER
: user.notification_settings?.emailFrequency
const [initialValues, setInitialValues] = useState({
notification_settings: {
...user.notification_settings,
emailFrequency,
},
})
return ( return (
<Flex <Flex

View File

@@ -15,7 +15,7 @@ const emailFrequencyOptions: {
value: EmailNotificationFrequency value: EmailNotificationFrequency
label: string label: string
}[] = [ }[] = [
{ value: EmailNotificationFrequency.NEVER, label: 'Never' }, { value: EmailNotificationFrequency.NEVER, label: 'Never (Unsubscribed)' },
{ value: EmailNotificationFrequency.DAILY, label: 'Daily' }, { value: EmailNotificationFrequency.DAILY, label: 'Daily' },
{ value: EmailNotificationFrequency.WEEKLY, label: 'Weekly' }, { value: EmailNotificationFrequency.WEEKLY, label: 'Weekly' },
{ value: EmailNotificationFrequency.MONTHLY, label: 'Monthly' }, { value: EmailNotificationFrequency.MONTHLY, label: 'Monthly' },

View File

@@ -456,6 +456,33 @@ describe('userStore', () => {
) )
}) })
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 () => { it('throws an error is no user id is provided', async () => {
const values = {} const values = {}

View File

@@ -257,8 +257,14 @@ export class UserStore extends ModuleStore {
throw new Error('notification_settings not found') throw new Error('notification_settings not found')
} }
const unsubscribeToken =
notification_settings.emailFrequency === EmailNotificationFrequency.NEVER
? this.activeUser.unsubscribeToken
: null
await this._updateUserRequest(_id, { await this._updateUserRequest(_id, {
notification_settings, notification_settings,
unsubscribeToken,
}) })
await this.refreshActiveUserDetails() await this.refreshActiveUserDetails()