Skip to content

Commit

Permalink
fix: setting countryCode when updating country
Browse files Browse the repository at this point in the history
  • Loading branch information
benfurber committed Sep 30, 2024
1 parent 11c6ce7 commit 7dc4817
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/components/src/CommentItem/CommentItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const CommentItem = (props: IProps) => {
<Flex sx={{ gap: 2, flexGrow: 1 }}>
<Box data-cy="commentAvatar" data-testid="commentAvatar">
<Avatar
data-cy="commentAvatarImage"
src={creatorImage ?? defaultProfileImage}
sx={{
objectFit: 'cover',
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/Username/Username.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const Username = ({ user, sx, isLink = true }: IProps) => {
countryCode={countryCode}
svg={true}
title={countryCode}
data-cy={`country:${countryCode}`}
/>
</Flex>
) : (
Expand Down
24 changes: 24 additions & 0 deletions packages/cypress/src/integration/howto/discussions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ describe('[Howto.Discussions]', () => {
cy.contains(`${howtoDiscussion.comments.length + 1} comments`)
cy.contains(newComment)

// The following step isn't possible to test atm due to the relationship between
// the functions listen for changes and how cypress creates new document collections
// for each test run.
// cy.step('Updating user settings shows on comments')
// cy.visit('/settings')
// cy.setSettingBasicUserInfo({
// country: 'Saint Lucia',
// description: "I'm a commenter",
// displayName: visitor.username,
// })
// cy.setSettingImage('avatar', 'userImage')
// cy.setSettingAddContactLink({
// index: 0,
// label: ExternalLinkLabel.SOCIAL_MEDIA,
// url: 'http://something.to.delete/',
// })
// cy.saveSettingsForm()

// cy.visit(`/how-to/${item.slug}`)
// cy.get('[data-cy="country:lc"]')
// cy.get('[data-cy="commentAvatarImage"]')
// .should('have.attr', 'src')
// .and('include', 'avatar')

cy.step('Can edit their comment')
cy.editDiscussionItem('CommentItem', updatedNewComment)
cy.contains(updatedNewComment)
Expand Down
5 changes: 4 additions & 1 deletion packages/cypress/src/integration/settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('[Settings]', () => {
})

it('[Edit a new profile]', () => {
const country = 'Brazil'
const country = 'Bolivia'
const userImage = 'avatar'
const displayName = 'settings_member_new'
const description = "I'm a very active member"
Expand Down Expand Up @@ -78,6 +78,7 @@ describe('[Settings]', () => {
country,
description,
})
cy.get('[data-cy="country:BO"]')

cy.step('Errors if trying to upload invalid image')
cy.get(`[data-cy=userImage]`)
Expand Down Expand Up @@ -126,6 +127,8 @@ describe('[Settings]', () => {
cy.contains(user.username)
cy.contains(displayName)
cy.contains(description)
cy.contains(country)
cy.get('[data-cy="country:bo"]')
cy.get(`[data-cy="MemberBadge-${profileType}"]`)
cy.get('[data-cy="profile-avatar"]')
.should('have.attr', 'src')
Expand Down
6 changes: 3 additions & 3 deletions shared/mocks/data/discussions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export const discussions = {
_creatorId: 'demo_admin',
_edited: '2022-03-27T22:10:12.271Z',
_id: 'm324vysdq71',
creatorCountry: 'uk',
creatorCountry: 'bo',
creatorName: 'demo_admin',
parentCommentId: null,
text: "Thanks for this how-to, it's taught me loads.",
Expand All @@ -207,7 +207,7 @@ export const discussions = {
_created: '2022-03-29T22:10:12.271Z',
_creatorId: 'benfurber',
_id: 'sgdfjk67123dx',
creatorCountry: 'uk',
creatorCountry: 'gb',
creatorName: 'ben',
parentCommentId: 'm324vysdq71',
text: 'Same!',
Expand All @@ -216,7 +216,7 @@ export const discussions = {
_created: '2022-03-30T22:10:12.271Z',
_creatorId: 'benfurber',
_id: 'jk67123dx',
creatorCountry: 'uk',
creatorCountry: 'gb',
creatorName: 'ben',
parentCommentId: null,
text: 'The third step confused me a bit...',
Expand Down
26 changes: 24 additions & 2 deletions src/pages/UserSettings/content/sections/UserInfos.section.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Field } from 'react-final-form'
import { FieldArray } from 'react-final-form-arrays'
import countriesList from 'countries-list'
import { Button, FieldInput, FieldTextarea, InternalLink } from 'oa-components'
import {
Button,
FieldInput,
FieldTextarea,
InternalLink,
Username,
} from 'oa-components'
import { ProfileTypeList } from 'oa-shared'
import { SelectField } from 'src/common/Form/Select.field'
import { isModuleSupported, MODULE } from 'src/modules'
Expand All @@ -28,6 +34,9 @@ export const UserInfosSection = ({ formValues }: IProps) => {
const isMemberProfile = profileType === ProfileTypeList.MEMBER
const { about, country, displayName, userName } = fields

const countryCode = Object.keys(countries).find(
(key) => countries[key].name === formValues.location?.country,
)
const noMapPin = !location?.latlng

return (
Expand Down Expand Up @@ -103,14 +112,27 @@ export const UserInfosSection = ({ formValues }: IProps) => {
{(field) => (
<SelectField
options={Object.keys(countries).map((country) => ({
label: countries[country].name,
label: `${countries[country].emoji} ${countries[country].native}`,
value: countries[country].name,
}))}
placeholder="Select your country..."
{...field}
/>
)}
</Field>
<Flex sx={{ gap: 1, alignItems: 'center' }}>
<Text sx={{ fontSize: 1 }} variant="quiet">
Preview:
</Text>
<Username
user={{
userName: formValues.userName || '',
countryCode,
isSupporter: false,
isVerified: false,
}}
/>
</Flex>
</Flex>
)}

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 @@ -8,6 +8,8 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'

import { UserStore } from './user.store'

import type { ILocation } from 'src/models'

Check failure on line 11 in src/stores/User/user.store.test.tsx

View workflow job for this annotation

GitHub Actions / build_and_preview

Module '"src/models"' has no exported member 'ILocation'.

vi.mock('../common/module.store')
vi.mock('../Aggragations/aggregations.store')

Expand Down Expand Up @@ -427,6 +429,31 @@ describe('userStore', () => {
}),
)
})

it('updates the location country code along with the country name', async () => {
const userProfile = FactoryUser()
store.activeUser = userProfile

const updateValues = {
_id: userProfile._id,
location: {
country: 'Nigeria',
} as ILocation,
}

await store.updateUserProfile(updateValues, 'test-b')

// Assert
expect(store.db.update).toHaveBeenCalledWith(
expect.objectContaining({
_id: updateValues._id,
location: {
country: 'Nigeria',
countryCode: 'NG',
},
}),
)
})
})

describe('updateUserNotificationSettings', () => {
Expand Down
13 changes: 12 additions & 1 deletion src/stores/User/user.store.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { countries } from 'countries-list'
import {
createUserWithEmailAndPassword,
onAuthStateChanged,
Expand Down Expand Up @@ -192,7 +193,7 @@ export class UserStore extends ModuleStore {
}

public async updateUserProfile(values: PartialUser, trigger: string) {
const { _id, coverImages, userImage } = values
const { _id, coverImages, location, userImage } = values

if (!_id) throw new Error(`User not found`)
if (_id !== this.activeUser?._id) {
Expand All @@ -218,6 +219,16 @@ export class UserStore extends ModuleStore {
)
}

if (location?.country) {
const countryCode = Object.keys(countries).find(
(key) => countries[key].name === location.country,
)
updatedUserProfile.location = {
...location,
countryCode: countryCode || '',
}
}

await this._updateUserRequest(_id, updatedUserProfile)
await this.refreshActiveUserDetails()
return this.activeUser
Expand Down

0 comments on commit 7dc4817

Please sign in to comment.