|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { useState } from "react"; |
4 | | -import { Button } from "@/components/ui/button"; |
5 | 3 | import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; |
6 | | -import { ArrowUpDown } from "lucide-react"; |
7 | 4 | import { useCoachingRelationshipStateStore } from "@/lib/providers/coaching-relationship-state-store-provider"; |
8 | 5 | import { useCoachingSessionList } from "@/lib/api/coaching-sessions"; |
9 | 6 | import { useCoachingSessionMutation } from "@/lib/api/coaching-sessions"; |
10 | 7 | import { CoachingSession as CoachingSessionComponent } from "@/components/ui/coaching-session"; |
11 | 8 | import { DateTime } from "ts-luxon"; |
12 | | -import type { CoachingSession } from "@/types/coaching-session"; |
13 | | -import { Id } from "@/types/general"; |
| 9 | +import { |
| 10 | + filterAndSortCoachingSessions, |
| 11 | + type CoachingSession, |
| 12 | +} from "@/types/coaching-session"; |
| 13 | +import { Id, SortOrder } from "@/types/general"; |
| 14 | +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 15 | +import CoachingRelationshipSelector from "../coaching-relationship-selector"; |
| 16 | +import { useOrganizationStateStore } from "@/lib/providers/organization-state-store-provider"; |
| 17 | +import { cn } from "@/components/lib/utils"; |
14 | 18 |
|
15 | 19 | interface CoachingSessionListProps { |
| 20 | + className?: string; |
16 | 21 | onUpdateSession: (session: CoachingSession) => void; |
17 | 22 | } |
18 | 23 |
|
19 | | -export default function CoachingSessionList({ onUpdateSession }: CoachingSessionListProps) { |
| 24 | +export default function CoachingSessionList({ |
| 25 | + className, |
| 26 | + onUpdateSession, |
| 27 | +}: CoachingSessionListProps) { |
| 28 | + const { currentOrganizationId } = useOrganizationStateStore((state) => state); |
20 | 29 | const { currentCoachingRelationshipId } = useCoachingRelationshipStateStore( |
21 | 30 | (state) => state |
22 | 31 | ); |
@@ -47,66 +56,107 @@ export default function CoachingSessionList({ onUpdateSession }: CoachingSession |
47 | 56 | } |
48 | 57 | }; |
49 | 58 |
|
50 | | - const [sortByDate, setSortByDate] = useState(true); |
| 59 | + const upcomingSessions = coachingSessions |
| 60 | + ? filterAndSortCoachingSessions(coachingSessions, SortOrder.Ascending, true) |
| 61 | + : []; |
51 | 62 |
|
52 | | - const sortedSessions = coachingSessions |
53 | | - ? [...coachingSessions].sort((a, b) => { |
54 | | - return new Date(b.date).getTime() - new Date(a.date).getTime(); |
55 | | - }) |
| 63 | + const previousSessions = coachingSessions |
| 64 | + ? filterAndSortCoachingSessions( |
| 65 | + coachingSessions, |
| 66 | + SortOrder.Descending, |
| 67 | + false |
| 68 | + ) |
56 | 69 | : []; |
57 | 70 |
|
58 | | - if (isLoadingCoachingSessions) return <div>Loading coaching sessions...</div>; |
59 | | - if (isErrorCoachingSessions) |
60 | | - return <div>Error loading coaching sessions</div>; |
| 71 | + let loadingCoachingSessions = ( |
| 72 | + <div className="flex items-center justify-center py-8"> |
| 73 | + <p className="text-lg text-muted-foreground"> |
| 74 | + Loading your coaching sessions... |
| 75 | + </p> |
| 76 | + </div> |
| 77 | + ); |
| 78 | + |
| 79 | + let noCoachingSessions = ( |
| 80 | + <div className="flex items-center justify-center py-8"> |
| 81 | + <p className="text-lg text-muted-foreground"> |
| 82 | + Select a coaching relationship to view your coaching sessions. |
| 83 | + </p> |
| 84 | + </div> |
| 85 | + ); |
| 86 | + |
| 87 | + let errorLoadingCoachingSessions = ( |
| 88 | + <div className="flex items-center justify-center py-8"> |
| 89 | + <p className="text-lg font-bold"> |
| 90 | + There was an error trying to load your coaching sessions. |
| 91 | + </p> |
| 92 | + </div> |
| 93 | + ); |
61 | 94 |
|
62 | 95 | return ( |
63 | | - <Card className="flex-1"> |
64 | | - <CardHeader className="space-y-4"> |
65 | | - <div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4"> |
66 | | - <CardTitle className="text-xl sm:text-2xl"> |
67 | | - Coaching Sessions |
68 | | - </CardTitle> |
69 | | - </div> |
70 | | - <div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-2"> |
71 | | - <Button |
72 | | - variant="ghost" |
73 | | - size="sm" |
74 | | - className="text-muted-foreground w-full sm:w-auto justify-between" |
75 | | - onClick={() => setSortByDate(true)} |
76 | | - > |
77 | | - <span>Date and Time</span> |
78 | | - <ArrowUpDown className="ml-2 h-4 w-4" /> |
79 | | - </Button> |
80 | | - <Button |
81 | | - variant="ghost" |
82 | | - size="sm" |
83 | | - className="text-muted-foreground w-full sm:w-auto justify-between" |
84 | | - onClick={() => setSortByDate(false)} |
85 | | - > |
86 | | - <span>Overarching Goal</span> |
87 | | - <ArrowUpDown className="ml-2 h-4 w-4" /> |
88 | | - </Button> |
89 | | - </div> |
| 96 | + <Card className={cn("min-w-96", className)}> |
| 97 | + <CardHeader> |
| 98 | + <CardTitle> |
| 99 | + <div className="flex justify-between flex-col lg:flex-row"> |
| 100 | + <div>Coaching Sessions</div> |
| 101 | + <CoachingRelationshipSelector |
| 102 | + className="pt-4 lg:min-w-64" |
| 103 | + organizationId={currentOrganizationId} |
| 104 | + disabled={!currentOrganizationId} |
| 105 | + /> |
| 106 | + </div> |
| 107 | + </CardTitle> |
90 | 108 | </CardHeader> |
91 | 109 | <CardContent> |
92 | | - {!currentCoachingRelationshipId ? ( |
93 | | - <div className="flex items-center justify-center py-8"> |
94 | | - <p className="text-lg text-muted-foreground"> |
95 | | - Choose a Relationship to view Coaching Sessions |
96 | | - </p> |
97 | | - </div> |
98 | | - ) : ( |
99 | | - <div className="space-y-4"> |
100 | | - {sortedSessions.map((coachingSession) => ( |
101 | | - <CoachingSessionComponent |
102 | | - key={coachingSession.id} |
103 | | - coachingSession={coachingSession} |
104 | | - onUpdate={() => onUpdateSession(coachingSession)} |
105 | | - onDelete={() => handleDeleteCoachingSession(coachingSession.id)} |
106 | | - /> |
107 | | - ))} |
108 | | - </div> |
109 | | - )} |
| 110 | + <Tabs defaultValue="upcoming" className="w-full items-start"> |
| 111 | + <TabsList className="grid w-full grid-cols-2"> |
| 112 | + <TabsTrigger value="upcoming">Upcoming</TabsTrigger> |
| 113 | + <TabsTrigger value="previous">Previous</TabsTrigger> |
| 114 | + </TabsList> |
| 115 | + <TabsContent value="upcoming" className="mt-4"> |
| 116 | + {isLoadingCoachingSessions ? ( |
| 117 | + loadingCoachingSessions |
| 118 | + ) : isErrorCoachingSessions ? ( |
| 119 | + errorLoadingCoachingSessions |
| 120 | + ) : !currentCoachingRelationshipId ? ( |
| 121 | + noCoachingSessions |
| 122 | + ) : ( |
| 123 | + <div className="space-y-4"> |
| 124 | + {upcomingSessions.map((coachingSession) => ( |
| 125 | + <CoachingSessionComponent |
| 126 | + key={coachingSession.id} |
| 127 | + coachingSession={coachingSession} |
| 128 | + onUpdate={() => onUpdateSession(coachingSession)} |
| 129 | + onDelete={() => |
| 130 | + handleDeleteCoachingSession(coachingSession.id) |
| 131 | + } |
| 132 | + /> |
| 133 | + ))} |
| 134 | + </div> |
| 135 | + )} |
| 136 | + </TabsContent> |
| 137 | + <TabsContent value="previous" className="mt-4"> |
| 138 | + {isLoadingCoachingSessions ? ( |
| 139 | + loadingCoachingSessions |
| 140 | + ) : isErrorCoachingSessions ? ( |
| 141 | + errorLoadingCoachingSessions |
| 142 | + ) : !currentCoachingRelationshipId ? ( |
| 143 | + noCoachingSessions |
| 144 | + ) : ( |
| 145 | + <div className="space-y-4"> |
| 146 | + {previousSessions.map((coachingSession) => ( |
| 147 | + <CoachingSessionComponent |
| 148 | + key={coachingSession.id} |
| 149 | + coachingSession={coachingSession} |
| 150 | + onUpdate={() => onUpdateSession(coachingSession)} |
| 151 | + onDelete={() => |
| 152 | + handleDeleteCoachingSession(coachingSession.id) |
| 153 | + } |
| 154 | + /> |
| 155 | + ))} |
| 156 | + </div> |
| 157 | + )} |
| 158 | + </TabsContent> |
| 159 | + </Tabs> |
110 | 160 | </CardContent> |
111 | 161 | </Card> |
112 | 162 | ); |
|
0 commit comments