-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoaching-session-selector.tsx
109 lines (102 loc) · 3.13 KB
/
coaching-session-selector.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"use client";
import * as React from "react";
import { PopoverProps } from "@radix-ui/react-popover";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import "@/styles/code-block.scss";
import {
CoachingSession,
coachingSessionToString,
getCoachingSessionById,
} from "@/types/coaching-session";
import { getDateTimeFromString, Id } from "@/types/general";
import { DateTime } from "ts-luxon";
import { useAppStateStore } from "@/lib/providers/app-state-store-provider";
interface CoachingSessionSelectorProps extends PopoverProps {
sessions: CoachingSession[];
placeholder: string;
onSelect: (session: Id) => void;
}
export function CoachingSessionSelector({
sessions,
placeholder,
onSelect,
...props
}: CoachingSessionSelectorProps) {
const { coachingSessionId, setCoachingSessionId } = useAppStateStore(
(state) => state
);
const { setCoachingSession } = useAppStateStore((state) => state);
const handleSetCoachingSession = (coachingSessionId: string) => {
setCoachingSessionId(coachingSessionId);
const coachingSession = getCoachingSessionById(coachingSessionId, sessions);
console.debug(
"coachingSession: " + coachingSessionToString(coachingSession)
);
setCoachingSession(coachingSession);
onSelect(coachingSessionId);
};
return (
<Select
defaultValue="today"
value={coachingSessionId}
onValueChange={handleSetCoachingSession}
>
<SelectTrigger id="session">
<SelectValue placeholder={placeholder} />
</SelectTrigger>
<SelectContent>
{sessions.some(
(session) => getDateTimeFromString(session.date) < DateTime.now()
) && (
<SelectGroup>
<SelectLabel>Previous Sessions</SelectLabel>
{sessions
.filter(
(session) =>
getDateTimeFromString(session.date) < DateTime.now()
)
.map((session) => (
<SelectItem value={session.id} key={session.id}>
{getDateTimeFromString(session.date).toLocaleString(
DateTime.DATETIME_FULL
)}
</SelectItem>
))}
</SelectGroup>
)}
{sessions.some(
(session) => getDateTimeFromString(session.date) >= DateTime.now()
) && (
<SelectGroup>
<SelectLabel>Upcoming Sessions</SelectLabel>
{sessions
.filter(
(session) =>
getDateTimeFromString(session.date) >= DateTime.now()
)
.map((session) => (
<SelectItem value={session.id} key={session.id}>
{getDateTimeFromString(session.date).toLocaleString(
DateTime.DATETIME_FULL
)}
</SelectItem>
))}
</SelectGroup>
)}
{sessions.length == 0 && (
<SelectItem disabled={true} value="none">
None found
</SelectItem>
)}
</SelectContent>
</Select>
);
}