|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { Appointment } from "../../types/appointments.types"; |
| 3 | +import { useUpdateAppointmentMutation } from "../../features/appointments/mutations/useUpdateAppointmentMutation"; |
| 4 | + |
| 5 | +interface RejectAppointmentModalProps { |
| 6 | + appointment: Appointment; |
| 7 | + isOpen: boolean; |
| 8 | + onClose: () => void; |
| 9 | +} |
| 10 | + |
| 11 | +export const RejectAppointmentModal = ({ |
| 12 | + appointment, |
| 13 | + isOpen, |
| 14 | + onClose, |
| 15 | +}: RejectAppointmentModalProps) => { |
| 16 | + const [rejectionReason, setRejectionReason] = useState(""); |
| 17 | + const [error, setError] = useState(""); |
| 18 | + const updateAppointmentMutation = useUpdateAppointmentMutation(); |
| 19 | + |
| 20 | + const handleSubmit = async (e: React.FormEvent) => { |
| 21 | + e.preventDefault(); |
| 22 | + |
| 23 | + if (rejectionReason.trim().length < 100) { |
| 24 | + setError("Rejection reason must be at least 100 characters"); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + if (rejectionReason.trim().length > 500) { |
| 29 | + setError("Rejection reason must not exceed 500 characters"); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + try { |
| 34 | + await updateAppointmentMutation.mutateAsync({ |
| 35 | + appointmentId: appointment.id, |
| 36 | + status: "rejected", |
| 37 | + rejectionReason: rejectionReason.trim(), |
| 38 | + }); |
| 39 | + onClose(); |
| 40 | + setRejectionReason(""); |
| 41 | + setError(""); |
| 42 | + } catch { |
| 43 | + setError("Failed to reject appointment. Please try again."); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + const handleClose = () => { |
| 48 | + onClose(); |
| 49 | + setRejectionReason(""); |
| 50 | + setError(""); |
| 51 | + }; |
| 52 | + |
| 53 | + if (!isOpen) return null; |
| 54 | + |
| 55 | + return ( |
| 56 | + <div className="fixed inset-0 backdrop-blur-sm bg-white/5 flex items-center justify-center z-50"> |
| 57 | + <div className="bg-white rounded-lg p-6 w-full max-w-md mx-4"> |
| 58 | + <h2 className="text-xl font-semibold mb-4">Reject Appointment</h2> |
| 59 | + |
| 60 | + <div className="mb-4"> |
| 61 | + <p className="text-sm text-gray-600 mb-2"> |
| 62 | + <strong>Student:</strong> {appointment.studentName} |
| 63 | + </p> |
| 64 | + <p className="text-sm text-gray-600 mb-2"> |
| 65 | + <strong>Lesson:</strong> {appointment.lesson} |
| 66 | + </p> |
| 67 | + <p className="text-sm text-gray-600 mb-4"> |
| 68 | + <strong>Date & Time:</strong> {appointment.date} at{" "} |
| 69 | + {appointment.time} |
| 70 | + </p> |
| 71 | + </div> |
| 72 | + |
| 73 | + <form onSubmit={handleSubmit}> |
| 74 | + <div className="mb-4"> |
| 75 | + <label |
| 76 | + htmlFor="rejectionReason" |
| 77 | + className="block text-sm font-medium text-gray-700 mb-2" |
| 78 | + > |
| 79 | + Reason for Rejection * |
| 80 | + </label> |
| 81 | + <textarea |
| 82 | + id="rejectionReason" |
| 83 | + value={rejectionReason} |
| 84 | + onChange={(e) => setRejectionReason(e.target.value)} |
| 85 | + className="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent resize-none" |
| 86 | + rows={6} |
| 87 | + placeholder="Please explain why you are rejecting this appointment (100-500 characters)..." |
| 88 | + required |
| 89 | + /> |
| 90 | + <div className="flex justify-between text-xs text-gray-500 mt-1"> |
| 91 | + <span>Minimum 100 characters</span> |
| 92 | + <span |
| 93 | + className={rejectionReason.length > 500 ? "text-red-500" : ""} |
| 94 | + > |
| 95 | + {rejectionReason.length}/500 |
| 96 | + </span> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + |
| 100 | + {error && ( |
| 101 | + <div className="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg"> |
| 102 | + <p className="text-sm text-red-700">{error}</p> |
| 103 | + </div> |
| 104 | + )} |
| 105 | + |
| 106 | + <div className="flex gap-3"> |
| 107 | + <button |
| 108 | + type="button" |
| 109 | + onClick={handleClose} |
| 110 | + className="flex-1 px-4 py-2 text-gray-700 bg-gray-100 rounded-lg hover:bg-gray-200 transition-colors" |
| 111 | + disabled={updateAppointmentMutation.isPending} |
| 112 | + > |
| 113 | + Cancel |
| 114 | + </button> |
| 115 | + <button |
| 116 | + type="submit" |
| 117 | + className="flex-1 px-4 py-2 text-white bg-red-600 rounded-lg hover:bg-red-700 transition-colors disabled:opacity-50" |
| 118 | + disabled={ |
| 119 | + updateAppointmentMutation.isPending || |
| 120 | + rejectionReason.trim().length < 100 |
| 121 | + } |
| 122 | + > |
| 123 | + {updateAppointmentMutation.isPending |
| 124 | + ? "Rejecting..." |
| 125 | + : "Reject Appointment"} |
| 126 | + </button> |
| 127 | + </div> |
| 128 | + </form> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + ); |
| 132 | +}; |
0 commit comments