1- import React , { useEffect , useMemo , useState } from "react" ;
1+ import React , { useEffect , useState } from "react" ;
22import { useDispatch , useSelector } from "react-redux" ;
33import {
44 fetchStates ,
@@ -18,16 +18,14 @@ const statusMessages = {
1818 [ fetchStates . failed ] : " Failed to save, please try again." ,
1919} ;
2020
21+ const initialFormValues = {
22+ id : null ,
23+ question : "" ,
24+ answer : "" ,
25+ creationDate : null ,
26+ } ;
27+
2128const QuestionForm = ( { questionId, formRef = null } ) => {
22- const initialFormValues = useMemo (
23- ( ) => ( {
24- id : null ,
25- question : "" ,
26- answer : "" ,
27- creationDate : null ,
28- } ) ,
29- [ ]
30- ) ;
3129 const history = useHistory ( ) ;
3230 const dispatch = useDispatch ( ) ;
3331 const question =
@@ -38,62 +36,45 @@ const QuestionForm = ({ questionId, formRef = null }) => {
3836 const [ saveQuestionStatus , setSaveQuestionStatus ] = useState (
3937 fetchStates . initial
4038 ) ;
41- const [ fetchPromise , setFetchPromise ] = useState ( null ) ;
42- useEffect ( ( ) => ( ) => fetchPromise ?. cancel ?. ( ) , [ fetchPromise ] ) ;
39+ const [ cancelFetch , setFetchCancel ] = useState ( null ) ;
40+ useEffect ( ( ) => ( ) => cancelFetch ?. ( ) , [ cancelFetch ] ) ;
4341 useEffect ( ( ) => {
4442 if ( questionId && ! question . id ) {
4543 history . push ( "/" ) ;
4644 }
4745 setQuestionFormData ( question ) ;
4846 } , [ question , questionId , history ] ) ;
4947
50- const canSubmit = questionFormData . answer && questionFormData . question ;
51-
5248 const handleChange = ( e ) => {
5349 setQuestionFormData ( {
5450 ...questionFormData ,
5551 [ e . target . name ] : e . target . value ,
5652 } ) ;
5753 } ;
5854
55+ const canSubmit = questionFormData . answer && questionFormData . question ;
5956 const handleSubmit = async ( e ) => {
6057 e . preventDefault ( ) ;
6158 if ( canSubmit ) {
62- if ( ! saveDelay ) {
63- dispatch ( quickAddQuestion ( questionFormData ) ) ;
64- } else {
65- try {
66- setSaveQuestionStatus ( fetchStates . loading ) ;
67- let cancelable = makeCancelable ( dispatch ( saveQuestion ( questionFormData ) ) ) ;
68- setFetchPromise ( cancelable ) ;
69-
70- const resultAction = await cancelable . promise ;
71- setFetchPromise ( null ) ;
72- unwrapResult ( resultAction ) ;
73- setSaveQuestionStatus ( fetchStates . initial ) ;
74- } catch ( err ) {
75- if ( ! err . isCanceled ) {
76- setSaveQuestionStatus ( fetchStates . failed ) ;
77- console . error ( "Failed to save the post: " , err . message ) ;
78- } else {
79- console . info ( "Form unmounted by the time the question was saved." ) ;
80- }
81- return ;
59+ try {
60+ await submitForm ( ) ;
61+ setQuestionFormData ( initialFormValues ) ;
62+ setImmediate ( ( ) => history . push ( "/" ) ) ;
63+ } catch ( err ) {
64+ if ( err . isCanceled ) {
65+ console . info ( "Form unmounted by the time the question was saved." ) ;
66+ } else {
67+ setSaveQuestionStatus ( fetchStates . failed ) ;
68+ console . error ( "Failed to save the post: " , err . message ) ;
8269 }
8370 }
84- setQuestionFormData ( initialFormValues ) ;
85- setImmediate ( ( ) => history . push ( "/" ) ) ;
8671 }
8772 } ;
8873
8974 const isEdit = Boolean ( questionFormData . id ) ;
9075 return (
9176 < section >
92- < Title
93- tooltip = {
94- "Create your shiny new questions and answer using this form below!"
95- }
96- >
77+ < Title tooltip = "Create your shiny new questions and answer using this form below!" >
9778 { isEdit ? "Edit" : "Create a new" } question
9879 </ Title >
9980 < form aria-label = "Question form" >
@@ -143,6 +124,23 @@ const QuestionForm = ({ questionId, formRef = null }) => {
143124 </ form >
144125 </ section >
145126 ) ;
127+
128+ async function submitForm ( ) {
129+ if ( ! saveDelay ) {
130+ return dispatch ( quickAddQuestion ( questionFormData ) ) ;
131+ }
132+
133+ setSaveQuestionStatus ( fetchStates . loading ) ;
134+ let cancelablePromise = makeCancelable (
135+ dispatch ( saveQuestion ( questionFormData ) )
136+ ) ;
137+ setFetchCancel ( ( ) => cancelablePromise . cancel ) ;
138+
139+ const resultAction = await cancelablePromise . promise ;
140+ setFetchCancel ( null ) ;
141+ unwrapResult ( resultAction ) ;
142+ setSaveQuestionStatus ( fetchStates . initial ) ;
143+ }
146144} ;
147145
148146export default QuestionForm ;
0 commit comments