Skip to content

Commit 1376b73

Browse files
committed
fix: Allow multiple updates to profile
https://harperdb.atlassian.net/browse/STUDIO-489
1 parent 3534d8a commit 1376b73

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

src/features/profile/index.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import { FormLabel } from '@/components/ui/form/FormLabel';
77
import { FormMessage } from '@/components/ui/form/FormMessage';
88
import { Input } from '@/components/ui/input';
99
import { logoutOnSuccess } from '@/features/auth/handlers/logoutOnSuccess';
10-
import { getCurrentUser } from '@/features/auth/queries/getCurrentUser';
1110
import { useUpdateUserMutation } from '@/features/profile/mutations/updateUserMutation';
1211
import { UpdateUserSchema } from '@/features/profile/mutations/updateUserSchema';
1312
import { useCloudAuth } from '@/hooks/useAuth';
13+
import { authStore, OverallAppSignIn } from '@/lib/authStore';
1414
import { zodResolver } from '@hookform/resolvers/zod';
1515
import { useNavigate, useRouter } from '@tanstack/react-router';
1616
import { Save } from 'lucide-react';
@@ -23,17 +23,15 @@ export function ProfileIndex() {
2323
const router = useRouter();
2424
const navigate = useNavigate();
2525
const { user } = useCloudAuth();
26+
2627
const form = useForm({
2728
resolver: zodResolver(UpdateUserSchema),
28-
defaultValues: async () => {
29-
const user = await getCurrentUser();
30-
return {
31-
confirmNewPassword: '',
32-
firstname: user.firstname,
33-
id: user.id,
34-
lastname: user.lastname,
35-
newPassword: '',
36-
};
29+
defaultValues: {
30+
confirmNewPassword: '',
31+
firstname: user?.firstname || '',
32+
id: user?.id || '',
33+
lastname: user?.lastname || '',
34+
newPassword: '',
3735
},
3836
});
3937
const { mutate: updateUser, isPending: isUpdatePending } = useUpdateUserMutation();
@@ -43,7 +41,11 @@ export function ProfileIndex() {
4341
if (formData) {
4442
updateUser(formData, {
4543
onSuccess: (data) => {
46-
form.reset(data);
44+
form.reset({
45+
...form.formState.defaultValues,
46+
...data,
47+
});
48+
authStore.updateUserForEntity(OverallAppSignIn, data);
4749
if (formData.newPassword) {
4850
toast.success('Profile updated successfully!', {
4951
description: 'Please sign in with your new password.',
@@ -158,7 +160,6 @@ export function ProfileIndex() {
158160
)}
159161
/>
160162

161-
162163
<div className="flex justify-between w-full">
163164
<Button
164165
type="submit"

src/features/profile/mutations/updateUserMutation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { apiClient } from '@/config/apiClient';
22
import { UpdateUserSchema } from '@/features/profile/mutations/updateUserSchema';
33
import { SchemaUser } from '@/lib/api.gen';
4+
import { User } from '@/lib/api.patch';
45
import { useMutation } from '@tanstack/react-query';
56
import z from 'zod';
67

@@ -13,7 +14,7 @@ async function onUpdateUser(formData: z.infer<typeof UpdateUserSchema>) {
1314
userData.password = newPassword;
1415
}
1516
const { data } = await apiClient.patch(`/User/${id}` as '/User/{id}', userData);
16-
return data;
17+
return data as Partial<User>;
1718
}
1819

1920
export function useUpdateUserMutation() {

src/lib/authStore.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ class AuthStore {
120120
this.updateConnectionIfChanged(id, false, user);
121121
}
122122

123+
public updateUserForEntity(entity: EntityTypes, changes: Partial<AuthenticatedConnection['user']>): void {
124+
const id = this.calculateIdFromEntity(entity);
125+
const key = this.calculateKeyFromEntity(entity);
126+
if (!id || !key) {
127+
return;
128+
}
129+
const connection = this.getConnectionById(id);
130+
if (!connection.user) {
131+
return;
132+
}
133+
this.updateConnectionIfChanged(id, false, {
134+
...connection.user,
135+
...changes,
136+
});
137+
}
138+
123139
public calculateIdFromEntity(entity: EntityTypes | EntityIds | undefined): EntityIds | undefined {
124140
if (isLocalStudio || entity === OverallAppSignIn) {
125141
return OverallAppSignIn;

0 commit comments

Comments
 (0)