-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed the ability to save answer of question when type is Open Answer #3148
base: develop
Are you sure you want to change the base?
Changes from 8 commits
f968d55
12b3b1a
6213e01
2be9d12
13ca7e8
8b96ff0
cec960d
f486d55
6c90fa4
946f673
eb8c585
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,4 @@ | ||
import { | ||
useCallback, | ||
FC, | ||
useState, | ||
useEffect, | ||
Dispatch, | ||
SetStateAction | ||
} from 'react' | ||
import React from 'react' | ||
import Box from '@mui/material/Box' | ||
ShadowOfTheSpace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import { useModalContext } from '~/context/modal-context' | ||
|
@@ -32,25 +25,25 @@ import { getErrorKey } from '~/utils/get-error-key' | |
|
||
interface CreateOrEditQuizQuestionProps { | ||
question?: Question | ||
setQuestions: Dispatch<SetStateAction<Question[]>> | ||
setQuestions: React.Dispatch<React.SetStateAction<Question[]>> | ||
onCancel: () => void | ||
} | ||
|
||
const CreateOrEditQuizQuestion: FC<CreateOrEditQuizQuestionProps> = ({ | ||
const CreateOrEditQuizQuestion: React.FC<CreateOrEditQuizQuestionProps> = ({ | ||
question, | ||
setQuestions, | ||
onCancel | ||
}) => { | ||
const dispatch = useAppDispatch() | ||
const [isNewQuestion, setIsNewQuestion] = useState<boolean>(!!question) | ||
const [isNewQuestion, setIsNewQuestion] = React.useState<boolean>(!!question) | ||
const { openModal, closeModal } = useModalContext() | ||
|
||
const createQuestionService = useCallback( | ||
const createQuestionService = React.useCallback( | ||
(data?: QuestionForm) => ResourceService.createQuestion(data), | ||
[] | ||
) | ||
|
||
const updateQuestionService = useCallback( | ||
const updateQuestionService = React.useCallback( | ||
(params?: UpdateQuestionParams) => ResourceService.updateQuestion(params), | ||
ShadowOfTheSpace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[] | ||
) | ||
|
@@ -112,9 +105,16 @@ const CreateOrEditQuizQuestion: FC<CreateOrEditQuizQuestionProps> = ({ | |
onResponseError | ||
}) | ||
|
||
const { data, handleInputChange, handleNonInputValueChange, handleSubmit } = | ||
useForm<QuestionForm>({ initialValues: initialValues(question) }) | ||
|
||
const { | ||
data, | ||
handleInputChange, | ||
handleNonInputValueChange, | ||
handleSubmit, | ||
handleErrors, | ||
errors | ||
} = useForm<QuestionForm>({ | ||
initialValues: initialValues(question) | ||
}) | ||
const onCloseCreation = () => { | ||
closeModal() | ||
onCancel() | ||
|
@@ -127,13 +127,30 @@ const CreateOrEditQuizQuestion: FC<CreateOrEditQuizQuestionProps> = ({ | |
closeModal() | ||
} | ||
|
||
const onCreateQuestion = async () => { | ||
await createQuestion(data) | ||
} | ||
const onCreateQuestion = React.useCallback(async () => { | ||
const updatedData = data.openAnswer | ||
? { | ||
...data, | ||
answers: [ | ||
...data.answers, | ||
{ | ||
text: data.openAnswer, | ||
isCorrect: true, | ||
id: data.answers.length | ||
} | ||
], | ||
openAnswer: '' | ||
} | ||
: data | ||
|
||
const onUpdateQuestion = async () => { | ||
question && (await updateQuestion({ ...data, id: question._id })) | ||
} | ||
await createQuestion(updatedData) | ||
}, [data, createQuestion]) | ||
|
||
const onUpdateQuestion = React.useCallback(async () => { | ||
if (question) { | ||
await updateQuestion({ ...data, id: question._id }) | ||
} | ||
}, [question, data, updateQuestion]) | ||
|
||
const onOpenCreateQuestionModal = () => { | ||
openModal({ | ||
|
@@ -147,15 +164,17 @@ const CreateOrEditQuizQuestion: FC<CreateOrEditQuizQuestionProps> = ({ | |
}) | ||
} | ||
|
||
useEffect(() => { | ||
React.useEffect(() => { | ||
!question && onOpenCreateQuestionModal() | ||
ShadowOfTheSpace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's try to not use eslint disablers in our project There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it isn't mine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but If you're working on the pr you should fix all of the issues coming with it. In my opinion at least... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll try to do my best There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just add |
||
}, [question]) | ||
|
||
return isNewQuestion ? ( | ||
<Box component={ComponentEnum.Form} onSubmit={handleSubmit}> | ||
<QuestionEditor | ||
data={data} | ||
errors={errors} | ||
handleErrors={handleErrors} | ||
handleInputChange={handleInputChange} | ||
handleNonInputValueChange={handleNonInputValueChange} | ||
isQuizQuestion | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add braces here, please