Skip to content

Commit e24dadf

Browse files
committed
update user detail
1 parent 91e460f commit e24dadf

2 files changed

Lines changed: 79 additions & 15 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { toast } from "sonner";
2+
import { apiClient } from "@/lib/apiClient";
3+
import { useGetUserKey } from "./use-get-user.hook";
4+
import { toastConfig } from "@/components/ui/sonner";
5+
import { useMutation, useQueryClient } from "@tanstack/react-query";
6+
7+
export type UserParam = { _id: string; name: string };
8+
9+
const updateUser = (data: UserParam) =>
10+
apiClient<{ token: string }>("/api/v1/users/" + data?._id, {
11+
method: "PATCH",
12+
body: JSON.stringify(data),
13+
});
14+
15+
export const useUpdateUser = () => {
16+
const queryClient = useQueryClient()
17+
18+
const mutation = useMutation({
19+
mutationFn: async (data: UserParam) => {
20+
const promise = updateUser(data);
21+
toast.promise(promise, {
22+
...toastConfig,
23+
loading: "Updating...",
24+
success: "Update successful",
25+
error: (err: any) => err?.message || "Something went wrong",
26+
});
27+
const res = await promise;
28+
return res
29+
},
30+
onSuccess: (data) => {
31+
queryClient.invalidateQueries({
32+
queryKey: [useGetUserKey,],
33+
})
34+
},
35+
onError: (error) => {
36+
// toast.error(error?.message ? error?.message : error?.stack, {
37+
// position: toastConfig.position
38+
// })
39+
}
40+
});
41+
42+
return {
43+
...mutation,
44+
updateUser: mutation.mutateAsync,
45+
};
46+
};

‎frontend/src/page/user/me/me.page.tsx‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import { cn } from "@/lib/utils"
1616
import { ButtonLoader } from "@/components/ui/button-loader"
1717
import RecycleBinDialog from "@/feature/quote/dialog/recycle-bin.dialog"
1818
import NavigationComponent from "@/feature/user/component/navigation"
19+
import { useUpdateUser } from "@/feature/user/hook/use-update-user.hook"
20+
import { toast } from "sonner"
21+
import { toastConfig } from "@/components/ui/sonner"
1922

2023
const profileFormSchema = z.object({
2124
name: z.string().min(1, "name should not be empty"),
@@ -29,24 +32,28 @@ const Me = () => {
2932
const { decodedToken, isExpired } = useJwt<User>(token);
3033

3134
const { data: user, isPending, invalidateQueries } = useGetUser({ _id: decodedToken?._id })
35+
const { updateUser } = useUpdateUser()
3236

33-
const { control, reset, formState: { isDirty }, handleSubmit } = useForm({
37+
const { control, reset, formState: { isDirty }, handleSubmit, getValues } = useForm({
3438
defaultValues: {
3539
name: '',
3640
email: ''
3741
},
3842
resolver: zodResolver(profileFormSchema)
3943
})
4044

41-
useEffect(() => {
42-
reset({
43-
name: user?.name || "",
44-
email: user?.email || "",
45-
})
46-
}, [user])
4745

48-
const onSubmit = (values: z.infer<typeof profileFormSchema>) => {
46+
const onSubmit = async (values: z.infer<typeof profileFormSchema>) => {
47+
console.log({ values });
4948

49+
if (!user?._id) {
50+
toast.warning("No user found", toastConfig)
51+
return
52+
}
53+
await updateUser({
54+
_id: user?._id,
55+
name: values.name
56+
})
5057
}
5158

5259
const onLogout = () => {
@@ -57,6 +64,13 @@ const Me = () => {
5764
})
5865
}
5966

67+
useEffect(() => {
68+
reset({
69+
name: user?.name || "",
70+
email: user?.email || "",
71+
})
72+
}, [user])
73+
6074
return (
6175
<div
6276
className='w-full h-dvh flex justify-self-center md:w-3/4 overflow-hidden'>
@@ -70,7 +84,7 @@ const Me = () => {
7084
className="w-full "
7185
>
7286
<div className='flex p-4 bg-background flex-row sticky top-0 z-10 justify-between items-center'>
73-
<NavigationComponent/>
87+
<NavigationComponent />
7488
</div>
7589
{/* <Separator className="bg-border" /> */}
7690

@@ -96,18 +110,21 @@ const Me = () => {
96110
>
97111
Logout
98112
</Button>
99-
{/* {
113+
{
100114
(isDirty) && (
101115
<Button
102116
variant="outline"
103-
size={'sm'}
117+
onClick={() => {
118+
onSubmit(getValues())
119+
}
120+
} size={'sm'}
104121
className="text-xs px-2 py-0"
105122
aria-label='save-profile-button'
106123
title='save-profile-button'>
107124
Save changes
108125
</Button>
109126
)
110-
} */}
127+
}
111128
</div>
112129

113130
</div>
@@ -171,13 +188,14 @@ const Me = () => {
171188
</p>
172189
<div className="flex justify-between items-center text-center ">
173190
<p className=" text-sm">
174-
Sync
191+
Sync
175192
</p>
176-
{/* <Button variant="outline" size="icon"
193+
<Button variant="outline" size="icon"
194+
disabled
177195
aria-label='sync-button'
178196
title='sync-button'>
179197
<CloudBackupIcon />
180-
</Button> */}
198+
</Button>
181199
</div>
182200
<div className="flex justify-between items-center text-sm ">
183201
Cloud Container

0 commit comments

Comments
 (0)