|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useState, useEffect, useCallback } from "react" |
| 4 | +import { Button } from "@/components/ui/button" |
| 5 | +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" |
| 6 | +import { ArrowUpDown, CalendarPlus } from "lucide-react" |
| 7 | +import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog" |
| 8 | +import { Input } from "@/components/ui/input" |
| 9 | +import { Label } from "@/components/ui/label" |
| 10 | +import CoachingSession from "@/components/ui/coaching-session" |
| 11 | +import { useCoachingRelationshipStateStore } from "@/lib/providers/coaching-relationship-state-store-provider" |
| 12 | +import { useCoachingSessionStateStore } from "@/lib/providers/coaching-session-state-store-provider"; |
| 13 | +import { createCoachingSession, useCoachingSessions } from "@/lib/api/coaching-sessions"; |
| 14 | + |
| 15 | + |
| 16 | +export default function CoachingSessionList() { |
| 17 | + const { currentCoachingRelationshipId } = useCoachingRelationshipStateStore((state) => state) |
| 18 | + const { |
| 19 | + coachingSessions, |
| 20 | + isLoading: isLoadingCoachingSessions, |
| 21 | + isError: isErrorCoachingSessions, |
| 22 | + } = useCoachingSessions(currentCoachingRelationshipId); |
| 23 | + const { setCurrentCoachingSessions } = useCoachingSessionStateStore( |
| 24 | + (state) => state |
| 25 | + ); |
| 26 | + |
| 27 | + const [sortByDate, setSortByDate] = useState(true) |
| 28 | + const [isDialogOpen, setIsDialogOpen] = useState(false) |
| 29 | + const [newSessionDate, setNewSessionDate] = useState("") |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + if (!coachingSessions.length) return; |
| 33 | + setCurrentCoachingSessions(coachingSessions); |
| 34 | + }, [coachingSessions]); |
| 35 | + |
| 36 | + const handleCreateSession = (e: React.FormEvent) => { |
| 37 | + e.preventDefault() |
| 38 | + createCoachingSession(currentCoachingRelationshipId, newSessionDate).then((createdCoachingSession) => { |
| 39 | + setIsDialogOpen(false) |
| 40 | + setNewSessionDate("") |
| 41 | + setCurrentCoachingSessions([...coachingSessions, createdCoachingSession]) |
| 42 | + }).catch((err) => { |
| 43 | + console.error("Failed to create new Coaching Session: " + err); |
| 44 | + throw err; |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + const sortedSessions = [...coachingSessions].sort((a, b) => { |
| 49 | + return new Date(b.date).getTime() - new Date(a.date).getTime() |
| 50 | + }) |
| 51 | + |
| 52 | + if (!currentCoachingRelationshipId) return <div>Choose a Coaching Relationship to View Coaching Sessions</div> |
| 53 | + if (isLoadingCoachingSessions) return <div>Loading coaching sessions...</div> |
| 54 | + if (isErrorCoachingSessions) return <div>Error loading coaching sessions</div> |
| 55 | + |
| 56 | + return ( |
| 57 | + <Card className="flex-1"> |
| 58 | + <CardHeader className="space-y-4"> |
| 59 | + <div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4"> |
| 60 | + <CardTitle className="text-xl sm:text-2xl">Coaching Sessions</CardTitle> |
| 61 | + <Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}> |
| 62 | + <DialogTrigger asChild> |
| 63 | + <Button |
| 64 | + variant="outline" |
| 65 | + size="sm" |
| 66 | + className="w-full sm:w-auto" |
| 67 | + disabled={!currentCoachingRelationshipId} |
| 68 | + > |
| 69 | + <CalendarPlus className="mr-2 h-4 w-4" /> |
| 70 | + Create New Coaching Session |
| 71 | + </Button> |
| 72 | + </DialogTrigger> |
| 73 | + <DialogContent> |
| 74 | + <DialogHeader> |
| 75 | + <DialogTitle>Create New Coaching Session</DialogTitle> |
| 76 | + </DialogHeader> |
| 77 | + <form onSubmit={handleCreateSession} className="space-y-4"> |
| 78 | + <div className="space-y-2"> |
| 79 | + <Label htmlFor="session-date">Session Date and Time</Label> |
| 80 | + <Input |
| 81 | + id="session-date" |
| 82 | + type="datetime-local" |
| 83 | + required |
| 84 | + value={newSessionDate} |
| 85 | + onChange={(e) => setNewSessionDate(e.target.value)} |
| 86 | + /> |
| 87 | + </div> |
| 88 | + <Button type="submit">Create Session</Button> |
| 89 | + </form> |
| 90 | + </DialogContent> |
| 91 | + </Dialog> |
| 92 | + </div> |
| 93 | + <div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-2"> |
| 94 | + <Button |
| 95 | + variant="ghost" |
| 96 | + size="sm" |
| 97 | + className="text-muted-foreground w-full sm:w-auto justify-between" |
| 98 | + onClick={() => setSortByDate(true)} |
| 99 | + > |
| 100 | + <span>Date and Time</span> |
| 101 | + <ArrowUpDown className="ml-2 h-4 w-4" /> |
| 102 | + </Button> |
| 103 | + <Button |
| 104 | + variant="ghost" |
| 105 | + size="sm" |
| 106 | + className="text-muted-foreground w-full sm:w-auto justify-between" |
| 107 | + onClick={() => setSortByDate(false)} |
| 108 | + > |
| 109 | + <span>Overarching Goal</span> |
| 110 | + <ArrowUpDown className="ml-2 h-4 w-4" /> |
| 111 | + </Button> |
| 112 | + </div> |
| 113 | + </CardHeader> |
| 114 | + <CardContent> |
| 115 | + <div className="space-y-4"> |
| 116 | + {sortedSessions.map((coachingSession) => ( |
| 117 | + <CoachingSession key={coachingSession.id} coachingSession={coachingSession} /> |
| 118 | + ))} |
| 119 | + </div> |
| 120 | + </CardContent> |
| 121 | + </Card> |
| 122 | + ) |
| 123 | +} |
0 commit comments