mirror of
https://github.com/fergalmoran/onearmy-community-platform.git
synced 2025-12-22 09:37:54 +00:00
feat: show unsubscribed on notification setting
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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 (
|
||||
<Flex
|
||||
|
||||
@@ -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' },
|
||||
|
||||
@@ -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 = {}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user