feat: remove ClassEnrollmentRequest and related enrollment logic, simplifying class enrollment management
This commit is contained in:
parent
65618bf961
commit
0d27a9e706
@ -3,7 +3,6 @@
|
||||
namespace App\Http\Controllers\Admin\Manage;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\Manage\ClassEnrollmentRequest;
|
||||
use App\Models\ClassEnrollment;
|
||||
use App\Models\CourseClass;
|
||||
use App\Services\Admin\Manage\ClassEnrollmentService;
|
||||
@ -24,17 +23,9 @@ public function index(CourseClass $courseClass): Response
|
||||
return Inertia::render('admin/manage/course-classes/enrollments', [
|
||||
'courseClass' => $courseClass,
|
||||
'enrollments' => $this->service->forClass($courseClass),
|
||||
'availableStudents' => $this->service->availableStudents($courseClass),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(ClassEnrollmentRequest $request, CourseClass $courseClass): RedirectResponse
|
||||
{
|
||||
$this->service->enrollMany($courseClass, $request->validated('student_ids'));
|
||||
|
||||
return Inertia::flash('toast', ['type' => 'success', 'message' => 'Mahasiswa berhasil ditambahkan ke kelas.'])->back();
|
||||
}
|
||||
|
||||
public function destroy(CourseClass $courseClass, ClassEnrollment $enrollment): RedirectResponse
|
||||
{
|
||||
$this->service->unenroll($enrollment);
|
||||
|
||||
@ -1,29 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin\Manage;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ClassEnrollmentRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user()->can('create-course-class-enrollments');
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
$courseClass = $this->route('course_class');
|
||||
$departmentId = $courseClass->course->department_id;
|
||||
|
||||
return [
|
||||
'student_ids' => ['required', 'array', 'min:1'],
|
||||
'student_ids.*' => [
|
||||
'integer',
|
||||
Rule::exists('students', 'id')->where('department_id', $departmentId),
|
||||
Rule::unique('class_enrollments', 'student_id')->where('course_class_id', $courseClass->id),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -4,7 +4,6 @@
|
||||
|
||||
use App\Models\ClassEnrollment;
|
||||
use App\Models\CourseClass;
|
||||
use App\Models\Student;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class ClassEnrollmentService
|
||||
@ -17,25 +16,6 @@ public function forClass(CourseClass $courseClass): Collection
|
||||
->get();
|
||||
}
|
||||
|
||||
public function availableStudents(CourseClass $courseClass): Collection
|
||||
{
|
||||
return Student::query()
|
||||
->where('department_id', $courseClass->course->department_id)
|
||||
->whereDoesntHave('enrollments', fn ($q) => $q->where('course_class_id', $courseClass->id))
|
||||
->with(['user.profile', 'department'])
|
||||
->get();
|
||||
}
|
||||
|
||||
public function enrollMany(CourseClass $courseClass, array $studentIds): void
|
||||
{
|
||||
foreach ($studentIds as $studentId) {
|
||||
$courseClass->enrollments()->firstOrCreate(
|
||||
['student_id' => $studentId],
|
||||
['enrolled_at' => now()],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function unenroll(ClassEnrollment $enrollment): bool
|
||||
{
|
||||
return $enrollment->delete();
|
||||
|
||||
@ -1,47 +1,33 @@
|
||||
import { Head, Link, router } from '@inertiajs/react';
|
||||
import type { ColumnDef } from '@tanstack/react-table';
|
||||
import { format } from 'date-fns';
|
||||
import { ArrowLeft, Trash2, UserPlus } from 'lucide-react';
|
||||
import { ArrowLeft, Trash2 } from 'lucide-react';
|
||||
import { useState } from 'react';
|
||||
import { DataTable } from '@/components/data-table';
|
||||
import { DeleteConfirmDialog } from '@/components/delete-confirm-dialog';
|
||||
import { FormDialog } from '@/components/form-dialog';
|
||||
import InputError from '@/components/input-error';
|
||||
import { PageHeader } from '@/components/page-header';
|
||||
import { RowActions } from '@/components/row-actions';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Checkbox } from '@/components/ui/checkbox';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { usePermissions } from '@/hooks/use-permissions';
|
||||
import { index as courseClassIndex } from '@/routes/admin/manage/course-classes';
|
||||
import {
|
||||
destroy,
|
||||
store,
|
||||
} from '@/routes/admin/manage/course-classes/enrollments';
|
||||
import { destroy } from '@/routes/admin/manage/course-classes/enrollments';
|
||||
import { formatAcademicTermLabel } from '@/types/academic-term';
|
||||
import type {
|
||||
ClassEnrollment,
|
||||
EnrollmentStudent,
|
||||
} from '@/types/class-enrollment';
|
||||
import type { ClassEnrollment } from '@/types/class-enrollment';
|
||||
import type { CourseClass } from '@/types/course-class';
|
||||
import { ClassMethodLabels } from '@/types/course-class';
|
||||
|
||||
type Props = {
|
||||
courseClass: CourseClass;
|
||||
enrollments: ClassEnrollment[];
|
||||
availableStudents: EnrollmentStudent[];
|
||||
};
|
||||
|
||||
export default function ClassEnrollmentIndex({
|
||||
courseClass,
|
||||
enrollments,
|
||||
availableStudents,
|
||||
}: Props) {
|
||||
const [createOpen, setCreateOpen] = useState(false);
|
||||
const [deleting, setDeleting] = useState<ClassEnrollment | null>(null);
|
||||
const { hasPermission } = usePermissions();
|
||||
const canCreate = hasPermission('create-course-class-enrollments');
|
||||
const canDelete = hasPermission('delete-course-class-enrollments');
|
||||
|
||||
function handleDelete() {
|
||||
@ -158,25 +144,10 @@ export default function ClassEnrollmentIndex({
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<h2 className="text-lg font-semibold">Daftar Mahasiswa</h2>
|
||||
{canCreate && (
|
||||
<Button onClick={() => setCreateOpen(true)}>
|
||||
<UserPlus className="h-4 w-4" />
|
||||
Tambah Mahasiswa
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
<h2 className="text-lg font-semibold">Daftar Mahasiswa</h2>
|
||||
|
||||
<DataTable columns={columns} data={enrollments} />
|
||||
|
||||
<EnrollForm
|
||||
open={createOpen}
|
||||
onOpenChange={setCreateOpen}
|
||||
courseClassId={courseClass.id}
|
||||
availableStudents={availableStudents}
|
||||
/>
|
||||
|
||||
<DeleteConfirmDialog
|
||||
target={deleting}
|
||||
onOpenChange={(open) => {
|
||||
@ -194,123 +165,3 @@ export default function ClassEnrollmentIndex({
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function EnrollForm({
|
||||
open,
|
||||
onOpenChange,
|
||||
courseClassId,
|
||||
availableStudents,
|
||||
}: {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
courseClassId: number;
|
||||
availableStudents: EnrollmentStudent[];
|
||||
}) {
|
||||
const [selectedIds, setSelectedIds] = useState<number[]>([]);
|
||||
|
||||
const allSelected =
|
||||
availableStudents.length > 0 &&
|
||||
selectedIds.length === availableStudents.length;
|
||||
|
||||
function toggleAll() {
|
||||
setSelectedIds(
|
||||
allSelected ? [] : availableStudents.map((student) => student.id),
|
||||
);
|
||||
}
|
||||
|
||||
function toggleOne(id: number) {
|
||||
setSelectedIds((prev) =>
|
||||
prev.includes(id)
|
||||
? prev.filter((selectedId) => selectedId !== id)
|
||||
: [...prev, id],
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<FormDialog
|
||||
open={open}
|
||||
onOpenChange={onOpenChange}
|
||||
title="Tambah Mahasiswa"
|
||||
action={store(courseClassId)}
|
||||
resetOnSuccess
|
||||
submitDisabled={selectedIds.length === 0}
|
||||
submitLabel={
|
||||
selectedIds.length > 0
|
||||
? `Tambahkan (${selectedIds.length})`
|
||||
: 'Tambahkan'
|
||||
}
|
||||
onSuccess={() => {
|
||||
onOpenChange(false);
|
||||
setSelectedIds([]);
|
||||
}}
|
||||
>
|
||||
{({ errors }) => (
|
||||
<div className="grid gap-4">
|
||||
<div className="grid gap-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<Label>
|
||||
Mahasiswa{' '}
|
||||
<span className="text-destructive">*</span>
|
||||
</Label>
|
||||
{availableStudents.length > 0 && (
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id="select-all-students"
|
||||
checked={allSelected}
|
||||
onCheckedChange={toggleAll}
|
||||
/>
|
||||
<Label
|
||||
htmlFor="select-all-students"
|
||||
className="text-sm font-normal"
|
||||
>
|
||||
Pilih Semua
|
||||
</Label>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{selectedIds.map((id) => (
|
||||
<input
|
||||
key={id}
|
||||
type="hidden"
|
||||
name="student_ids[]"
|
||||
value={id}
|
||||
/>
|
||||
))}
|
||||
<div className="max-h-64 overflow-y-auto rounded-md border">
|
||||
{availableStudents.length === 0 ? (
|
||||
<p className="p-4 text-sm text-muted-foreground">
|
||||
Tidak ada mahasiswa dari jurusan yang sama
|
||||
untuk didaftarkan.
|
||||
</p>
|
||||
) : (
|
||||
availableStudents.map((student) => (
|
||||
<label
|
||||
key={student.id}
|
||||
htmlFor={`student-${student.id}`}
|
||||
className="flex items-center gap-2 border-b px-3 py-2 last:border-b-0 hover:bg-muted/50"
|
||||
>
|
||||
<Checkbox
|
||||
id={`student-${student.id}`}
|
||||
checked={selectedIds.includes(
|
||||
student.id,
|
||||
)}
|
||||
onCheckedChange={() =>
|
||||
toggleOne(student.id)
|
||||
}
|
||||
/>
|
||||
<span className="text-sm">
|
||||
{student.user?.profile?.full_name ??
|
||||
'N/A'}{' '}
|
||||
- {student.student_number}
|
||||
</span>
|
||||
</label>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
<InputError message={errors.student_ids} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</FormDialog>
|
||||
);
|
||||
}
|
||||
|
||||
@ -117,7 +117,6 @@
|
||||
|
||||
Route::prefix('course-classes/{course_class}/enrollments')->name('course-classes.enrollments.')->group(function () {
|
||||
Route::get('/', [ClassEnrollmentController::class, 'index'])->name('index')->middleware('permission:view-course-class-enrollments');
|
||||
Route::post('/', [ClassEnrollmentController::class, 'store'])->name('store')->middleware('permission:create-course-class-enrollments');
|
||||
Route::delete('{enrollment}', [ClassEnrollmentController::class, 'destroy'])->name('destroy')->middleware('permission:delete-course-class-enrollments');
|
||||
});
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user