You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, there's a reliance on useAction to solely retrieve the status of an action. This approach leads to a situation where the toast is displayed prematurely, before the action is completed, necessitating the need to await the action.
Is there an alternative method to obtain the action status without resorting to useAction?
I attempted to use action.result.data.failure, but encountered the issue of the promise still being in progress.
Any insights or suggestions on a more efficient approach would be greatly appreciated.
'use client'...import{useForm}from'react-hook-form'import{useAction}from'next-safe-action/hooks'import{loginUser}from'@/app/(auth)/login/actions'import{FormSchema,schema}from'@/app/(auth)/login/schema'
...
exportdefaultfunctionLoginForm(){const{ toast }=useToast()constaction=useAction(loginUser)// Avoid doing this, I just use this to the SubmitButton load indicatorconstform=useForm<FormSchema>({resolver: zodResolver(schema),defaultValues: {email: '',password: '',},})constonSubmit=form.handleSubmit(async(data)=>{constresult=awaitloginUser(data)// It was easir to use action.result.data.failure// I can be something wrongif(result?.data?.failure){toast({title: 'Failed to login',description: result.data?.failure,variant: 'destructive',})return}toast({title: 'Successfully logged in!',})})return(<Form{...form}><formonSubmit={onSubmit}><divclassName="grid gap-4"><FormFieldcontrol={form.control}name="email"render={({ field })=>(<FormItem><FormLabel>Email</FormLabel><FormControl><Inputplaceholder="m@example.com"{...field}/></FormControl><FormMessage/></FormItem>)}/><FormFieldcontrol={form.control}name="password"render={({ field })=>(<FormItem><divclassName="flex items-center"><FormLabel>Password</FormLabel><Linkhref="#"className="ml-auto inline-block text-sm underline">
Forgot your password?
</Link></div><FormControl><Inputtype="password"placeholder="••••••••"{...field}/></FormControl><FormMessage/></FormItem>)}/><SubmitButton{...action}>Login</SubmitButton></div></form></Form>)}// SubmitButton...import{HookActionStatus}from'next-safe-action/hooks'import{isExecuting}from'next-safe-action/status'
...
interfaceSubmitButtonProps{status: HookActionStatuschildren: ReactNode}exportdefaultfunctionSubmitButton({ status, children }: SubmitButtonProps){letisPending=isExecuting(status)constrenderPending=()=>{return(<><ReloadIconclassName="mr-2 h-4 w-4 animate-spin"/>
Please wait
</>)}return(<Buttontype="submit"disabled={isPending}className="w-full">{isPending ? renderPending() : children}</Button>)}// Action'use server'import{redirect}from'next/navigation'import{action}from'@/lib/safe-action'import{schema}from'./schema'exportconstloginUser=action(schema,async({ email, password })=>{// ...const{ error }=awaitdatabaseThing()if(error!==null){console.log('error',error.message)return{failure: error.message}}redirect('/')})
Thanks in advance for your assistance. It's possible that I might be overlooking something, so any additional guidance or insights would be greatly appreciated.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Currently, there's a reliance on
useAction
to solely retrieve the status of an action. This approach leads to a situation where the toast is displayed prematurely, before the action is completed, necessitating the need to await the action.Is there an alternative method to obtain the action status without resorting to
useAction
?I attempted to use action.result.data.failure, but encountered the issue of the promise still being in progress.
Any insights or suggestions on a more efficient approach would be greatly appreciated.
Thanks in advance for your assistance. It's possible that I might be overlooking something, so any additional guidance or insights would be greatly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions