Skip to content

Commit 8f04f5a

Browse files
committed
Refactor of the QuestionForm to make it more readable
1 parent eeec5a2 commit 8f04f5a

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

src/features/questions/QuestionForm.js

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useMemo, useState } from "react";
1+
import React, { useEffect, useState } from "react";
22
import { useDispatch, useSelector } from "react-redux";
33
import {
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+
2128
const 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

148146
export default QuestionForm;

src/features/questions/QuestionList.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ const QuestionList = ({ formRef = null }) => {
6969
);
7070
}
7171

72-
const tooltip =
73-
"You can find the created questions and their answers right here!";
7472
return (
7573
<section aria-label="Question list" role="list">
76-
<Title tooltip={tooltip}>Created questions</Title>
74+
<Title tooltip="You can find the created questions and their answers right here!">
75+
Created questions
76+
</Title>
7777
{questionListContent}
7878
{actionButtons}
7979
</section>

0 commit comments

Comments
 (0)