Skip to content

Commit

Permalink
fix: invitation page error display area
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Aug 1, 2024
1 parent 8bd9bd0 commit 5915357
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
13 changes: 10 additions & 3 deletions src/renderer/src/pages/(external)/invitation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
FormMessage,
} from "@renderer/components/ui/form"
import { Input } from "@renderer/components/ui/input"
import { getFetchErrorMessage } from "@renderer/lib/api-fetch"
import { useInvitationMutation } from "@renderer/queries/invitations"
import { useEffect } from "react"
import { useForm } from "react-hook-form"
Expand All @@ -25,7 +26,12 @@ export function Component() {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
})
const invitationMutation = useInvitationMutation()
const invitationMutation = useInvitationMutation({
onError(error) {
const message = getFetchErrorMessage(error)
form.setError("code", { message })
},
})

function onSubmit(values: z.infer<typeof formSchema>) {
invitationMutation.mutate(values.code)
Expand All @@ -46,7 +52,6 @@ export function Component() {
className="w-[512px] max-w-full space-y-8"
>
<FormField

control={form.control}
name="code"
render={({ field }) => (
Expand All @@ -55,7 +60,9 @@ export function Component() {
<FormControl>
<Input autoFocus {...field} />
</FormControl>
<FormMessage />
<div className="h-6">
<FormMessage />
</div>
</FormItem>
)}
/>
Expand Down
21 changes: 12 additions & 9 deletions src/renderer/src/queries/invitations.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { apiClient, getFetchErrorMessage } from "@renderer/lib/api-fetch"
import { apiClient } from "@renderer/lib/api-fetch"
import { useMutation } from "@tanstack/react-query"
import { toast } from "sonner"

export const useInvitationMutation = () => useMutation({
mutationFn: (code: string) => apiClient.invitations.use.$post({ json: { code } }),
onSuccess() {},
async onError(err) {
toast.error(await getFetchErrorMessage(err))
},
})
import type { MutationBaseProps } from "./types"

export const useInvitationMutation = ({ onError }: MutationBaseProps = {}) =>
useMutation({
mutationFn: (code: string) =>
apiClient.invitations.use.$post({ json: { code } }),

onError: (error) => {
onError?.(error)
},
})
6 changes: 6 additions & 0 deletions src/renderer/src/queries/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface MutationBaseProps {
onSuccess?: () => void
onError?: (error: Error) => void
}

export {}

0 comments on commit 5915357

Please sign in to comment.