Skip to content

Commit

Permalink
RA EventRegistration autofill Grade and School from Profile (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalmasrna1 authored Nov 23, 2024
1 parent b0228a6 commit e1f681e
Showing 1 changed file with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
import {FC} from 'react'
import {AutocompleteInput, ReferenceInput, required, SimpleForm} from 'react-admin'
import {AutocompleteInput, RaRecord, ReferenceInput, required, SimpleForm} from 'react-admin'
import {useFormContext} from 'react-hook-form'

import {MyCreate} from '@/components/Admin/custom/MyCreate'

export const EventRegistrationCreate: FC = () => (
<MyCreate>
<SimpleForm>
<ReferenceInput source="profile" reference="personal/profiles">
<AutocompleteInput optionText="verbose_name" fullWidth validate={required()} />
</ReferenceInput>
<ReferenceInput source="school" reference="personal/schools">
<AutocompleteInput optionText="verbose_name" fullWidth validate={required()} />
</ReferenceInput>
<ReferenceInput source="grade" reference="competition/grade">
<AutocompleteInput optionText="name" fullWidth validate={required()} />
</ReferenceInput>
<ReferenceInput source="event" reference="competition/event">
<AutocompleteInput optionText="verbose_name" fullWidth validate={required()} />
</ReferenceInput>
</SimpleForm>
</MyCreate>
)
const ProfileInput = () => {
const {setValue} = useFormContext()
const prefill = (_: number, profile: RaRecord | string) => {
if (typeof profile === 'string') return
setValue('school', profile.school.code)
setValue('grade', profile.grade)
}

return (
<ReferenceInput source="profile" reference="personal/profiles">
<AutocompleteInput optionText="verbose_name" fullWidth validate={required()} onChange={prefill} />
</ReferenceInput>
)
}

export const EventRegistrationCreate: FC = () => {
return (
<MyCreate>
<SimpleForm>
<ProfileInput />
<ReferenceInput source="school" reference="personal/schools">
<AutocompleteInput optionText="verbose_name" fullWidth validate={required()} />
</ReferenceInput>
<ReferenceInput source="grade" reference="competition/grade">
<AutocompleteInput optionText="name" fullWidth validate={required()} />
</ReferenceInput>
<ReferenceInput source="event" reference="competition/event">
<AutocompleteInput optionText="verbose_name" fullWidth validate={required()} />
</ReferenceInput>
</SimpleForm>
</MyCreate>
)
}

0 comments on commit e1f681e

Please sign in to comment.