import { type Component, createEffect, createResource, createSignal, For, Show, } from "solid-js"; import { useNavigate } from "@solidjs/router"; import { useAuth } from "@/context/AuthContext"; import { consultationSlotApi } from "@/api/consultationSlot"; import { formatDateWithWeekday, getTodayString } from "@/utils"; import { UserType } from "@/enums/UserType"; const ConsultationSlots: Component = () => { const { user, isAuthenticated } = useAuth(); const navigate = useNavigate(); const [selectedDate, setSelectedDate] = createSignal(""); const [error, setError] = createSignal(""); const [slots, { refetch }] = createResource( () => ({ authenticated: isAuthenticated(), userId: user()?.userId, }), async (params) => { if (!params.authenticated || !params.userId) return null; return await consultationSlotApi.getSlots(params.userId); }, ); createEffect(() => { if (!isAuthenticated()) { navigate("/login", { replace: true }); return; } const currentUser = user(); if (currentUser?.userType !== UserType.THERAPIST) { navigate("/", { replace: true }); } }); const handleAddSlot = async (e: Event) => { e.preventDefault(); const dateStr = selectedDate(); if (!dateStr) { setError("Please select a date"); return; } if (dateStr < getTodayString()) { setError("Cannot add slots for past dates"); return; } const currentUser = user(); if (!currentUser?.userId) return; try { await consultationSlotApi.addSlot(currentUser.userId, dateStr); setSelectedDate(""); setError(""); refetch(); } catch (err: any) { setError(err.message || "Failed to add slot"); } }; const handleRemoveSlot = async (date: string) => { const currentUser = user(); if (!currentUser?.userId) return; if ( !confirm(`Remove consultation slot for ${formatDateWithWeekday(date)}?`) ) { return; } try { await consultationSlotApi.removeSlot(currentUser.userId, date); setError(""); refetch(); } catch (err: any) { setError(err.message || "Failed to remove slot"); } }; const sortedSlots = () => { const slotData = slots(); if (!slotData?.consultationSlots) return []; return [...slotData.consultationSlots].sort(); }; const futureSlots = () => { const today = getTodayString(); return sortedSlots().filter((slot) => slot >= today); }; const pastSlots = () => { const today = getTodayString(); return sortedSlots().filter((slot) => slot < today); }; return (
Add or remove dates when you're available for patient consultations
No upcoming consultation slots scheduled
} >