Skip to content

Commit

Permalink
Moving to using useSWR for the ServicesAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
allister-grange committed Dec 27, 2023
1 parent b09f185 commit 298161e
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 247 deletions.
4 changes: 2 additions & 2 deletions src/components/ServicesMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "react-leaflet";

interface ServiceMapProps {
services: ServiceContainer;
services?: ServiceContainer;
city: string;
}

Expand Down Expand Up @@ -124,7 +124,7 @@ const ServicesMap: React.FC<ServiceMapProps> = ({
/>
<ZoomControl position="topright" />
<ChangeMapView coords={centerLatLong!} />
{services.allServices.map((service: Service, index) =>
{services?.allServices.map((service: Service, index) =>
getMapMarker(service, index)
)}
</MapContainer>
Expand Down
16 changes: 9 additions & 7 deletions src/components/Timetable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import convertSecondsToMinutes from "@/helpers/convertors";
import { useTable, useSortBy, usePagination } from "react-table";

interface TimetableProps {
serviceDataToDisplay: Service[];
serviceDataToDisplay?: Service[];
}

interface DisplayServiceData {
Expand All @@ -17,15 +17,17 @@ interface DisplayServiceData {
export const Timetable: React.FC<TimetableProps> = ({
serviceDataToDisplay,
}) => {
const data: DisplayServiceData[] = React.useMemo(
() =>
serviceDataToDisplay.map((service) => ({
const data: DisplayServiceData[] = React.useMemo(() => {
if (serviceDataToDisplay) {
return serviceDataToDisplay.map((service) => ({
delay: convertSecondsToMinutes(service.delay, true),
routeLongName: service.routeLongName,
routeShortName: service.routeShortName,
})),
[serviceDataToDisplay]
);
}));
} else {
return [];
}
}, [serviceDataToDisplay]);

const columns = React.useMemo(
() => [
Expand Down
14 changes: 7 additions & 7 deletions src/components/Toast.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Action } from "@/hooks/useServiceApi";
import React from "react";
import React, { useRef } from "react";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";

interface ToastProps {
error?: boolean;
dispatch: React.Dispatch<Action>;
}

export const Toast: React.FC<ToastProps> = ({ error, dispatch }) => {
if (error) {
toast.info(
export const Toast: React.FC<ToastProps> = ({ error }) => {
const shownToast = useRef(false);

if (error && !shownToast.current) {
toast.error(
"There's an error with either my API or Metlink's, please try again later",
{
position: "top-center",
Expand All @@ -22,7 +22,7 @@ export const Toast: React.FC<ToastProps> = ({ error, dispatch }) => {
progress: undefined,
}
);
dispatch({ type: "REJECTED", error: false });
shownToast.current = true;
}

return <ToastContainer limit={1} style={{ fontSize: "1.8rem" }} />;
Expand Down
59 changes: 59 additions & 0 deletions src/helpers/sorters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ServiceContainer, Service } from "@/types/ServiceTypes";

export const sortServicesByRoute = (
service: ServiceContainer
): ServiceContainer => {
const sort = (serviceArray: Service[]) => {
serviceArray.sort((a, b) => {
if (!a.routeShortName || !b.routeShortName) {
return Number.MAX_VALUE;
}
return a.routeShortName.localeCompare(b.routeShortName, undefined, {
numeric: true,
sensitivity: "base",
});
});
};
sort(service.allServices);
sort(service.cancelledServices);
sort(service.earlyServices);
sort(service.lateServices);
sort(service.onTimeServices);
sort(service.unknownServices);

return service;
};

export const sortServicesResponseByStatus = (data: Service[]) => {
const serviceHolder: ServiceContainer = {
cancelledServices: [],
earlyServices: [],
onTimeServices: [],
unknownServices: [],
allServices: [],
lateServices: [],
};

for (let i = 0; i < data.length; i += 1) {
const service = data[i];
service.vehicleId = service.vehicle_id;
if (service.status === "EARLY") {
serviceHolder.earlyServices.push(service);
} else if (service.status === "LATE") {
serviceHolder.lateServices.push(service);
} else if (service.status === "ONTIME") {
serviceHolder.onTimeServices.push(service);
} else if (service.status === "UNKNOWN") {
serviceHolder.unknownServices.push(service);
} else if (service.status === "CANCELLED") {
serviceHolder.cancelledServices.push(service);
}

if (service.status !== "CANCELLED") {
serviceHolder.allServices.push(service);
}
}

const sortedServices = sortServicesByRoute(serviceHolder);
return sortedServices;
};
29 changes: 0 additions & 29 deletions src/hooks/fetchData.ts

This file was deleted.

196 changes: 0 additions & 196 deletions src/hooks/useServiceApi.ts

This file was deleted.

Loading

1 comment on commit 298161e

@vercel
Copy link

@vercel vercel bot commented on 298161e Dec 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.