|
1 |
| -import React, { useState } from 'react'; |
2 |
| -import { useAppStateStore } from '@/lib/providers/app-state-store-provider'; |
| 1 | +import React, { useState } from "react"; |
| 2 | +import { useAppStateStore } from "@/lib/providers/app-state-store-provider"; |
3 | 3 | import { Id } from "@/types/general";
|
4 |
| -import { DynamicApiSelect } from './dynamic-api-select'; |
| 4 | +import { DynamicApiSelect } from "./dynamic-api-select"; |
5 | 5 | import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
6 |
| -import { Organization } from '@/types/organization'; |
7 |
| -import { CoachingRelationshipWithUserNames } from '@/types/coaching_relationship_with_user_names'; |
8 |
| -import { CoachingSession } from '@/types/coaching-session'; |
9 |
| -import { DateTime } from 'ts-luxon'; |
| 6 | +import { Organization } from "@/types/organization"; |
| 7 | +import { CoachingRelationshipWithUserNames } from "@/types/coaching_relationship_with_user_names"; |
| 8 | +import { |
| 9 | + CoachingSession, |
| 10 | + getCoachingSessionById, |
| 11 | +} from "@/types/coaching-session"; |
| 12 | +import { DateTime } from "ts-luxon"; |
10 | 13 | import { Label } from "@/components/ui/label";
|
11 |
| -import { Button } from '../button'; |
12 |
| -import Link from 'next/link'; |
13 |
| - |
| 14 | +import { Button } from "../button"; |
| 15 | +import Link from "next/link"; |
| 16 | +import { fetchOrganization } from "@/lib/api/organizations"; |
| 17 | +import { fetchCoachingRelationshipWithUserNames } from "@/lib/api/coaching-relationships"; |
| 18 | +import { fetchCoachingSessions } from "@/lib/api/coaching-sessions"; |
14 | 19 |
|
15 | 20 | export interface CoachingSessionCardProps {
|
16 | 21 | userId: Id;
|
17 | 22 | }
|
18 | 23 |
|
19 |
| -export function JoinCoachingSession({ userId: userId }: CoachingSessionCardProps) { |
20 |
| - const setOrganizationId = useAppStateStore(state => state.setOrganizationId); |
21 |
| - const setRelationshipId = useAppStateStore(state => state.setRelationshipId); |
22 |
| - const setCoachingSessionId = useAppStateStore(state => state.setCoachingSessionId); |
23 |
| - const [organizationId, setOrganization] = useState<string | null>(null); |
24 |
| - const [relationshipId, setRelationship] = useState<string | null>(null); |
25 |
| - const [sessionId, setSessions] = useState<string | null>(null); |
| 24 | +export function JoinCoachingSession({ |
| 25 | + userId: userId, |
| 26 | +}: CoachingSessionCardProps) { |
| 27 | + const { organizationId, setOrganizationId } = useAppStateStore((state) => ({ |
| 28 | + organizationId: state.organizationId, |
| 29 | + setOrganizationId: state.setOrganizationId, |
| 30 | + })); |
| 31 | + const { relationshipId, setRelationshipId } = useAppStateStore((state) => ({ |
| 32 | + relationshipId: state.setRelationshipId, |
| 33 | + setRelationshipId: state.setRelationshipId, |
| 34 | + })); |
| 35 | + const { coachingSessionId, setCoachingSessionId } = useAppStateStore( |
| 36 | + (state) => ({ |
| 37 | + coachingSessionId: state.coachingSessionId, |
| 38 | + setCoachingSessionId: state.setCoachingSessionId, |
| 39 | + }) |
| 40 | + ); |
| 41 | + const { organization, setOrganization } = useAppStateStore((state) => ({ |
| 42 | + organization: state.organization, |
| 43 | + setOrganization: state.setOrganization, |
| 44 | + })); |
| 45 | + const { relationship, setRelationship } = useAppStateStore((state) => ({ |
| 46 | + relationship: state.coachingRelationship, |
| 47 | + setRelationship: state.setCoachingRelationship, |
| 48 | + })); |
| 49 | + const { coachingSession, setCoachingSession } = useAppStateStore((state) => ({ |
| 50 | + coachingSession: state.coachingSession, |
| 51 | + setCoachingSession: state.setCoachingSession, |
| 52 | + })); |
26 | 53 | const FROM_DATE = DateTime.now().minus({ month: 1 }).toISODate();
|
27 | 54 | const TO_DATE = DateTime.now().plus({ month: 1 }).toISODate();
|
28 | 55 |
|
29 |
| - const handleOrganizationSelection = (value: string) => { |
30 |
| - setOrganization(value); |
31 |
| - setRelationship(null); |
32 |
| - setSessions(null); |
33 |
| - setOrganizationId(value); |
34 |
| - } |
35 |
| - |
36 |
| - const handleRelationshipSelection = (value: string) => { |
37 |
| - setRelationship(value); |
38 |
| - setSessions(null); |
39 |
| - setRelationshipId(value); |
40 |
| - } |
| 56 | + const handleOrganizationSelection = (id: Id) => { |
| 57 | + fetchOrganization(id) |
| 58 | + .then((organization) => { |
| 59 | + setOrganizationId(organization.id); |
| 60 | + setOrganization(organization); |
| 61 | + }) |
| 62 | + .catch((err) => { |
| 63 | + console.error("Failed to retrieve and set organization: " + err); |
| 64 | + }); |
| 65 | + }; |
41 | 66 |
|
42 |
| - const handleSessionSelection = (value: string) => { |
43 |
| - setSessions(value); |
44 |
| - setCoachingSessionId(value); |
45 |
| - } |
| 67 | + const handleRelationshipSelection = (id: Id) => { |
| 68 | + fetchCoachingRelationshipWithUserNames(organization.id, id) |
| 69 | + .then((relationship) => { |
| 70 | + setRelationshipId(relationship.id); |
| 71 | + setRelationship(relationship); |
| 72 | + }) |
| 73 | + .catch((err) => { |
| 74 | + console.error("Failed to retrieve and set relationship: " + err); |
| 75 | + }); |
| 76 | + }; |
46 | 77 |
|
| 78 | + const handleSessionSelection = (id: Id) => { |
| 79 | + fetchCoachingSessions(relationship.id) |
| 80 | + .then((sessions) => { |
| 81 | + const session = getCoachingSessionById(id, sessions); |
| 82 | + setCoachingSessionId(session.id); |
| 83 | + setCoachingSession(session); |
| 84 | + }) |
| 85 | + .catch((err) => { |
| 86 | + console.error("Failed to retrieve and set relationship: " + err); |
| 87 | + }); |
| 88 | + }; |
47 | 89 |
|
48 | 90 | return (
|
49 | 91 | <Card className="w-[300px]">
|
50 | 92 | <CardHeader>
|
51 | 93 | <CardTitle>Join a Coaching Session</CardTitle>
|
52 | 94 | </CardHeader>
|
53 |
| - <CardContent className='grid gap-6'> |
| 95 | + <CardContent className="grid gap-6"> |
54 | 96 | <div className="grid gap-2">
|
55 |
| - <Label htmlFor='organization-selector'>Organization</Label> |
| 97 | + <Label htmlFor="organization-selector">Organization</Label> |
56 | 98 |
|
57 | 99 | <DynamicApiSelect<Organization>
|
58 | 100 | url="/organizations"
|
59 | 101 | params={{ userId }}
|
60 | 102 | onChange={handleOrganizationSelection}
|
61 | 103 | placeholder="Select an organization"
|
62 | 104 | getOptionLabel={(org) => org.name}
|
63 |
| - getOptionValue={(org) => org.id.toString()} |
64 |
| - elementId='organization-selector' |
| 105 | + getOptionValue={(org) => org.id} // FIXME: this doesn't seem to display the currently selected organization when the page loads and the org.id is set |
| 106 | + elementId="organization-selector" |
65 | 107 | />
|
66 | 108 | </div>
|
67 |
| - {organizationId && ( |
| 109 | + {organization.id.length > 0 && ( |
68 | 110 | <div className="grid gap-2">
|
69 |
| - <Label htmlFor='relationship-selector'>Relationship</Label> |
| 111 | + <Label htmlFor="relationship-selector">Relationship</Label> |
70 | 112 |
|
71 | 113 | <DynamicApiSelect<CoachingRelationshipWithUserNames>
|
72 | 114 | url={`/organizations/${organizationId}/coaching_relationships`}
|
73 | 115 | params={{ organizationId }}
|
74 | 116 | onChange={handleRelationshipSelection}
|
75 | 117 | placeholder="Select coaching relationship"
|
76 |
| - getOptionLabel={ |
77 |
| - (relationship) => |
78 |
| - `${relationship.coach_first_name} ${relationship.coach_last_name} -> ${relationship.coachee_first_name} ${relationship.coach_last_name}` |
| 118 | + getOptionLabel={(relationship) => |
| 119 | + `${relationship.coach_first_name} ${relationship.coach_last_name} -> ${relationship.coachee_first_name} ${relationship.coach_last_name}` |
79 | 120 | }
|
80 |
| - getOptionValue={(relationship) => relationship.id.toString()} |
81 |
| - elementId='relationship-selector' |
| 121 | + getOptionValue={(relationship) => relationship.id} |
| 122 | + elementId="relationship-selector" |
82 | 123 | />
|
83 | 124 | </div>
|
84 | 125 | )}
|
85 |
| - {relationshipId && ( |
| 126 | + {relationship.id.length > 0 && ( |
86 | 127 | <div className="grid gap-2">
|
87 |
| - <Label htmlFor='session-selector'>Coaching Session</Label> |
| 128 | + <Label htmlFor="session-selector">Coaching Session</Label> |
88 | 129 |
|
89 | 130 | <DynamicApiSelect<CoachingSession>
|
90 | 131 | url="/coaching_sessions"
|
91 | 132 | params={{
|
92 |
| - coaching_relationship_id: relationshipId, |
| 133 | + coaching_relationship_id: relationship.id, |
93 | 134 | from_date: FROM_DATE,
|
94 |
| - to_Date: TO_DATE |
| 135 | + to_Date: TO_DATE, |
95 | 136 | }}
|
96 | 137 | onChange={handleSessionSelection}
|
97 | 138 | placeholder="Select coaching session"
|
98 |
| - getOptionLabel={(session) => session.date.toString()} |
99 |
| - getOptionValue={(session) => session.id.toString()} |
100 |
| - elementId='session-selector' |
| 139 | + getOptionLabel={(session) => session.date} |
| 140 | + getOptionValue={(session) => session.id} |
| 141 | + elementId="session-selector" |
101 | 142 | groupByDate={true}
|
102 | 143 | />
|
103 | 144 | </div>
|
104 | 145 | )}
|
105 |
| - {sessionId && ( |
106 |
| - <div className='grid gap-2'> |
107 |
| - <Button variant='outline' className='w-full'> |
108 |
| - <Link href={`/coaching-sessions/${sessionId}`}> |
| 146 | + {coachingSession.id.length > 0 && ( |
| 147 | + <div className="grid gap-2"> |
| 148 | + <Button variant="outline" className="w-full"> |
| 149 | + <Link href={`/coaching-sessions/${coachingSessionId}`}> |
109 | 150 | Join Session
|
110 | 151 | </Link>
|
111 | 152 | </Button>
|
112 | 153 | </div>
|
113 | 154 | )}
|
114 | 155 | </CardContent>
|
115 | 156 | </Card>
|
116 |
| - ) |
| 157 | + ); |
117 | 158 | }
|
0 commit comments