Skip to content
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

Rewrote SubjectsStep to tsx format #2233

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useCallback } from 'react'
import { useState, useCallback, SyntheticEvent } from 'react'
import { useTranslation } from 'react-i18next'

import Box from '@mui/material/Box'
Expand All @@ -16,25 +16,35 @@ import img from '~/assets/img/tutor-home-page/become-tutor/study-category.svg'
import { useStepContext } from '~/context/step-context'
import { categoryService } from '~/services/category-service'
import { subjectService } from '~/services/subject-service'
import { CategoryNameInterface, SubjectNameInterface } from '~/types'

const SubjectsStep = ({ btnsBox }) => {
interface SubjectsStepProps {
btnsBox: JSX.Element
}

interface SubjectsType {
category: CategoryNameInterface | null
subject: SubjectNameInterface | null
}

const SubjectsStep = ({ btnsBox }: SubjectsStepProps) => {
const { t } = useTranslation()
const { isLaptopAndAbove, isMobile } = useBreakpoints()
const { stepData, handleSubjects } = useStepContext()
const subjectData = stepData.subjects

const [subjects, setSubjects] = useState({
const [subjects, setSubjects] = useState<SubjectsType>({
category: null,
subject: null
})

const [subjectError, setSubjectError] = useState('')
const [subjectFetched, setSubjectIsFetched] = useState(false)
const [subjectIsFetched, setSubjectIsFetched] = useState(false)

const fetchSubjectHandler = () => setSubjectIsFetched(true)

const getSubjectsNames = useCallback(
() => subjectService.getSubjectsNames(subjects.category._id),
() => subjectService.getSubjectsNames(subjects.category?._id ?? null),
[subjects.category]
)

Expand All @@ -44,18 +54,25 @@ const SubjectsStep = ({ btnsBox }) => {
</Box>
)

const onChangeCategory = (_, value) => {
setSubjects(
(prev) =>
prev.category?._id !== value?._id && {
category: value,
subject: null
}
const onChangeCategory = (
_: SyntheticEvent,
value: CategoryNameInterface | null
) => {
setSubjects((prev) =>
prev.category?._id !== value?._id
? {
category: value,
subject: null
}
: prev
)
setSubjectIsFetched(false)
}

const onChangeSubject = (_, value) => {
const onChangeSubject = (
_: SyntheticEvent,
value: SubjectNameInterface | null
) => {
setSubjects((prev) => ({ category: prev.category, subject: value }))
subjectError && setSubjectError('')
}
Expand Down Expand Up @@ -89,7 +106,7 @@ const SubjectsStep = ({ btnsBox }) => {
})
}

const handleChipDelete = (item) => {
const handleChipDelete = (item: string) => {
const newItems = subjectData.filter(({ name }) => name !== item)
handleSubjects(newItems)
}
Expand Down Expand Up @@ -117,7 +134,7 @@ const SubjectsStep = ({ btnsBox }) => {
<AsyncAutocomplete
axiosProps={{ onResponse: fetchSubjectHandler }}
disabled={!subjects.category}
fetchCondition={!subjectFetched}
fetchCondition={!subjectIsFetched}
fetchOnFocus
labelField='name'
onChange={onChangeSubject}
Expand Down
2 changes: 1 addition & 1 deletion src/types/common/interfaces/common.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export interface StepData {
errors: Record<string, string>
}
photo: string[]
subjects: SubjectInterface[]
subjects: Array<SubjectNameInterface & { category: CategoryNameInterface }>
language: UserResponse['nativeLanguage']
}

Expand Down
Loading