Skip to content

Commit

Permalink
Remove fetch functions from JoinCoachingSession
Browse files Browse the repository at this point in the history
  • Loading branch information
qafui committed Oct 7, 2024
1 parent 3971ef0 commit 1349240
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 62 deletions.
1 change: 1 addition & 0 deletions src/components/ui/dashboard/dynamic-api-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function DynamicApiSelect<T>({
isLoading,
error,
} = useApiData<ApiResponse<T>>(url, { method, params });

const [value, setValue] = useState<string>("");

const handleValueChange = (newValue: string) => {
Expand Down
103 changes: 41 additions & 62 deletions src/components/ui/dashboard/join-coaching-session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import { Organization } from "@/types/organization";
import { CoachingRelationshipWithUserNames } from "@/types/coaching_relationship_with_user_names";
import {
CoachingSession,
getCoachingSessionById,
} from "@/types/coaching-session";
import { DateTime } from "ts-luxon";
import { Label } from "@/components/ui/label";
import { Button } from "../button";
import Link from "next/link";
import { fetchOrganization } from "@/lib/api/organizations";
import { fetchCoachingRelationshipWithUserNames } from "@/lib/api/coaching-relationships";
import { fetchCoachingSessions } from "@/lib/api/coaching-sessions";

export interface CoachingSessionCardProps {
userId: Id;
Expand All @@ -24,63 +20,46 @@ export interface CoachingSessionCardProps {
export function JoinCoachingSession({
userId: userId,
}: CoachingSessionCardProps) {
const { setOrganizationId } = useAppStateStore((state) => ({

const FROM_DATE = DateTime.now().minus({ month: 1 }).toISODate();
const TO_DATE = DateTime.now().plus({ month: 1 }).toISODate();

const {
organization,
organizationId,
relationship,
relationshipId,
coachingSession,
coachingSessionId,
setOrganizationId,
setRelationshipId,
setCoachingSessionId
} = useAppStateStore(state => ({
organization: state.setOrganization,
organizationId: state.organizationId,
relationship: state.setCoachingRelationship,
relationshipId: state.relationshipId,
coachingSession: state.setCoachingSession,
coachingSessionId: state.coachingSessionId,
setOrganizationId: state.setOrganizationId,
}));
const { setRelationshipId } = useAppStateStore((state) => ({
setRelationshipId: state.setRelationshipId,
setCoachingSessionId: state.setCoachingSessionId
}));
const { setCoachingSessionId } = useAppStateStore((state) => ({
setCoachingSessionId: state.setCoachingSessionId,
}));
const { organization, setOrganization } = useAppStateStore((state) => ({
organization: state.organization,
setOrganization: state.setOrganization,
}));
const { relationship, setRelationship } = useAppStateStore((state) => ({
relationship: state.coachingRelationship,
setRelationship: state.setCoachingRelationship,
}));
const { coachingSession, setCoachingSession } = useAppStateStore((state) => ({
coachingSession: state.coachingSession,
setCoachingSession: state.setCoachingSession,
}));
const FROM_DATE = DateTime.now().minus({ month: 1 }).toISODate();
const TO_DATE = DateTime.now().plus({ month: 1 }).toISODate();

const handleOrganizationSelection = (id: Id) => {
fetchOrganization(id)
.then((organization) => {
setOrganizationId(organization.id);
setOrganization(organization);
})
.catch((err) => {
console.error("Failed to retrieve and set organization: " + err);
});
};
const handleOrganizationSelection = (value: string) => {
setOrganizationId(value);
setRelationshipId;
setCoachingSessionId;
}

const handleRelationshipSelection = (id: Id) => {
fetchCoachingRelationshipWithUserNames(organization.id, id)
.then((relationship) => {
setRelationshipId(relationship.id);
setRelationship(relationship);
})
.catch((err) => {
console.error("Failed to retrieve and set relationship: " + err);
});
};
const handleRelationshipSelection = (value: string) => {
setRelationshipId(value);
setCoachingSessionId;
}

const handleSessionSelection = (id: Id) => {
fetchCoachingSessions(relationship.id)
.then((sessions) => {
const session = getCoachingSessionById(id, sessions);
setCoachingSessionId(session.id);
setCoachingSession(session);
})
.catch((err) => {
console.error("Failed to retrieve and set relationship: " + err);
});
};
const handleSessionSelection = (value: string) => {
setCoachingSessionId(value);
}

return (
<Card className="w-[300px]">
Expand All @@ -101,13 +80,13 @@ export function JoinCoachingSession({
elementId="organization-selector"
/>
</div>
{organization.id.length > 0 && (
{organizationId.length > 0 && (
<div className="grid gap-2">
<Label htmlFor="relationship-selector">Relationship</Label>

<DynamicApiSelect<CoachingRelationshipWithUserNames>
url={`/organizations/${organization.id}/coaching_relationships`}
params={{ organizationId: organization.id }}
url={`/organizations/${organizationId}/coaching_relationships`}
params={{ organizationId: organizationId }}
onChange={handleRelationshipSelection}
placeholder="Select coaching relationship"
getOptionLabel={(relationship) =>
Expand All @@ -118,14 +97,14 @@ export function JoinCoachingSession({
/>
</div>
)}
{relationship.id.length > 0 && (
{relationshipId.length > 0 && (
<div className="grid gap-2">
<Label htmlFor="session-selector">Coaching Session</Label>

<DynamicApiSelect<CoachingSession>
url="/coaching_sessions"
params={{
coaching_relationship_id: relationship.id,
coaching_relationship_id: relationshipId,
from_date: FROM_DATE,
to_Date: TO_DATE,
}}
Expand All @@ -138,10 +117,10 @@ export function JoinCoachingSession({
/>
</div>
)}
{coachingSession.id.length > 0 && (
{coachingSessionId.length > 0 && (
<div className="grid gap-2">
<Button variant="outline" className="w-full">
<Link href={`/coaching-sessions/${coachingSession.id}`}>
<Link href={`/coaching-sessions/${coachingSessionId}`}>
Join Session
</Link>
</Button>
Expand Down

0 comments on commit 1349240

Please sign in to comment.