Skip to content

Commit

Permalink
chore: generalize PaMessageForm
Browse files Browse the repository at this point in the history
Moves form component for PA messages from `NewPaMessage` into
`PaMessageForm` so that it can be used to make an `EditPaMessage`
component later on.
  • Loading branch information
sloanelybutsurely committed Aug 20, 2024
1 parent 635a43b commit 64be6c0
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 180 deletions.
173 changes: 3 additions & 170 deletions assets/js/components/Dashboard/NewPaMessage/NewPaMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,175 +1,8 @@
import React, { useState } from "react";
import moment, { type Moment } from "moment";
import { Page } from "./types";
import NewPaMessagePage from "./NewPaMessagePage";
import AssociateAlertPage from "./AssociateAlertPage";
import { Alert, InformedEntity } from "Models/alert";
import SelectStationsAndZones from "./StationsAndZones/SelectStationsAndZones";
import { usePlacesWithPaEss } from "Hooks/usePlacesWithPaEss";
import ErrorToast from "Components/ErrorToast";
import { PaMessage } from "Models/pa_message";
import { createNewPaMessage } from "Utils/api";
import { useNavigate } from "react-router-dom";
import { busRouteIdsAtPlaces, getRouteIdsForSign } from "../../../util";
import fp from "lodash/fp";
import React from "react";
import PaMessageForm from "../PaMessageForm";

const NewPaMessage = () => {
const [page, setPage] = useState<Page>(Page.NEW);
const now = moment();

const [associatedAlert, setAssociatedAlert] = useState<Alert>({} as Alert);
const [endWithEffectPeriod, setEndWithEffectPeriod] =
useState<boolean>(false);
const [startDateTime, setStartDateTime] = useState(now);
const [endDateTime, setEndDateTime] = useState<Moment>(
moment(now).add(1, "hour"),
);
const [days, setDays] = useState([1, 2, 3, 4, 5, 6, 7]);
const [priority, setPriority] = useState(2);
const [interval, setInterval] = useState("4");
const [visualText, setVisualText] = useState("");
const [phoneticText, setPhoneticText] = useState("");
const [errorMessage, setErrorMessage] = useState("");
const [errors, setErrors] = useState<string[]>([]);

const [signIds, setSignIds] = useState<string[]>([]);
const places = usePlacesWithPaEss();
const busRoutes = busRouteIdsAtPlaces(places);

const onClearAssociatedAlert = () => {
setEndDateTime(moment(startDateTime).add(1, "hour"));
setAssociatedAlert({} as Alert);
setEndWithEffectPeriod(false);
};

const onImportMessage = (alertMessage: string) => {
setVisualText(alertMessage);
};

const onImportLocations = (informedEntities: InformedEntity[]) => {
const importedSigns = informedEntities.flatMap((entity) => {
const informedPlaces = entity.stop
? places.filter((place) => place.id === entity.stop)
: places;

if (entity.route) {
const entityRoute = entity.route;
let signsToAdd = informedPlaces
.flatMap((place) => place.screens)
.filter((screen) => getRouteIdsForSign(screen).includes(entityRoute));

const directionId = entity.direction_id;
if (directionId !== null) {
signsToAdd = signsToAdd.filter((screen) =>
screen.routes
?.map((route) => route.direction_id)
.includes(directionId),
);
}

return signsToAdd.map((screen) => screen.id);
}

return [];
});

setSignIds(fp.uniq(importedSigns));
};

const navigate = useNavigate();
const onSubmit = async () => {
const newMessage: PaMessage = {
alert_id: associatedAlert.id,
start_datetime: startDateTime.toISOString(),
end_datetime: endWithEffectPeriod ? null : endDateTime.toISOString(),
days_of_week: days,
sign_ids: signIds,
priority,
interval_in_minutes: Number(interval),
visual_text: visualText,
audio_text: phoneticText,
};

const { status, errors } = await createNewPaMessage(newMessage);

if (status === 200) {
navigate("/pa-messages");
} else if (status === 422) {
setErrorMessage("Correct the following errors:");
setErrors(Object.keys(errors));
} else {
setErrorMessage("Something went wrong. Please try again.");
}
};

return (
<div className="new-pa-message">
{page === Page.NEW && (
<NewPaMessagePage
{...{
days,
interval,
navigateTo: setPage,
phoneticText,
priority,
setDays,
startDateTime,
setStartDateTime,
endDateTime,
setEndDateTime,
setErrorMessage,
setInterval,
setPhoneticText,
setPriority,
setVisualText,
setAssociatedAlert,
onClearAssociatedAlert,
setEndWithEffectPeriod,
visualText,
associatedAlert,
endWithEffectPeriod,
signIds,
setSignIds,
places,
busRoutes,
onSubmit,
}}
/>
)}
{[Page.STATIONS, Page.ZONES].includes(page) && (
<div className="select-station-and-zones-container">
<SelectStationsAndZones
places={places}
value={signIds}
onChange={setSignIds}
page={page}
navigateTo={setPage}
busRoutes={busRoutes}
onError={(message: string) => setErrorMessage(message)}
/>
</div>
)}
{page === Page.ALERTS && (
<AssociateAlertPage
associatedAlert={associatedAlert}
endWithEffectPeriod={endWithEffectPeriod}
onImportMessage={onImportMessage}
onImportLocations={onImportLocations}
navigateTo={setPage}
setAssociatedAlert={setAssociatedAlert}
setEndWithEffectPeriod={setEndWithEffectPeriod}
/>
)}
<ErrorToast
errorMessage={errorMessage}
errors={errors}
onClose={() => {
setErrors([]);
setErrorMessage("");
}}
/>
</div>
);
return <PaMessageForm />;
};

export default NewPaMessage;
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface AssociateAlertPageProps {
setEndWithEffectPeriod: Dispatch<SetStateAction<boolean>>;
}

const AssociateAlertPage = ({
const AssociateAlert = ({
associatedAlert,
endWithEffectPeriod,
onImportMessage,
Expand Down Expand Up @@ -76,7 +76,7 @@ const AssociateAlertPage = ({
<h1>Select alert to associate with PA/ESS Message</h1>
</Col>
<Col className="associate-alert-page-header__cancel">
<Button variant="link" onClick={() => navigateTo(Page.NEW)}>
<Button variant="link" onClick={() => navigateTo(Page.MAIN)}>
Cancel
</Button>
</Col>
Expand Down Expand Up @@ -211,7 +211,7 @@ const AssociateAlertPage = ({
if (importLocations) {
onImportLocations(associatedAlert.informed_entities);
}
navigateTo(Page.NEW);
navigateTo(Page.MAIN);
}}
className="apply-button"
>
Expand Down Expand Up @@ -346,4 +346,4 @@ const AssociateAlertsTableRow: ComponentType<AssociateAlertsTableRowProps> = ({
);
};

export default AssociateAlertPage;
export default AssociateAlert;
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ interface Props {
onSubmit: () => void;
}

const NewPaMessagePage = ({
const MainForm = ({
days,
startDateTime,
setStartDateTime,
Expand Down Expand Up @@ -538,4 +538,4 @@ const NewPaMessageHeader = ({
);
};

export default NewPaMessagePage;
export default MainForm;
Loading

0 comments on commit 64be6c0

Please sign in to comment.