Skip to content

Commit 32f6ef4

Browse files
committed
fix: move update account to orpc
1 parent 3f1a5ac commit 32f6ef4

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

app/features/account/change-name-drawer.tsx

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useForm } from 'react-hook-form';
66
import { toast } from 'sonner';
77

88
import { authClient } from '@/lib/auth/client';
9+
import { orpc } from '@/lib/orpc/client';
910

1011
import {
1112
Form,
@@ -41,26 +42,23 @@ export const ChangeNameDrawer = (props: { children: ReactNode }) => {
4142
},
4243
});
4344

44-
const updateUser = useMutation({
45-
mutationFn: async (variables: { name: string }) => {
46-
await authClient.updateUser({
47-
name: variables.name,
48-
});
49-
await session.refetch();
50-
},
51-
onSuccess: () => {
52-
toast.success('Name updated');
53-
form.reset();
54-
router.navigate({
55-
replace: true,
56-
to: '.',
57-
search: {
58-
state: '',
59-
},
60-
});
61-
},
62-
onError: () => toast.error('Failed to update your name'),
63-
});
45+
const updateUser = useMutation(
46+
orpc.account.updateInfo.mutationOptions({
47+
onSuccess: async () => {
48+
await session.refetch();
49+
toast.success('Name updated');
50+
form.reset();
51+
router.navigate({
52+
replace: true,
53+
to: '.',
54+
search: {
55+
state: '',
56+
},
57+
});
58+
},
59+
onError: () => toast.error('Failed to update your name'),
60+
})
61+
);
6462

6563
return (
6664
<ResponsiveDrawer

app/server/routers/account.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { z } from 'zod';
22

33
import { zFormFieldsOnboarding } from '@/features/auth/schema';
44
import { protectedProcedure } from '@/server/orpc';
5+
import { zUser } from '@/features/user/schema';
56

67
const tags = ['account'];
78

@@ -26,4 +27,28 @@ export default {
2627
},
2728
});
2829
}),
30+
31+
updateInfo: protectedProcedure({
32+
permission: null,
33+
})
34+
.route({
35+
method: 'POST',
36+
path: '/account/info',
37+
tags,
38+
})
39+
.input(
40+
zUser().pick({
41+
name: true,
42+
})
43+
)
44+
.output(z.void())
45+
.handler(async ({ context, input }) => {
46+
context.logger.info('Update user');
47+
await context.db.user.update({
48+
where: { id: context.user.id },
49+
data: {
50+
name: input.name ?? '',
51+
},
52+
});
53+
}),
2954
};

0 commit comments

Comments
 (0)