Skip to content

Commit

Permalink
Move static template file from server to client.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaddox5 committed Nov 19, 2024
1 parent fc2fda3 commit 2c4d2e3
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 139 deletions.
18 changes: 6 additions & 12 deletions assets/js/components/Dashboard/EditPaMessage/EditPaMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import PaMessageForm from "../PaMessageForm";
import { updateExistingPaMessage } from "Utils/api";
import { Alert } from "Models/alert";
import { AudioPreview } from "Components/PaMessageForm/types";
import { StaticTemplate } from "Models/static_template";
import { STATIC_TEMPLATES } from "Components/PaMessageForm/StaticTemplatePage";

interface PaMessageResponse {
pa_message: PaMessage;
alert: Alert | null;
template: StaticTemplate | null;
}

const usePaMessage = (id: string | number) => {
Expand Down Expand Up @@ -43,13 +42,7 @@ const FetchPaMessage = ({ id }: { id: string | number }) => {

if (isLoading || error || data?.pa_message == null) return null;

return (
<EditPaMessage
paMessage={data.pa_message}
alert={data.alert}
template={data.template}
/>
);
return <EditPaMessage paMessage={data.pa_message} alert={data.alert} />;
};

const EditPaMessageContainer = () => {
Expand All @@ -68,10 +61,9 @@ const EditPaMessageContainer = () => {
interface Props {
paMessage: PaMessage;
alert?: Alert | null;
template?: StaticTemplate | null;
}

const EditPaMessage = ({ paMessage, alert, template }: Props) => {
const EditPaMessage = ({ paMessage, alert }: Props) => {
const navigate = useNavigate();
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const [errors, setErrors] = useState<string[]>([]);
Expand All @@ -86,7 +78,9 @@ const EditPaMessage = ({ paMessage, alert, template }: Props) => {
onErrorsChange={setErrors}
defaultValues={paMessage}
defaultAlert={alert ?? paMessage.alert_id}
defaultTemplate={template}
defaultTemplate={STATIC_TEMPLATES.find(
(t) => t.id === paMessage.template_id,
)}
defaultAudioState={AudioPreview.Reviewed}
paused={paMessage.paused}
onSubmit={async (data) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useEffect, useState } from "react";
import React, { useState } from "react";
import { Container, Row, Col, Button } from "react-bootstrap";
import { StaticTemplate } from "Models/static_template";
import { fetchStaticTemplates } from "Utils/api";
import FilterGroup from "Components/FilterGroup";
import _staticTemplates from "../../../../static/static_templates.json";

interface Props {
onCancel: () => void;
Expand All @@ -11,15 +11,12 @@ interface Props {

type MessageType = "psa" | "emergency";

export const STATIC_TEMPLATES = _staticTemplates as StaticTemplate[];

const StaticTemplatePage = ({ onCancel, onSelect }: Props) => {
const [staticTemplates, setStaticTemplates] = useState<StaticTemplate[]>([]);
const [selectedMessageType, setSelectedMessageType] =
useState<MessageType>("psa");

useEffect(() => {
fetchStaticTemplates().then((templates) => setStaticTemplates(templates));
}, []);

return (
<div className="static-template-page">
<Container fluid>
Expand Down Expand Up @@ -51,7 +48,7 @@ const StaticTemplatePage = ({ onCancel, onSelect }: Props) => {
</Col>
<Col>
<StaticTemplateTable
templates={staticTemplates.filter(
templates={STATIC_TEMPLATES.filter(
(template) => template.type === selectedMessageType,
)}
selectedMessageType={selectedMessageType}
Expand Down
6 changes: 0 additions & 6 deletions assets/js/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ScreensByAlert } from "../models/screensByAlert";
import { PlaceIdsAndNewScreens } from "../components/Dashboard/PermanentConfiguration/Workflows/GlEink/ConfigureScreensPage";
import getCsrfToken from "../csrf";
import { NewPaMessageBody, UpdatePaMessageBody } from "Models/pa_message";
import { StaticTemplate } from "Models/static_template";

export const fetchPlaces = async (): Promise<Place[]> => {
const response = await fetch("/api/dashboard");
Expand Down Expand Up @@ -171,11 +170,6 @@ export const updateExistingPaMessage = async (
};
};

export const fetchStaticTemplates = async (): Promise<StaticTemplate[]> => {
const response = await fetch("/api/pa-messages/static-templates");
return await response.json();
};

const getPostBodyAndHeaders = (
bodyData: { [key: string]: any },
extraHeaders: { [key: string]: string } = {},
Expand Down
146 changes: 146 additions & 0 deletions assets/static/static_templates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
[
{
"id": 1,
"title": "Bicycles (English)",
"visual_text": "To make space for other passengers, bicycles are not permitted on trains during rush hour, or at any time of day on the Green Line. For details visit mbta.com/bikes.",
"audio_text": "To make space for other passengers, bicycles are not permitted on trains during rush hour, or at any time of day on the Green Line. For details visit mbta.com/bikes.",
"type": "psa",
"archived": false
},
{
"id": 2,
"title": "Backpacks (English)",
"visual_text": "To make space for other passengers and speed up boarding, please take off your backpack before entering the train and hold it at your side.",
"audio_text": "To make space for other passengers and speed up boarding, please take off your backpack before entering the train and hold it at your side.",
"type": "psa",
"archived": false
},
{
"id": 3,
"title": "Track Fires (English)",
"visual_text": "Delays due to fires are often caused by trash on the tracks. Please help us keep our stations clean by throwing away or recycling unwanted items.",
"audio_text": "Delays due to fires are often caused by trash on the tracks. Please help us keep our stations clean by throwing away or recycling unwanted items.",
"type": "psa",
"archived": false
},
{
"id": 4,
"title": "Bicycles (English + Spanish)",
"visual_text": "To make space for other passengers, bicycles are not permitted on trains during rush hour, or at any time of day on the Green Line. For details visit mbta.com/bikes.\n\nPara dejar espacio para otros pasajeros, bicicletas no son permitidas en los trenes durante hora pico y nunca son permitidas en la linea verde. Para mas informacion, visite mbta.com/bikes",
"audio_text": "To make space for other passengers, bicycles are not permitted on trains during rush hour, or at any time of day on the Green Line. For details visit mbta.com/bikes.\n\n<lang xml:lang=\"es-US\"> Para dejar espacio para otros pasajeros, bicicletas no son permitidas en los trenes durante hora pico y nunca son permitidas en la línea verde. Para más información, visite [mbta.com/bikes](http://mbta.com/bikes) </lang>",
"type": "psa",
"archived": false
},
{
"id": 5,
"title": "Backpacks (English + Spanish)",
"visual_text": "To make space for other passengers and speed up boarding, please take off your backpack before entering the train and hold it at your side.\n\nPara dejar espacio para otros pasajeros y acelerar el embarque, por favor quitese la mochila y mantengala a su lado.",
"audio_text": "To make space for other passengers and speed up boarding, please take off your backpack before entering the train and hold it at your side.\n\n<lang xml:lang=\"es-US\"> Para dejar espacio para otros pasajeros y acelerar el embarque, por favor quítese la mochila y manténgala a su lado. </lang>",
"type": "psa",
"archived": false
},
{
"id": 6,
"title": "Track Fires (English + Spanish)",
"visual_text": "Delays due to fires are often caused by trash on the tracks. Please help us keep our stations clean by throwing away or recycling unwanted items.\n\nRetrasos debidos a incendios, muchas veces son causados por basura en las rieles. Por favor, ayudenos a mantener nuestras estaciones limpias tirando o reciclando sus articulos no deseados.",
"audio_text": "Delays due to fires are often caused by trash on the tracks. Please help us keep our stations clean by throwing away or recycling unwanted items.\n\n<lang xml:lang=\"es-US\"> Retrasos debidos a incendios, muchas veces son causados por basura en las rieles. Por favor, ayúdenos a mantener nuestras estaciones limpias tirando o reciclando sus artículos no deseados. </lang>",
"type": "psa",
"archived": false
},
{
"id": 7,
"title": "Safety: See something, say something",
"visual_text": "Safety Reminder: If you see something, say something. Call Transit Police at 617-222-1212",
"audio_text": "Safety Reminder: If you see something, say something. Call Transit Police at 617-222-1212",
"type": "psa",
"archived": false
},
{
"id": 8,
"title": "Safety: Don't scroll and stroll",
"visual_text": "Safety Reminder: Don't scroll and stroll. Please pay attention to your surroundings and other riders.",
"audio_text": "Safety Reminder: Don't scroll and stroll. Please pay attention to your surroundings and other riders.",
"type": "psa",
"archived": false
},
{
"id": 9,
"title": "Safety: Do not run car to car",
"visual_text": "Safety Reminder: Do not run from car to car. It can cause injuries to yourself and others.",
"audio_text": "Safety Reminder: Do not run from car to car. It can cause injuries to yourself and others.",
"type": "psa",
"archived": false
},
{
"id": 10,
"title": "Safety: Closing doors",
"visual_text": "Safety Reminder: Please watch for closing doors for your own safety.",
"audio_text": "Safety Reminder: Please watch for closing doors for your own safety.",
"type": "psa",
"archived": false
},
{
"id": 11,
"title": "Safety: Follow instructions",
"visual_text": "Safety Reminder: In the event of an emergency, follow instructions from MBTA personnel for your safety.",
"audio_text": "Safety Reminder: In the event of an emergency, follow instructions from MBTA personnel for your safety.",
"type": "psa",
"archived": false
},
{
"id": 12,
"title": "Safety: Don't throw trash",
"visual_text": "Do not throw trash on the tracks. It can cause track fires and risk your safety.",
"audio_text": "Safety Reminder: Do not throw trash on the tracks. It can cause track fires and risk your safety.",
"type": "psa",
"archived": false
},
{
"id": 13,
"title": "Emergency in station",
"visual_text": "Attention Passengers. An emergency situation has been reported, please leave the station through the nearest exit.",
"audio_text": "Attention Passengers. An emergency situation has been reported, please leave the station through the nearest exit.",
"type": "emergency",
"archived": false
},
{
"id": 14,
"title": "Emergency outside station",
"visual_text": "Attention Passengers. An emergency situation has been reported outside the station. Please remain in the station until advised otherwise by MBTA or emergency personnel.",
"audio_text": "Attention Passengers. An emergency situation has been reported outside the station. Please remain in the station until advised otherwise by MBTA or emergency personnel.",
"type": "emergency",
"archived": false
},
{
"id": 15,
"title": "Emergency follow instructions",
"visual_text": "Attention Passengers. An emergency situation has been reported. Please follow the instructions of MBTA or emergency personnel.",
"audio_text": "Attention Passengers. An emergency situation has been reported. Please follow the instructions of MBTA or emergency personnel.",
"type": "emergency",
"archived": false
},
{
"id": 16,
"title": "Stand behind line",
"visual_text": "Attention Passengers. Please stand behind the yellow safety line, a train is approaching the station.",
"audio_text": "Attention Passengers. Please stand behind the yellow safety line, a train is approaching the station.",
"type": "emergency",
"archived": false
},
{
"id": 17,
"title": "Emergency situation ended",
"visual_text": "Attention Passengers. The emergency situation has ended. It is now safe to exit the station.",
"audio_text": "Attention Passengers. The emergency situation has ended. It is now safe to exit the station.",
"type": "emergency",
"archived": false
},
{
"id": 18,
"title": "Emergency TEST",
"visual_text": "This is a test of the emergency messaging system. This is not an emergency, this is a test.",
"audio_text": "This is a test of the emergency messaging system. This is not an emergency, this is a test.",
"type": "emergency",
"archived": false
}
]
3 changes: 1 addition & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ config :screenplay,
pending_screens_config_fetcher: Screenplay.PendingScreensConfig.Fetch.S3,
config_s3_bucket: "mbta-ctd-config",
record_sentry: false,
start_cache_processes: config_env() != :test,
static_template_file_spec: {:priv, "static_templates.json"}
start_cache_processes: config_env() != :test

# Include 2 logger backends
config :logger,
Expand Down
3 changes: 1 addition & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ config :screenplay,
local_signs_json_path: {:test, "signs.json"},
stops_mod: Screenplay.Stops.Mock,
routes_mod: Screenplay.Routes.Mock,
facilities_mod: Screenplay.Facilities.Mock,
static_template_file_spec: {:test, "static_templates.json"}
facilities_mod: Screenplay.Facilities.Mock

config :ueberauth, Ueberauth,
providers: [
Expand Down
41 changes: 0 additions & 41 deletions lib/screenplay/pa_messages/static_templates.ex

This file was deleted.

13 changes: 0 additions & 13 deletions lib/screenplay_web/controllers/pa_messages_api_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ defmodule ScreenplayWeb.PaMessagesApiController do
alias Screenplay.Alerts.Cache, as: AlertsCache
alias Screenplay.PaMessages
alias Screenplay.PaMessages.ListParams
alias Screenplay.PaMessages.StaticTemplates

@watts_client Application.compile_env(:screenplay, :watts_client, Screenplay.Watts.Client)

Expand Down Expand Up @@ -52,12 +51,10 @@ defmodule ScreenplayWeb.PaMessagesApiController do

def show(conn, %{"id" => id}) do
if pa_message = PaMessages.get_message(id) do
{:ok, template} = StaticTemplates.get_template(pa_message.template_id)
alert = AlertsCache.alert(pa_message.alert_id)

json(conn, %{
pa_message: pa_message,
template: template,
alert: if(is_nil(alert), do: alert, else: Alert.to_full_map(alert))
})
else
Expand All @@ -66,14 +63,4 @@ defmodule ScreenplayWeb.PaMessagesApiController do
|> json(%{error: "not_found"})
end
end

def static_templates(conn, _) do
templates =
case StaticTemplates.get_all() do
{:ok, templates} -> templates
:error -> []
end

json(conn, templates)
end
end
1 change: 0 additions & 1 deletion lib/screenplay_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ defmodule ScreenplayWeb.Router do
get("/pa-messages/new/associate-alert", PaMessagesController, :index)
get("/pa-messages/:id/edit", PaMessagesController, :index)
get("/api/pa-messages/preview_audio", PaMessagesApiController, :preview_audio)
get("/api/pa-messages/static-templates", PaMessagesApiController, :static_templates)
end

scope "/", ScreenplayWeb do
Expand Down
25 changes: 0 additions & 25 deletions test/screenplay/pa_messages/static_templates_test.exs

This file was deleted.

Loading

0 comments on commit 2c4d2e3

Please sign in to comment.