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

RA EventRegistration autofill Grade and School from Profile #475

Merged
merged 1 commit into from
Nov 23, 2024
Merged
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,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>
)
}
Loading