Skip to content

Commit af7c13d

Browse files
authored
fix: adjust update current user and fix mismatching formats (AppFlowy-IO#65)
* chore: expose method to update indexed db * fix: revert translation
1 parent 1b10ed9 commit af7c13d

File tree

5 files changed

+70
-25
lines changed

5 files changed

+70
-25
lines changed

cypress/e2e/account/update-user-profile.cy.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,21 @@ describe('Update User Profile', () => {
5050
cy.log('Step 5: Verifying Account Settings dialog opened');
5151
cy.get('[data-testid="account-settings-dialog"]', { timeout: 10000 }).should('be.visible');
5252

53-
// Check initial date format (should be Local/default)
53+
// Check initial date format (should be Month/Day/Year)
5454
cy.log('Step 6: Checking initial date format');
5555
cy.get('[data-testid="date-format-dropdown"]').should('be.visible');
5656

57-
// Test Date Format change - select US format (Month/Day/Year)
58-
cy.log('Step 7: Testing Date Format change to US format');
57+
// Test Date Format change - select Year/Month/Day
58+
cy.log('Step 7: Testing Date Format change to Year/Month/Day');
5959
cy.get('[data-testid="date-format-dropdown"]').click();
6060
cy.wait(500);
6161

62-
// Select US format (value 1) which is Month/Day/Year
62+
// Select US format (value 1) which is Year/Month/Day
6363
cy.get('[data-testid="date-format-1"]').should('be.visible').click();
6464
cy.wait(3000); // Wait for API call to complete
6565

66-
// Verify the dropdown now shows US format
67-
cy.get('[data-testid="date-format-dropdown"]').should('contain.text', 'Month/Day/Year');
66+
// Verify the dropdown now shows Year/Month/Day
67+
cy.get('[data-testid="date-format-dropdown"]').should('contain.text', 'Year/Month/Day');
6868

6969
// Test Time Format change
7070
cy.log('Step 8: Testing Time Format change');
@@ -93,7 +93,7 @@ describe('Update User Profile', () => {
9393
cy.log('Step 10: Verifying all settings are showing correctly');
9494

9595
// Verify all dropdowns still show the selected values
96-
cy.get('[data-testid="date-format-dropdown"]').should('contain.text', 'Month/Day/Year');
96+
cy.get('[data-testid="date-format-dropdown"]').should('contain.text', 'Year/Month/Day');
9797
cy.get('[data-testid="time-format-dropdown"]').should('contain.text', '24');
9898
cy.get('[data-testid="start-week-on-dropdown"]').should('contain.text', 'Monday');
9999

src/@types/translations/en.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,8 +1686,8 @@
16861686
"isRange": "End date",
16871687
"dateFormatFriendly": "Month Day, Year",
16881688
"dateFormatISO": "Year-Month-Day",
1689-
"dateFormatLocal": "Local",
1690-
"dateFormatUS": "Month/Day/Year",
1689+
"dateFormatLocal": "Month/Day/Year",
1690+
"dateFormatUS": "Year/Month/Day",
16911691
"dateFormatDayMonthYear": "Day/Month/Year",
16921692
"timeFormat": "Time format",
16931693
"invalidTimeFormat": "Invalid format",

src/components/app/workspaces/AccountSettings.tsx

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useTranslation } from 'react-i18next';
66
import { DateFormat, TimeFormat } from '@/application/types';
77
import { MetadataKey } from '@/application/user-metadata';
88
import { ReactComponent as ChevronDownIcon } from '@/assets/icons/alt_arrow_down.svg';
9-
import { useCurrentUser, useService } from '@/components/main/app.hooks';
9+
import { useAppConfig } from '@/components/main/app.hooks';
1010
import { Dialog, DialogContent, DialogDescription, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
1111
import {
1212
DropdownMenu,
@@ -19,8 +19,7 @@ import { cn } from '@/lib/utils';
1919

2020
export function AccountSettings({ children }: { children?: React.ReactNode }) {
2121
const { t } = useTranslation();
22-
const currentUser = useCurrentUser();
23-
const service = useService();
22+
const { currentUser, updateCurrentUser, service } = useAppConfig();
2423
const [open, setIsOpen] = useState(false);
2524

2625
const [dateFormat, setDateFormat] = useState(
@@ -50,35 +49,40 @@ export function AccountSettings({ children }: { children?: React.ReactNode }) {
5049
const handleSelectDateFormat = useCallback(
5150
async (dateFormat: number) => {
5251
setDateFormat(dateFormat);
53-
if (!currentUser?.metadata) return;
5452

55-
metadataUpdateRef.current = { ...currentUser.metadata, [MetadataKey.DateFormat]: dateFormat };
53+
metadataUpdateRef.current = {
54+
...metadataUpdateRef?.current,
55+
[MetadataKey.DateFormat]: dateFormat,
56+
};
5657
await debounceUpdateProfile();
5758
},
58-
[currentUser, debounceUpdateProfile]
59+
[debounceUpdateProfile]
5960
);
6061

6162
const handleSelectTimeFormat = useCallback(
6263
async (timeFormat: number) => {
6364
setTimeFormat(timeFormat);
64-
if (!currentUser?.metadata) return;
6565

66-
metadataUpdateRef.current = { ...currentUser.metadata, [MetadataKey.TimeFormat]: timeFormat };
66+
metadataUpdateRef.current = {
67+
...metadataUpdateRef?.current,
68+
[MetadataKey.TimeFormat]: timeFormat,
69+
};
6770
await debounceUpdateProfile();
6871
},
69-
[currentUser, debounceUpdateProfile]
72+
[debounceUpdateProfile]
7073
);
7174

7275
const handleSelectStartWeekOn = useCallback(
7376
async (startWeekOn: number) => {
7477
setStartWeekOn(startWeekOn);
7578

76-
if (!currentUser?.metadata) return;
77-
78-
metadataUpdateRef.current = { ...currentUser.metadata, [MetadataKey.StartWeekOn]: startWeekOn };
79+
metadataUpdateRef.current = {
80+
...metadataUpdateRef?.current,
81+
[MetadataKey.StartWeekOn]: startWeekOn,
82+
};
7983
await debounceUpdateProfile();
8084
},
81-
[currentUser, debounceUpdateProfile]
85+
[debounceUpdateProfile]
8286
);
8387

8488
const onOpenChange = useCallback(
@@ -89,13 +93,22 @@ export function AccountSettings({ children }: { children?: React.ReactNode }) {
8993
setDateFormat(Number(user?.metadata?.[MetadataKey.DateFormat] as DateFormat) || DateFormat.Local);
9094
setTimeFormat(Number(user?.metadata?.[MetadataKey.TimeFormat] as TimeFormat) || TimeFormat.TwelveHour);
9195
setStartWeekOn(Number(user?.metadata?.[MetadataKey.StartWeekOn]) || 0);
96+
97+
metadataUpdateRef.current = {
98+
...user?.metadata,
99+
};
92100
} else {
93-
void service?.getCurrentUser();
101+
if (currentUser) {
102+
void updateCurrentUser({
103+
...currentUser,
104+
metadata: metadataUpdateRef.current || currentUser.metadata,
105+
});
106+
}
94107
}
95108

96109
setIsOpen(isOpen);
97110
},
98-
[service]
111+
[currentUser, service, updateCurrentUser]
99112
);
100113

101114
if (!currentUser || !service) {

src/components/main/AppConfig.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getService } from '@/application/services';
77
import { AFServiceConfig } from '@/application/services/services.type';
88
import { EventType, on } from '@/application/session';
99
import { getTokenParsed, isTokenValid } from '@/application/session/token';
10+
import { User } from '@/application/types';
1011
import { MetadataKey } from '@/application/user-metadata';
1112
import { createInitialTimezone, UserTimezone } from '@/application/user-timezone.types';
1213
import { InfoSnackbarProps } from '@/components/_shared/notify';
@@ -33,6 +34,19 @@ function AppConfig({ children }: { children: React.ReactNode }) {
3334
const [loginOpen, setLoginOpen] = React.useState(false);
3435
const [loginCompletedRedirectTo, setLoginCompletedRedirectTo] = React.useState<string>('');
3536

37+
const updateCurrentUser = useCallback(
38+
async (user: User) => {
39+
if (!service || !userId) return;
40+
41+
try {
42+
await db.users.put(user, user.uuid);
43+
} catch (e) {
44+
console.error(e);
45+
}
46+
},
47+
[service, userId]
48+
);
49+
3650
const openLoginModal = useCallback((redirectTo?: string) => {
3751
setLoginOpen(true);
3852
setLoginCompletedRedirectTo(redirectTo || window.location.href);
@@ -176,6 +190,7 @@ function AppConfig({ children }: { children: React.ReactNode }) {
176190
service,
177191
isAuthenticated,
178192
currentUser,
193+
updateCurrentUser,
179194
openLoginModal,
180195
}}
181196
>

src/components/main/app.hooks.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,28 @@ export const AFConfigContext = createContext<
1919
service: AFService | undefined;
2020
isAuthenticated: boolean;
2121
currentUser?: User;
22+
updateCurrentUser: (user: User) => Promise<void>;
2223
openLoginModal: (redirectTo?: string) => void;
2324
}
2425
| undefined
2526
>(undefined);
2627

28+
export function useAppConfig() {
29+
const context = useContext(AFConfigContext);
30+
31+
if (!context) {
32+
throw new Error('useAppConfig must be used within a AFConfigContext');
33+
}
34+
35+
return {
36+
service: context.service,
37+
isAuthenticated: context.isAuthenticated,
38+
currentUser: context.currentUser,
39+
updateCurrentUser: context.updateCurrentUser,
40+
openLoginModal: context.openLoginModal,
41+
};
42+
}
43+
2744
export function useCurrentUser() {
2845
const context = useContext(AFConfigContext);
2946

@@ -42,4 +59,4 @@ export function useService() {
4259
}
4360

4461
return context.service;
45-
}
62+
}

0 commit comments

Comments
 (0)