'use client' import { useEffect, useState } from 'react' import { toast } from 'sonner' import { Loader2 } from 'lucide-react' import { trpc } from '@/lib/trpc/client' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Label } from '@/components/ui/label' import { Textarea } from '@/components/ui/textarea' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' const REASON_MIN = 10 const REASON_MAX = 2000 const TARGET_ANY = '__any__' type MentorOption = { assignmentId: string name: string } type RequestChangeDialogProps = { projectId: string mentors: MentorOption[] open: boolean onOpenChange: (open: boolean) => void } export function RequestChangeDialog({ projectId, mentors, open, onOpenChange, }: RequestChangeDialogProps) { const [reason, setReason] = useState('') const [target, setTarget] = useState(TARGET_ANY) const [touched, setTouched] = useState(false) const utils = trpc.useUtils() const requestChange = trpc.mentor.requestChange.useMutation({ onSuccess: async () => { toast.success( "Your request has been sent to the program admins. We'll review it and follow up.", ) onOpenChange(false) // Refresh dashboard so the disabled state for the button updates. await utils.applicant.getMyDashboard.invalidate() }, onError: (error) => { toast.error(error.message || 'Could not send your request. Please try again.') }, }) // Reset form when the dialog is closed. useEffect(() => { if (!open) { setReason('') setTarget(TARGET_ANY) setTouched(false) } }, [open]) const trimmedReason = reason.trim() const reasonTooShort = trimmedReason.length < REASON_MIN const reasonTooLong = trimmedReason.length > REASON_MAX const reasonInvalid = reasonTooShort || reasonTooLong const showReasonError = touched && reasonInvalid const handleSubmit = (e: React.FormEvent) => { e.preventDefault() setTouched(true) if (reasonInvalid) return requestChange.mutate({ projectId, targetAssignmentId: target === TARGET_ANY ? undefined : target, reason: trimmedReason, }) } return ( Request a mentor change Share a few details so the program admins can follow up with you. Your current mentor will not see this message.
{mentors.length > 0 && (

Optional. Use this if your request is about one of your co-mentors in particular.

)}