Skip to content

Commit

Permalink
feat: show unsubscribed on notification setting
Browse files Browse the repository at this point in the history
  • Loading branch information
benfurber committed Aug 14, 2024
1 parent 0ef54b6 commit 3b54bc2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/models/user.models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/pages/UserSettings/SettingsPageNotifications.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, <SettingsPageNotifications />)
})

await waitFor(() => {
expect(wrapper.getAllByText('Never', { exact: false })).toHaveLength(1)
expect(wrapper.queryByText('Weekly')).toBeNull()
})
})
})
24 changes: 20 additions & 4 deletions src/pages/UserSettings/SettingsPageNotifications.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -33,14 +34,21 @@ export const SettingsPageNotifications = () => {
...user,
notification_settings,
}
await userStore.updateUserNotificationSettings(updatingUser)
const updatedUser =
await userStore.updateUserNotificationSettings(updatingUser)

setNotification({
message: notificationForm.succesfulSave,
icon: 'check',
show: true,
variant: 'success',
})
setInitialValues({
notification_settings: {
...updatedUser.notification_settings,
emailFrequency,
},
})
} catch (error) {
setNotification({
message: `Save Failed - ${error.message} `,
Expand All @@ -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 (
<Flex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const emailFrequencyOptions: {
value: EmailNotificationFrequency
label: string
}[] = [
{ value: EmailNotificationFrequency.NEVER, label: 'Never' },
{ value: EmailNotificationFrequency.NEVER, label: 'Never (Unsubscribed)' },
{ value: EmailNotificationFrequency.DAILY, label: 'Daily' },
{ value: EmailNotificationFrequency.WEEKLY, label: 'Weekly' },
{ value: EmailNotificationFrequency.MONTHLY, label: 'Monthly' },
Expand Down
27 changes: 27 additions & 0 deletions src/stores/User/user.store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
const values = {}

Expand Down
6 changes: 6 additions & 0 deletions src/stores/User/user.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 3b54bc2

Please sign in to comment.