import { useState } from 'react' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { gradingScaleAllOptions, lessonPlanItemMutations, lessonPlanItemGradeHistoryOptions, lessonPlanItemKeys } from '@/api/lessons' import { Button } from '@/components/ui/button' import { Label } from '@/components/ui/label' import { Textarea } from '@/components/ui/textarea' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { toast } from 'sonner' import type { LessonPlanItem } from '@/types/lesson' interface Props { item: LessonPlanItem open: boolean onClose: () => void } export function GradeEntryDialog({ item, open, onClose }: Props) { const queryClient = useQueryClient() const [selectedScaleId, setSelectedScaleId] = useState(item.gradingScaleId ?? '') const [selectedValue, setSelectedValue] = useState('') const [gradeNotes, setGradeNotes] = useState('') const { data: scales } = useQuery(gradingScaleAllOptions()) const { data: history } = useQuery(lessonPlanItemGradeHistoryOptions(item.id)) const selectedScale = scales?.find((s) => s.id === selectedScaleId) const gradeMutation = useMutation({ mutationFn: () => lessonPlanItemMutations.addGrade(item.id, { gradingScaleId: selectedScaleId || undefined, gradeValue: selectedValue, notes: gradeNotes || undefined, }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: lessonPlanItemKeys.gradeHistory(item.id) }) toast.success('Grade recorded') setSelectedValue('') setGradeNotes('') }, onError: (err) => toast.error(err.message), }) return ( { if (!o) onClose() }}> Grade: {item.title}
{selectedScale ? ( ) : ( setSelectedValue(e.target.value)} placeholder="Enter grade (e.g. A, Pass, 85)" /> )}