Skip to content

Commit

Permalink
Fixed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurenko committed Sep 15, 2023
1 parent 059e175 commit 105167e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
7 changes: 7 additions & 0 deletions src/components/question-editor/QuestionEditor.constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
12 changes: 8 additions & 4 deletions src/components/question-editor/QuestionEditor.styles.ts
Original file line number Diff line number Diff line change
@@ -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: {},
Expand All @@ -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',
Expand Down
29 changes: 18 additions & 11 deletions src/components/question-editor/QuestionEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -58,9 +62,8 @@ const QuestionEditor: FC<QuestionEditorProps> = ({ 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)

Expand Down Expand Up @@ -156,16 +159,20 @@ const QuestionEditor: FC<QuestionEditorProps> = ({ fetchData, loading }) => {
</Box>
))

const disabledButton =
type === 'openAnswer' ? text && openAnswer : text && answers[0]?.text
const disabledButton = isOpenAnswer
? Boolean(text && openAnswer)
: Boolean(text && answers[0]?.text)

const buttons = (
<Box sx={styles.buttons}>
<AppButton onClick={() => navigate(-1)} variant={ButtonVariantEnum.Tonal}>
<AppButton
onClick={() => navigate(authRoutes.myResources.root.path)}
variant={ButtonVariantEnum.Tonal}
>
{t('common.cancel')}
</AppButton>
<AppButton
disabled={!disabledButton}
disabled={disabledButton}
loading={loading}
type={ButtonTypeEnum.Submit}
>
Expand All @@ -183,11 +190,11 @@ const QuestionEditor: FC<QuestionEditorProps> = ({ fetchData, loading }) => {
sx={styles.root}
>
<AppTextField
InputLabelProps={styles.titleLabel}
InputLabelProps={styles.titleLabel(title)}
InputProps={styles.titleInput}
fullWidth
inputProps={styles.input}
label={title ? '' : t('questionPage.untitled')}
label={t('questionPage.untitled')}
onChange={handleInputChange('title')}
value={title}
variant={TextFieldVariantEnum.Standard}
Expand All @@ -207,7 +214,7 @@ const QuestionEditor: FC<QuestionEditorProps> = ({ fetchData, loading }) => {
<Divider sx={styles.mainDivider} />
<Box sx={styles.editorBlock}>
<Box sx={styles.options}>
<Box sx={styles.iconWrapper}>{option!.icon}</Box>
{option && <Box sx={styles.iconWrapper}>{option.icon}</Box>}
<AppSelect
fields={sortOptions}
setValue={handleTypeChange}
Expand Down
7 changes: 4 additions & 3 deletions src/pages/create-or-edit-question/CreateOrEditQuestion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import PageWrapper from '~/components/page-wrapper/PageWrapper'
import QuestionEditor from '~/components/question-editor/QuestionEditor'
import useAxios from '~/hooks/use-axios'
import { useSnackBarContext } from '~/context/snackbar-context'
import { authRoutes } from '~/router/constants/authRoutes'

import { questionService } from '~/services/question-service'
import { ResourceService } from '~/services/resource-service'
import { defaultResponses, snackbarVariants } from '~/constants'
import { ErrorResponse, QuestionForm } from '~/types'

Expand All @@ -15,7 +16,7 @@ const CreateOrEditQuestion = () => {
const { setAlert } = useSnackBarContext()

const createQuestion = useCallback(
(data?: QuestionForm) => questionService.createQuestion(data),
(data?: QuestionForm) => ResourceService.createQuestion(data),
[]
)

Expand All @@ -24,7 +25,7 @@ const CreateOrEditQuestion = () => {
severity: snackbarVariants.success,
message: 'categoriesPage.newSubject.successMessage'
})
navigate(-1)
navigate(authRoutes.myResources.root.path)
}

const handleResponseError = (error: ErrorResponse) => {
Expand Down
10 changes: 0 additions & 10 deletions src/services/question-service.tsx

This file was deleted.

6 changes: 5 additions & 1 deletion src/services/resource-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
Lesson,
NewLesson,
UpdateAttachmentParams,
Question
Question,
QuestionForm
} from '~/types'
import { createUrlPath } from '~/utils/helper-functions'

Expand Down Expand Up @@ -53,5 +54,8 @@ export const ResourceService = {
params?: GetResourcesParams
): Promise<AxiosResponse<ItemsWithCount<Question>>> => {
return axiosClient.get(URLs.resources.questions.get, { params })
},
createQuestion: async (data?: QuestionForm): Promise<AxiosResponse> => {
return await axiosClient.post(URLs.question.post, data)
}
}

0 comments on commit 105167e

Please sign in to comment.