Last active
November 16, 2021 18:43
-
-
Save drodil/476eb3a63d92cf97f1540f22dd3e763d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import { useForm, SubmitHandler } from "react-hook-form"; | |
import { usePutFormMutation } from "../formApi"; | |
import { useDispatch, useSelector } from 'react-redux' | |
type Inputs = { | |
name: string, | |
}; | |
export default function FormPageTwo() { | |
const dispatch = useDispatch(); | |
const formData = useSelector(getFormData); | |
const { register, handleSubmit, formState: { errors } } = useForm<Inputs>(); | |
const [ | |
sendForm, | |
{ data, isLoading: isUpdating, isError, isSuccess }, | |
] = usePutFormMutation() | |
const onSubmit: SubmitHandler<Inputs> = (data) => { | |
/* Save this page data to redux. There's already some data to be sent in the state. */ | |
dispatch(setFormData(data)); | |
/* And send it... Or not. The state is not updated synchronously with dispatch | |
so this would only send data that is already saved in the formData object | |
*/ | |
sendForm(formData); | |
}; | |
useEffect(() => { | |
if(isSuccess) { | |
/* For some reason this does not trigger.. */ | |
console.log(data); | |
} | |
}, [isSuccess, data]) | |
if(isUpdating) { | |
return (<div>Wait a minute..</div>); | |
} | |
return ( | |
<form onSubmit={handleSubmit(onSubmit)}> | |
{isError && (<div>Error, try again!</div>)} | |
<input defaultValue="eminem" {...register("name")} /> | |
<input type="submit" /> | |
</form> | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useEffect} from "react"; | |
import { useForm, SubmitHandler } from "react-hook-form"; | |
import { usePutFormMutation, getFormData, getReadyToSend } from "../formApi"; | |
import { useDispatch, useSelector } from 'react-redux' | |
type Inputs = { | |
name: string, | |
}; | |
export default function FormPageTwo() { | |
const dispatch = useDispatch(); | |
const { register, handleSubmit, formState: { errors } } = useForm<Inputs>(); | |
const formData = useSelector(getFormData); | |
const readyToSend = useSelector(getReadyToSend); | |
const [ | |
sendForm, | |
{ data, isLoading: isUpdating, isError, isSuccess }, | |
] = usePutFormMutation() | |
useEffect(() => { | |
/* Check that we are ready to send the data */ | |
if(readyToSend) { | |
sendForm(formData); | |
} | |
}, [formData, readyToSend]); | |
useEffect(() => { | |
if(isSuccess) { | |
/* For some reason this does not trigger.. */ | |
console.log(data); | |
} | |
}, [isSuccess, data]) | |
const onSubmit: SubmitHandler<Inputs> = (data) => { | |
/* Save page data to redux. There's already some data to be sent in the state. */ | |
dispatch(setFormData(data)); | |
/* Also need to set flag that we are ready with the forms */ | |
dispatch(setReadyToSend(true)); | |
}; | |
if(isUpdating) { | |
return (<div>Wait a minute..</div>); | |
} | |
return ( | |
<form onSubmit={handleSubmit(onSubmit)}> | |
{isError && (<div>Error, try again!</div>)} | |
<input defaultValue="eminem" {...register("name")} /> | |
<input type="submit" /> | |
</form> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment