Skip to content

Commit

Permalink
fix: user alias form improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
matejfalat committed May 3, 2024
1 parent c929e74 commit 5e1d4b4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/components/sections/profile/SetUserAliasForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import {Button} from '@/components/ui/Button'
import {useSetUserAlias} from '@/lib/queries/user/setUserAlias'
import {FormError} from '@/lib/types/error'
import {toast} from '@/components/ui/Toast'

const formSchema = z.object({
alias: z.string().min(3),
Expand All @@ -41,6 +42,7 @@ const SetUserAliasForm = ({initialAlias}: SetUserAliasFormProps) => {
getValues,
control,
formState: {isDirty},
reset,
} = form

const onSubmit = () => {
Expand All @@ -51,6 +53,10 @@ const SetUserAliasForm = ({initialAlias}: SetUserAliasFormProps) => {
form.setError('alias', {type: 'custom', message: error.message})
}
},
onSuccess: () => {
reset(getValues(), {keepValues: true})
toast({title: 'Alias set.'})
},
})
}

Expand Down
4 changes: 3 additions & 1 deletion src/lib/queries/user/getUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {withApiErrorHandler} from '@/lib/utils/common/error'

const getQueryOptions = (userId: string | undefined) => ({
queryKey: queryKeys.users.detail(userId).queryKey,
queryFn: withApiErrorHandler(getUser),
// getUser has to be explicitly called without arguments, otherwise React Query
// would inject context, which is not serializable for server actions
queryFn: withApiErrorHandler(() => getUser()),
})

export const useGetUser = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/server/actions/user/setUserAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const setUserAliasAction = async (alias: string | null) => {
)

if (doesAliasExists) {
throw new FormError('Alias already exists')
throw new FormError('Alias already exists.')
}

return db.update(users).set({alias}).where(eq(users.id, id)).returning()
Expand Down

0 comments on commit 5e1d4b4

Please sign in to comment.