Skip to content

Commit

Permalink
Revert some unnecessarily changed code.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaddox5 committed Nov 19, 2024
1 parent 2c4d2e3 commit 87549ed
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
61 changes: 44 additions & 17 deletions assets/js/components/Dashboard/EditPaMessage/EditPaMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useMemo, useState } from "react";
import useSWR, { mutate } from "swr";
import { useNavigate, useParams } from "react-router-dom";
import { PaMessage } from "Models/pa_message";
Expand All @@ -8,41 +8,68 @@ import { Alert } from "Models/alert";
import { AudioPreview } from "Components/PaMessageForm/types";
import { STATIC_TEMPLATES } from "Components/PaMessageForm/StaticTemplatePage";

interface PaMessageResponse {
pa_message: PaMessage;
alert: Alert | null;
}
const useAlert = (id: string | null | undefined) => {
const { data: alerts, isLoading } = useSWR<Array<Alert>>(
id ? "/api/alerts/non_access_alerts" : null,
async (url: string | null) => {
if (url == null) return [];

const usePaMessage = (id: string | number) => {
const { data, isLoading, error } = useSWR<PaMessageResponse>(
`/api/pa-messages/${id}`,
async (url: string) => {
const response = await fetch(url);
const body = await response.json();

if (400 <= response.status) throw response;
return body;
const { alerts } = await response.json();
return alerts;
},
);

const alert = useMemo(() => {
if (id == null) return null;
return alerts?.find((a) => a.id === id);
}, [id, alerts]);

return {
alert,
isLoading,
};
};

const usePaMessage = (id: string | number) => {
const {
data: paMessage,
isLoading,
error,
} = useSWR<PaMessage>(`/api/pa-messages/${id}`, async (url: string) => {
const response = await fetch(url);
const body = await response.json();

if (400 <= response.status) throw response;
return body;
});

return {
data,
paMessage,
isLoading,
error,
};
};

const FetchPaMessage = ({ id }: { id: string | number }) => {
const navigate = useNavigate();
const { data, isLoading, error } = usePaMessage(id);
const { paMessage, isLoading, error } = usePaMessage(id);

useEffect(() => {
if (error?.status === 404) navigate("/pa-messages");
}, [error, navigate]);

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

return <FetchAlert paMessage={paMessage} />;
};

const FetchAlert = ({ paMessage }: { paMessage: PaMessage }) => {
const { alert, isLoading } = useAlert(paMessage.alert_id);

if (isLoading) return null;

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

const EditPaMessageContainer = () => {
Expand Down
9 changes: 1 addition & 8 deletions lib/screenplay_web/controllers/pa_messages_api_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ defmodule ScreenplayWeb.PaMessagesApiController do

action_fallback ScreenplayWeb.FallbackController

alias Screenplay.Alerts.Alert
alias Screenplay.Alerts.Cache, as: AlertsCache
alias Screenplay.PaMessages
alias Screenplay.PaMessages.ListParams

Expand Down Expand Up @@ -51,12 +49,7 @@ defmodule ScreenplayWeb.PaMessagesApiController do

def show(conn, %{"id" => id}) do
if pa_message = PaMessages.get_message(id) do
alert = AlertsCache.alert(pa_message.alert_id)

json(conn, %{
pa_message: pa_message,
alert: if(is_nil(alert), do: alert, else: Alert.to_full_map(alert))
})
json(conn, pa_message)
else
conn
|> put_status(404)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,7 @@ defmodule ScreenplayWeb.PaMessagesApiControllerTest do
audio_text: "Audio Text"
})

assert %{"pa_message" => %{"id" => 1}, "alert" => nil} =
conn
|> get("/api/pa-messages/1")
|> json_response(200)
assert %{"id" => 1} = conn |> get("/api/pa-messages/1") |> json_response(200)
end

@tag :authenticated_pa_message_admin
Expand All @@ -382,10 +379,7 @@ defmodule ScreenplayWeb.PaMessagesApiControllerTest do
alert_id: "1"
})

assert %{"pa_message" => %{"id" => 1}, "alert" => %{"id" => "1"}} =
conn
|> get("/api/pa-messages/1")
|> json_response(200)
assert %{"id" => 1} = conn |> get("/api/pa-messages/1") |> json_response(200)
end

@tag :authenticated_pa_message_admin
Expand Down

0 comments on commit 87549ed

Please sign in to comment.