Skip to content

Commit a96596a

Browse files
authored
Merge pull request #36 from Jim-Hodapp-Coaching/store_full_organization_in_app_state_store
2 parents f0e3bb0 + 6772772 commit a96596a

File tree

4 files changed

+98
-61
lines changed

4 files changed

+98
-61
lines changed

src/components/ui/dashboard/select-coaching-session.tsx

+14-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ import {
3434
getCoachingRelationshipById,
3535
} from "@/types/coaching_relationship_with_user_names";
3636
import { getDateTimeFromString, Id } from "@/types/general";
37-
import { Organization } from "@/types/organization";
37+
import {
38+
getOrganizationById,
39+
Organization,
40+
organizationToString,
41+
} from "@/types/organization";
3842
import Link from "next/link";
3943
import { useEffect, useState } from "react";
4044
import { DateTime } from "ts-luxon";
@@ -51,6 +55,7 @@ export function SelectCoachingSession({
5155
const { organizationId, setOrganizationId } = useAppStateStore(
5256
(state) => state
5357
);
58+
const { organization, setOrganization } = useAppStateStore((state) => state);
5459
const { relationshipId, setRelationshipId } = useAppStateStore(
5560
(state) => state
5661
);
@@ -129,6 +134,13 @@ export function SelectCoachingSession({
129134
loadCoachingSessions();
130135
}, [relationshipId]);
131136

137+
const handleSetOrganization = (organizationId: Id) => {
138+
setOrganizationId(organizationId);
139+
const organization = getOrganizationById(organizationId, organizations);
140+
console.debug("organization: " + organizationToString(organization));
141+
setOrganization(organization);
142+
};
143+
132144
const handleSetCoachingRelationship = (coachingRelationshipId: string) => {
133145
setRelationshipId(coachingRelationshipId);
134146
const coachingRelationship = getCoachingRelationshipById(
@@ -168,7 +180,7 @@ export function SelectCoachingSession({
168180
<Select
169181
defaultValue="0"
170182
value={organizationId}
171-
onValueChange={setOrganizationId}
183+
onValueChange={handleSetOrganization}
172184
>
173185
<SelectTrigger id="organization">
174186
<SelectValue placeholder="Select organization" />

src/components/ui/main-nav-menu.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ import {
4949
PopoverContent,
5050
PopoverTrigger,
5151
} from "@/components/ui/popover";
52+
import { useAppStateStore } from "@/lib/providers/app-state-store-provider";
5253

5354
export function MainNavMenu() {
5455
const [open, setIsOpen] = React.useState(false);
56+
const { organization } = useAppStateStore((state) => state);
5557

5658
return (
5759
<>
@@ -60,8 +62,8 @@ export function MainNavMenu() {
6062
<NavigationMenuItem>
6163
<NavigationMenuTrigger>
6264
<span className="hidden font-bold sm:inline-block">
63-
{/* TODO: Replace this with currently selected organization's name */}
64-
Refactor Coaching
65+
{/* FIXME: Replace "Refactor" with something from siteConfig or something else */}
66+
{organization.name.length > 0 ? organization.name : "Refactor"}
6567
</span>
6668
</NavigationMenuTrigger>
6769
<NavigationMenuContent>

src/lib/stores/app-state-store.ts

+70-57
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,83 @@
1-
import { CoachingSession, defaultCoachingSession } from '@/types/coaching-session';
2-
import { CoachingRelationshipWithUserNames, defaultCoachingRelationshipWithUserNames } from '@/types/coaching_relationship_with_user_names';
3-
import { Id } from '@/types/general';
4-
import { create } from 'zustand';
5-
import { createJSONStorage, devtools, persist } from 'zustand/middleware';
1+
import {
2+
CoachingSession,
3+
defaultCoachingSession,
4+
} from "@/types/coaching-session";
5+
import {
6+
CoachingRelationshipWithUserNames,
7+
defaultCoachingRelationshipWithUserNames,
8+
} from "@/types/coaching_relationship_with_user_names";
9+
import { Id } from "@/types/general";
10+
import { defaultOrganization, Organization } from "@/types/organization";
11+
import { create } from "zustand";
12+
import { createJSONStorage, devtools, persist } from "zustand/middleware";
613

714
interface AppState {
8-
organizationId: Id;
9-
relationshipId: Id;
10-
coachingSessionId: Id;
11-
coachingSession: CoachingSession;
12-
coachingRelationship: CoachingRelationshipWithUserNames;
15+
organizationId: Id;
16+
relationshipId: Id;
17+
coachingSessionId: Id;
18+
organization: Organization;
19+
coachingSession: CoachingSession;
20+
coachingRelationship: CoachingRelationshipWithUserNames;
1321
}
1422

1523
interface AppStateActions {
16-
setOrganizationId: (organizationId: Id) => void;
17-
setRelationshipId: (relationshipId: Id) => void;
18-
setCoachingSessionId: (coachingSessionId: Id) => void;
19-
setCoachingSession: (coachingSession: CoachingSession) => void;
20-
setCoachingRelationship: (coachingRelationship: CoachingRelationshipWithUserNames) => void;
21-
reset (): void;
24+
setOrganizationId: (organizationId: Id) => void;
25+
setRelationshipId: (relationshipId: Id) => void;
26+
setCoachingSessionId: (coachingSessionId: Id) => void;
27+
setOrganization: (organization: Organization) => void;
28+
setCoachingSession: (coachingSession: CoachingSession) => void;
29+
setCoachingRelationship: (
30+
coachingRelationship: CoachingRelationshipWithUserNames
31+
) => void;
32+
reset(): void;
2233
}
2334

2435
export type AppStateStore = AppState & AppStateActions;
2536

2637
export const defaultInitState: AppState = {
27-
organizationId: "",
28-
relationshipId: "",
29-
coachingSessionId: "",
30-
coachingSession: defaultCoachingSession(),
31-
coachingRelationship: defaultCoachingRelationshipWithUserNames(),
32-
}
38+
organizationId: "",
39+
relationshipId: "",
40+
coachingSessionId: "",
41+
organization: defaultOrganization(),
42+
coachingSession: defaultCoachingSession(),
43+
coachingRelationship: defaultCoachingRelationshipWithUserNames(),
44+
};
3345

34-
export const createAppStateStore = (
35-
initState: AppState = defaultInitState,
36-
) => {
37-
const appStateStore = create<AppStateStore>()(
38-
devtools(
39-
persist(
40-
(set) => ({
41-
... initState,
46+
export const createAppStateStore = (initState: AppState = defaultInitState) => {
47+
const appStateStore = create<AppStateStore>()(
48+
devtools(
49+
persist(
50+
(set) => ({
51+
...initState,
4252

43-
setOrganizationId: (organizationId) => {
44-
set({ organizationId });
45-
},
46-
setRelationshipId: (relationshipId) => {
47-
set({ relationshipId });
48-
},
49-
setCoachingSessionId: (coachingSessionId) => {
50-
set({ coachingSessionId });
51-
},
52-
setCoachingSession: (coachingSession) => {
53-
set({ coachingSession });
54-
},
55-
setCoachingRelationship: (coachingRelationship) => {
56-
set({ coachingRelationship });
57-
},
58-
reset (): void {
59-
set(defaultInitState);
60-
}
61-
}),
62-
{
63-
name: 'app-state-store',
64-
storage: createJSONStorage(() => sessionStorage),
65-
}
66-
)
67-
)
53+
setOrganizationId: (organizationId) => {
54+
set({ organizationId });
55+
},
56+
setRelationshipId: (relationshipId) => {
57+
set({ relationshipId });
58+
},
59+
setCoachingSessionId: (coachingSessionId) => {
60+
set({ coachingSessionId });
61+
},
62+
setOrganization: (organization) => {
63+
set({ organization });
64+
},
65+
setCoachingSession: (coachingSession) => {
66+
set({ coachingSession });
67+
},
68+
setCoachingRelationship: (coachingRelationship) => {
69+
set({ coachingRelationship });
70+
},
71+
reset(): void {
72+
set(defaultInitState);
73+
},
74+
}),
75+
{
76+
name: "app-state-store",
77+
storage: createJSONStorage(() => sessionStorage),
78+
}
79+
)
6880
)
69-
return appStateStore;
70-
}
81+
);
82+
return appStateStore;
83+
};

src/types/organization.ts

+10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ export function isOrganizationsArray(value: unknown): value is Organization[] {
3030
return Array.isArray(value) && value.every(isOrganization);
3131
}
3232

33+
export function getOrganizationById(
34+
id: string,
35+
organizations: Organization[]
36+
): Organization {
37+
const organization = organizations.find(
38+
(organization) => organization.id === id
39+
);
40+
return organization ? organization : defaultOrganization();
41+
}
42+
3343
export function defaultOrganization(): Organization {
3444
var now = DateTime.now();
3545
return {

0 commit comments

Comments
 (0)