-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RA EventRegistration autofill Grade and School from Profile (#475)
- Loading branch information
1 parent
b0228a6
commit e1f681e
Showing
1 changed file
with
35 additions
and
19 deletions.
There are no files selected for viewing
54 changes: 35 additions & 19 deletions
54
src/components/Admin/resources/competition/event-registration/EventRegistrationCreate.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |