feat: add department selection and advisor filtering in student create and edit forms
This commit is contained in:
parent
a7aec7245b
commit
6621757713
@ -73,7 +73,8 @@ public function rules(): array
|
||||
'academic_advisor_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists('lecturers', 'id'),
|
||||
Rule::exists('lecturer_department', 'lecturer_id')
|
||||
->where('department_id', $this->input('department_id')),
|
||||
],
|
||||
'status' => [
|
||||
'nullable',
|
||||
|
||||
@ -35,6 +35,7 @@ type Lecturer = {
|
||||
id: number;
|
||||
lecturer_number: string;
|
||||
user: { profile: { full_name: string } | null } | null;
|
||||
departments: { id: number }[];
|
||||
};
|
||||
|
||||
type Props = {
|
||||
@ -45,8 +46,15 @@ type Props = {
|
||||
export default function StudentCreate({ departments, lecturers }: Props) {
|
||||
const [gender, setGender] = useState('');
|
||||
const [birthDate, setBirthDate] = useState<Date | undefined>();
|
||||
const [departmentId, setDepartmentId] = useState('');
|
||||
const [advisor, setAdvisor] = useState<Lecturer | null>(null);
|
||||
|
||||
const availableAdvisors = departmentId
|
||||
? lecturers.filter((l) =>
|
||||
l.departments.some((d) => String(d.id) === departmentId),
|
||||
)
|
||||
: [];
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head title="Tambah Mahasiswa" />
|
||||
@ -279,8 +287,15 @@ export default function StudentCreate({ departments, lecturers }: Props) {
|
||||
<input
|
||||
type="hidden"
|
||||
name="department_id"
|
||||
value={departmentId}
|
||||
/>
|
||||
<Select name="department_id">
|
||||
<Select
|
||||
value={departmentId}
|
||||
onValueChange={(value) => {
|
||||
setDepartmentId(value);
|
||||
setAdvisor(null);
|
||||
}}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue placeholder="Pilih jurusan" />
|
||||
</SelectTrigger>
|
||||
@ -354,8 +369,9 @@ export default function StudentCreate({ departments, lecturers }: Props) {
|
||||
value={advisor?.id ?? ''}
|
||||
/>
|
||||
<Combobox
|
||||
items={lecturers}
|
||||
items={availableAdvisors}
|
||||
value={advisor}
|
||||
disabled={!departmentId}
|
||||
onValueChange={setAdvisor}
|
||||
itemToStringLabel={(lect) =>
|
||||
`${lect.user?.profile?.full_name ?? 'N/A'} - ${lect.lecturer_number}`
|
||||
@ -365,12 +381,18 @@ export default function StudentCreate({ departments, lecturers }: Props) {
|
||||
}
|
||||
>
|
||||
<ComboboxInput
|
||||
placeholder="Pilih pembimbing akademik"
|
||||
disabled={!departmentId}
|
||||
placeholder={
|
||||
departmentId
|
||||
? 'Pilih pembimbing akademik'
|
||||
: 'Pilih jurusan terlebih dahulu'
|
||||
}
|
||||
className="w-full"
|
||||
/>
|
||||
<ComboboxContent>
|
||||
<ComboboxEmpty>
|
||||
Dosen tidak ditemukan.
|
||||
Dosen tidak
|
||||
ditemukan.
|
||||
</ComboboxEmpty>
|
||||
<ComboboxList>
|
||||
{(lect: Lecturer) => (
|
||||
|
||||
@ -35,6 +35,7 @@ type Lecturer = {
|
||||
id: number;
|
||||
lecturer_number: string;
|
||||
user: { profile: { full_name: string } | null } | null;
|
||||
departments: { id: number }[];
|
||||
};
|
||||
|
||||
type User = {
|
||||
@ -71,12 +72,23 @@ export default function StudentEdit({ user, departments, lecturers }: Props) {
|
||||
? new Date(user.profile.birth_date)
|
||||
: undefined,
|
||||
);
|
||||
const [departmentId, setDepartmentId] = useState(
|
||||
user.student?.department_id
|
||||
? String(user.student.department_id)
|
||||
: '',
|
||||
);
|
||||
const [advisor, setAdvisor] = useState<Lecturer | null>(
|
||||
lecturers.find(
|
||||
(lect) => lect.id === user.student?.academic_advisor_id,
|
||||
) ?? null,
|
||||
);
|
||||
|
||||
const availableAdvisors = departmentId
|
||||
? lecturers.filter((l) =>
|
||||
l.departments.some((d) => String(d.id) === departmentId),
|
||||
)
|
||||
: [];
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head title="Edit Mahasiswa" />
|
||||
@ -318,16 +330,17 @@ export default function StudentEdit({ user, departments, lecturers }: Props) {
|
||||
*
|
||||
</span>
|
||||
</Label>
|
||||
<Select
|
||||
<input
|
||||
type="hidden"
|
||||
name="department_id"
|
||||
defaultValue={
|
||||
user.student?.department_id
|
||||
? String(
|
||||
user.student
|
||||
.department_id,
|
||||
)
|
||||
: undefined
|
||||
}
|
||||
value={departmentId}
|
||||
/>
|
||||
<Select
|
||||
value={departmentId}
|
||||
onValueChange={(value) => {
|
||||
setDepartmentId(value);
|
||||
setAdvisor(null);
|
||||
}}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue placeholder="Pilih jurusan" />
|
||||
@ -407,8 +420,9 @@ export default function StudentEdit({ user, departments, lecturers }: Props) {
|
||||
value={advisor?.id ?? ''}
|
||||
/>
|
||||
<Combobox
|
||||
items={lecturers}
|
||||
items={availableAdvisors}
|
||||
value={advisor}
|
||||
disabled={!departmentId}
|
||||
onValueChange={setAdvisor}
|
||||
itemToStringLabel={(lect) =>
|
||||
`${lect.user?.profile?.full_name ?? 'N/A'} - ${lect.lecturer_number}`
|
||||
@ -418,12 +432,18 @@ export default function StudentEdit({ user, departments, lecturers }: Props) {
|
||||
}
|
||||
>
|
||||
<ComboboxInput
|
||||
placeholder="Pilih pembimbing akademik"
|
||||
disabled={!departmentId}
|
||||
placeholder={
|
||||
departmentId
|
||||
? 'Pilih pembimbing akademik'
|
||||
: 'Pilih jurusan terlebih dahulu'
|
||||
}
|
||||
className="w-full"
|
||||
/>
|
||||
<ComboboxContent>
|
||||
<ComboboxEmpty>
|
||||
Dosen tidak ditemukan.
|
||||
Dosen tidak
|
||||
ditemukan.
|
||||
</ComboboxEmpty>
|
||||
<ComboboxList>
|
||||
{(lect: Lecturer) => (
|
||||
|
||||
Loading…
Reference in New Issue
Block a user