From 105167e78f0f24a1946c23b16d59735b19045348 Mon Sep 17 00:00:00 2001 From: Yura Didenko Date: Fri, 15 Sep 2023 12:31:11 +0300 Subject: [PATCH] Fixed comments --- .../QuestionEditor.constants.tsx | 7 +++++ .../question-editor/QuestionEditor.styles.ts | 12 +++++--- .../question-editor/QuestionEditor.tsx | 29 ++++++++++++------- .../CreateOrEditQuestion.tsx | 7 +++-- src/services/question-service.tsx | 10 ------- src/services/resource-service.ts | 6 +++- 6 files changed, 42 insertions(+), 29 deletions(-) delete mode 100644 src/services/question-service.tsx diff --git a/src/components/question-editor/QuestionEditor.constants.tsx b/src/components/question-editor/QuestionEditor.constants.tsx index 93e60b0a39..82fbe58dc6 100644 --- a/src/components/question-editor/QuestionEditor.constants.tsx +++ b/src/components/question-editor/QuestionEditor.constants.tsx @@ -21,3 +21,10 @@ export const sortQuestions = [ value: 'oneAnswer' } ] + +export const questionType = (type: string) => { + const isMultipleChoice = type === sortQuestions[0].value + const isOpenAnswer = type === sortQuestions[1].value + const isSingleChoice = type === sortQuestions[2].value + return { isMultipleChoice, isOpenAnswer, isSingleChoice } +} diff --git a/src/components/question-editor/QuestionEditor.styles.ts b/src/components/question-editor/QuestionEditor.styles.ts index 9fe3825005..acf0dc6a9b 100644 --- a/src/components/question-editor/QuestionEditor.styles.ts +++ b/src/components/question-editor/QuestionEditor.styles.ts @@ -1,5 +1,5 @@ import { commonShadow } from '~/styles/app-theme/custom-shadows' -import { TypographyVariantEnum } from '~/types' +import { TypographyVariantEnum, VisibilityEnum } from '~/types' export const styles = { root: {}, @@ -20,10 +20,14 @@ export const styles = { marginTop: 0 } }, - titleLabel: { + titleLabel: (value: string) => ({ shrink: false, - sx: { typography: TypographyVariantEnum.H5, top: -14 } - }, + sx: { + visibility: value ? VisibilityEnum.Hidden : VisibilityEnum.Visible, + typography: TypographyVariantEnum.H5, + top: -14 + } + }), labelCategory: { color: 'primary.600', maxWidth: '464px', diff --git a/src/components/question-editor/QuestionEditor.tsx b/src/components/question-editor/QuestionEditor.tsx index 4570a53e78..a4880c5808 100644 --- a/src/components/question-editor/QuestionEditor.tsx +++ b/src/components/question-editor/QuestionEditor.tsx @@ -20,7 +20,11 @@ import AppTextField from '~/components/app-text-field/AppTextField' import AppSelect from '~/components/app-select/AppSelect' import useForm from '~/hooks/use-form' import AppButton from '~/components/app-button/AppButton' -import { sortQuestions } from '~/components/question-editor/QuestionEditor.constants' +import { + questionType, + sortQuestions +} from '~/components/question-editor/QuestionEditor.constants' +import { authRoutes } from '~/router/constants/authRoutes' import { styles } from '~/components/question-editor/QuestionEditor.styles' import { @@ -58,9 +62,8 @@ const QuestionEditor: FC = ({ fetchData, loading }) => { const { type, title, text, answers, openAnswer } = data - const isMultipleChoice = type === sortQuestions[0].value - const isOpenAnswer = type === sortQuestions[1].value - const isSingleChoice = type === sortQuestions[2].value + const { isMultipleChoice, isOpenAnswer, isSingleChoice } = questionType(type) + const isEmptyAnswer = answers[answers.length - 1]?.text === '' const option = sortQuestions.find((item) => item.value === data.type) @@ -156,16 +159,20 @@ const QuestionEditor: FC = ({ fetchData, loading }) => { )) - const disabledButton = - type === 'openAnswer' ? text && openAnswer : text && answers[0]?.text + const disabledButton = isOpenAnswer + ? Boolean(text && openAnswer) + : Boolean(text && answers[0]?.text) const buttons = ( - navigate(-1)} variant={ButtonVariantEnum.Tonal}> + navigate(authRoutes.myResources.root.path)} + variant={ButtonVariantEnum.Tonal} + > {t('common.cancel')} @@ -183,11 +190,11 @@ const QuestionEditor: FC = ({ fetchData, loading }) => { sx={styles.root} > = ({ fetchData, loading }) => { - {option!.icon} + {option && {option.icon}} { const { setAlert } = useSnackBarContext() const createQuestion = useCallback( - (data?: QuestionForm) => questionService.createQuestion(data), + (data?: QuestionForm) => ResourceService.createQuestion(data), [] ) @@ -24,7 +25,7 @@ const CreateOrEditQuestion = () => { severity: snackbarVariants.success, message: 'categoriesPage.newSubject.successMessage' }) - navigate(-1) + navigate(authRoutes.myResources.root.path) } const handleResponseError = (error: ErrorResponse) => { diff --git a/src/services/question-service.tsx b/src/services/question-service.tsx deleted file mode 100644 index 5b5eb40999..0000000000 --- a/src/services/question-service.tsx +++ /dev/null @@ -1,10 +0,0 @@ -import { axiosClient } from '~/plugins/axiosClient' -import { AxiosResponse } from 'axios' -import { URLs } from '~/constants/request' -import { QuestionForm } from '~/types' - -export const questionService = { - createQuestion: async (data?: QuestionForm): Promise => { - return await axiosClient.post(URLs.question.post, data) - } -} diff --git a/src/services/resource-service.ts b/src/services/resource-service.ts index 21eb780371..76c2e7fe1a 100644 --- a/src/services/resource-service.ts +++ b/src/services/resource-service.ts @@ -11,7 +11,8 @@ import { Lesson, NewLesson, UpdateAttachmentParams, - Question + Question, + QuestionForm } from '~/types' import { createUrlPath } from '~/utils/helper-functions' @@ -53,5 +54,8 @@ export const ResourceService = { params?: GetResourcesParams ): Promise>> => { return axiosClient.get(URLs.resources.questions.get, { params }) + }, + createQuestion: async (data?: QuestionForm): Promise => { + return await axiosClient.post(URLs.question.post, data) } }