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

Change Grade Field na Autocomplete #332

Merged
merged 1 commit into from
Apr 12, 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
6 changes: 3 additions & 3 deletions src/components/Profile/ProfileForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const defaultValues: ProfileFormValues = {
without_school: false,
school: null,
school_not_found: false,
grade: '',
grade: null,
}

export const ProfileForm: FC = () => {
Expand All @@ -52,7 +52,7 @@ export const ProfileForm: FC = () => {
without_school: profile?.school_id === 1,
school: ({id: profile?.school.code, label: profile?.school.verbose_name} as SelectOption) ?? null,
school_not_found: profile?.school_id === 0,
grade: profile?.grade ?? '',
grade: ({id: profile?.grade, label: profile?.grade_name} as SelectOption) ?? null,
}

const {handleSubmit, control, watch, setValue} = useForm<ProfileFormValues>({
Expand Down Expand Up @@ -80,7 +80,7 @@ export const ProfileForm: FC = () => {
school: data.school?.id,
phone: data.phone?.replaceAll(/\s+/gu, ''),
parent_phone: data.parent_phone?.replaceAll(/\s+/gu, ''),
grade: data.grade,
grade: data.grade?.id,
},
new_school_description: data.new_school_description || '',
})
Expand Down
4 changes: 2 additions & 2 deletions src/components/RegisterForm/RegisterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const defaultValues: RegisterFormValues = {
without_school: false,
school: null,
school_not_found: false,
grade: '',
grade: null,
}

export const RegisterForm: FC = () => {
Expand Down Expand Up @@ -69,7 +69,7 @@ export const RegisterForm: FC = () => {
school: data.school?.id,
phone: data.phone?.replaceAll(/\s+/gu, ''),
parent_phone: data.parent_phone?.replaceAll(/\s+/gu, ''),
grade: data.grade,
grade: data.grade?.id,
},
new_school_description: data.new_school_description || '',
})
Expand Down
13 changes: 8 additions & 5 deletions src/components/SchoolSubForm/SchoolSubForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {ISchool} from '@/types/api/personal'
import {FormAutocomplete} from '../FormItems/FormAutocomplete/FormAutocomplete'
import {FormCheckbox} from '../FormItems/FormCheckbox/FormCheckbox'
import {FormInput} from '../FormItems/FormInput/FormInput'
import {FormSelect, SelectOption} from '../FormItems/FormSelect/FormSelect'
import {SelectOption} from '../FormItems/FormSelect/FormSelect'

export type SchoolSubFormValues = {
new_school_description?: string
without_school: boolean
school?: SelectOption | null
school_not_found: boolean
grade: number | ''
grade: SelectOption | null
}

type SchoolSubFormProps<T extends SchoolSubFormValues> = {
Expand All @@ -33,6 +33,7 @@ export const SchoolSubForm = ({control, watch, setValue, gap}: SchoolSubFormProp

const otherSchoolItem = useRef<SelectOption>()
const withoutSchoolItem = useRef<SelectOption>()
const noGradeItem = useRef<SelectOption>()

// načítanie ročníkov z BE, ktorými vyplníme FormSelect s ročníkmi
const {data: gradesData} = useQuery({
Expand All @@ -56,16 +57,18 @@ export const SchoolSubForm = ({control, watch, setValue, gap}: SchoolSubFormProp
otherSchoolItem.current = emptySchoolItems.find(({id}) => id === 0)
withoutSchoolItem.current = emptySchoolItems.find(({id}) => id === 1)
const schoolItems = allSchoolItems.filter(({id}) => ![0, 1].includes(id))
noGradeItem.current = gradeItems.find(({id}) => id === 13)

// predvyplnenie/zmazania hodnôt pri zakliknutí checkboxu pre užívateľa po škole
useUpdateEffect(() => {
if (without_school) {
setValue('school', withoutSchoolItem.current)
setValue('grade', 13)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
setValue('grade', noGradeItem.current!)
setValue('school_not_found', false)
} else {
setValue('school', null)
setValue('grade', '')
setValue('grade', null)
}
}, [without_school])

Expand Down Expand Up @@ -105,7 +108,7 @@ export const SchoolSubForm = ({control, watch, setValue, gap}: SchoolSubFormProp
sx={{mb: '1rem'}}
/>
)}
<FormSelect
<FormAutocomplete
control={control}
name="grade"
label="ročník*"
Expand Down
Loading