Skip to content

Commit 5f054ec

Browse files
committed
feat: Update StatusContext to use v2 entities and enhance data loading logic
1 parent d348d50 commit 5f054ec

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/Services/Status.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { openDB } from "idb";
33
import { createContext, useContext, useState } from "react";
44
import { Dic } from "~/Helpers/Entities";
55
import { Logger } from "~/Helpers/Logger";
6-
import { StatusEntity } from "./Status.Entities";
6+
import { IncidentEntityV2, StatusEntityV2 } from "./Status.Entities";
77
import { IStatusContext } from "./Status.Models";
8-
import { Transformer } from "./Status.Trans";
8+
import { TransformerV2 } from "./Status.Trans.V2";
99

1010
/**
1111
* @author Aloento
@@ -94,20 +94,35 @@ export function useStatus() {
9494
*/
9595
export function StatusContext({ children }: { children: JSX.Element }) {
9696
const [db, setDB] = useState(DB);
97+
9798
const url = process.env.SD_BACKEND_URL;
9899
const uri = process.env.SD_BACKEND_API;
100+
const file = process.env.SD_BACKEND_FILE === "true";
99101

100102
useRequest(
101103
async () => {
102-
log.info("Loading status data...");
103-
const response = await fetch(`${url}${uri}/component_status`);
104-
const data = await response.json();
105-
log.debug("Status data loaded.", data);
106-
return data as StatusEntity[];
104+
log.info(`Loading status data from v2...`);
105+
106+
const compLink = file ? "/mock.json" : `${url}${uri}/components`;
107+
const compRes = await fetch(compLink);
108+
const compData = await compRes.json();
109+
110+
log.debug("Components Status loaded.", compData);
111+
112+
const eventLink = file ? "/event.json" : `${url}${uri}/incidents`;
113+
const eventRes = await fetch(eventLink);
114+
const eventData = (await eventRes.json()).data;
115+
116+
log.debug("Events loaded.", eventData);
117+
118+
return {
119+
Components: compData as StatusEntityV2[],
120+
Events: eventData as IncidentEntityV2[]
121+
};
107122
},
108123
{
109124
cacheKey: log.namespace,
110-
onSuccess: (list) => update(Transformer(list)),
125+
onSuccess: (res) => update(TransformerV2(res)),
111126
}
112127
);
113128

0 commit comments

Comments
 (0)