Skip to content

Commit

Permalink
use swr in getMentorAvailabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanito98 committed Oct 30, 2024
1 parent f9790fe commit 6986b85
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 136 deletions.
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"remark-gfm": "^4.0.0",
"remark-mdx-frontmatter": "^4.0.0",
"sharp": "^0.33.5",
"swr": "^2.2.5",
"tailwindest": "^2.2.1",
"typescript": "5.2.2"
},
Expand Down
224 changes: 148 additions & 76 deletions src/components/mentorias/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,83 @@
import { UserAvailability } from "@/types/mentor.schema";
import useSWR from "swr";
import {
type GetAvailabilitiesRequest,
UserAvailability,
GetAvailabilitiesResponseSchema,
} from "@/types/mentor.schema";
import Calendar from "react-calendar";
import { useState } from "react";
import { InlineWidget } from "react-calendly";
import { Link } from "../link";
import { getLocalDateTimeWithOffset } from "@/utils/time";
import { getLocalDateTimeWithOffset, nextHalfHour } from "@/utils/time";
import { Value } from "@sinclair/typebox/value";

const fetcher = (args: RequestInfo): Promise<unknown> =>
fetch(args).then((res) => res.json());

function useGetMentorAvailabilities({
ofmiEdition,
startTimeISOString,
endTimeISOString,
}: {
ofmiEdition: number;
startTimeISOString: string;
endTimeISOString: string;
}): Array<UserAvailability> | null {
const request: GetAvailabilitiesRequest = {
startTime: startTimeISOString,
endTime: endTimeISOString,
ofmiEdition,
};
const { data } = useSWR(
`/api/volunteer/getMentorAvailabilities?${new URLSearchParams({
...request,
ofmiEdition: request.ofmiEdition.toString(),
}).toString()}`,
fetcher,
);
if (Value.Check(GetAvailabilitiesResponseSchema, data)) {
return data.availabilities;
}
return null;
}

function Loading(): JSX.Element {
return (
<div className="flex justify-center">
<svg
aria-hidden="true"
className="h-8 w-8 animate-spin fill-blue-600 text-gray-200 dark:text-gray-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
);
}

// Receives a list of connected providers
export default function Mentorias({
startTime,
endTime,
availabilities,
ofmiEdition,
}: {
startTime: Date;
endTime: Date;
availabilities: Array<UserAvailability>;
ofmiEdition: number;
}): JSX.Element {
const startTime = nextHalfHour(new Date(Date.now()));
const endTime = new Date(startTime.getTime() + 7 * 24 * 60 * 60 * 1000);
const availabilities = useGetMentorAvailabilities({
ofmiEdition,
startTimeISOString: startTime.toISOString(),
endTimeISOString: endTime.toISOString(),
});
const [selectedDay, setSelectedDay] = useState<Date>();
const [selectedStartTime, setSelectedStartTime] = useState<Date>();
const [schedulingUrlToShow, setSchedulingUrlToShow] = useState<string>();
Expand All @@ -30,18 +93,21 @@ export default function Mentorias({
: schedulingUrlToShow)
);
};
const availableLocalDates = new Set(
availabilities
.map((v) =>
v.availableStartTimes.map((startTime) => {
const time = new Date(startTime);
return time.toLocaleDateString();
}),
)
.flat(),
);
const availableLocalDates =
availabilities &&
new Set(
availabilities
.map((v) =>
v.availableStartTimes.map((startTime) => {
const time = new Date(startTime);
return time.toLocaleDateString();
}),
)
.flat(),
);

const availableLocalStartTimes =
availabilities &&
selectedDay &&
Array.from(
new Set(
Expand All @@ -64,9 +130,10 @@ export default function Mentorias({
.map((v) => new Date(v));

return (
<div className="mx-auto max-w-4xl px-2 pt-4">
<div className="mx-auto max-w-4xl px-2 py-8 md:mt-8 md:px-0">
{/* Filter calendar */}
<div className="grid md:grid-cols-4 md:gap-6">
<h1 className="text-4xl font-bold">Mentorías</h1>
<div className="grid py-6 md:grid-cols-4 md:gap-6">
<div
className="group relative z-0 col-span-3 mb-5 w-full"
hidden={!showFilterCalendar}
Expand All @@ -93,7 +160,10 @@ export default function Mentorias({
if (date.getTime() === selectedDay?.getTime()) {
res.push("react-calendar__tile--activeV2");
}
if (availableLocalDates.has(date.toLocaleDateString())) {
if (
availableLocalDates &&
availableLocalDates.has(date.toLocaleDateString())
) {
res.push("react-calendar__tile--available");
}
return res;
Expand Down Expand Up @@ -148,7 +218,7 @@ export default function Mentorias({
<div className="group relative z-0 mb-5 w-full">
<table
className="w-full table-auto"
hidden={availabilities.length == 0}
hidden={availabilities === null || availabilities.length == 0}
>
<thead>
<tr>
Expand All @@ -157,63 +227,65 @@ export default function Mentorias({
</tr>
</thead>
<tbody>
{availabilities.map(
({
calendlySchedulingUrl,
firstName,
lastName,
availableStartTimes,
}) => {
const hidden =
availableStartTimes.find((v) => {
if (selectedDay === undefined) {
{availabilities &&
availabilities.map(
({
calendlySchedulingUrl,
firstName,
lastName,
availableStartTimes,
}) => {
const hidden =
availableStartTimes.find((v) => {
if (selectedDay === undefined) {
return true;
}
const time = new Date(v);
if (
time.toLocaleDateString() !==
selectedDay.toLocaleDateString()
) {
return false;
}
if (
selectedStartTime &&
selectedStartTime.getTime() !== time.getTime()
) {
return false;
}
return true;
}
const time = new Date(v);
if (
time.toLocaleDateString() !==
selectedDay.toLocaleDateString()
) {
return false;
}
if (
selectedStartTime &&
selectedStartTime.getTime() !== time.getTime()
) {
return false;
}
return true;
}) === undefined;
return (
<tr key={calendlySchedulingUrl} hidden={hidden}>
<td>{`${firstName} ${lastName}`}</td>
<td>
<div className="text-center">
<Link
className="font-light text-blue-600 hover:cursor-pointer hover:underline dark:text-blue-500"
onClick={(ev) => {
ev.preventDefault();
if (
calendlySchedulingUrl === schedulingUrlToShow
) {
setSchedulingUrlToShow(undefined);
} else {
setSchedulingUrlToShow(calendlySchedulingUrl);
}
}}
>
{calendlySchedulingUrl === schedulingUrlToShow
? "Ocultar"
: "Mostrar"}
</Link>
</div>
</td>
</tr>
);
},
)}
}) === undefined;
return (
<tr key={calendlySchedulingUrl} hidden={hidden}>
<td>{`${firstName} ${lastName}`}</td>
<td>
<div className="text-center">
<Link
className="font-light text-blue-600 hover:cursor-pointer hover:underline dark:text-blue-500"
onClick={(ev) => {
ev.preventDefault();
if (
calendlySchedulingUrl === schedulingUrlToShow
) {
setSchedulingUrlToShow(undefined);
} else {
setSchedulingUrlToShow(calendlySchedulingUrl);
}
}}
>
{calendlySchedulingUrl === schedulingUrlToShow
? "Ocultar"
: "Mostrar"}
</Link>
</div>
</td>
</tr>
);
},
)}
</tbody>
</table>
{availabilities === null && <Loading />}
</div>
{schedulingUrlToShow && (
<div className="group relative z-0 mb-5 w-full">
Expand Down
Loading

0 comments on commit 6986b85

Please sign in to comment.