Skip to content

Commit 100710c

Browse files
committed
refactor: Update Status.Entities.ts and Status.Trans.ts
- Update Status.Entities.ts to add a new property "updates" to the IncidentEntityV2 interface. - Update Status.Trans.ts to rename it to Status.Trans.V1.ts and update the Logger name to "TransformerV1". - Add a new file Status.Trans.V2.ts to introduce a new TransformerV2 function for handling v2 status data. - Update Status.tsx to use the appropriate Transformer function based on the value of the environment variable "SD_BACKEND_V2".
1 parent 38ffcb9 commit 100710c

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

src/Services/Status.Entities.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ interface IncidentEntityV1 {
4242
* @since 1.0.0
4343
* @version 0.1.0
4444
*/
45-
export interface IncidentEntityV2 extends IncidentEntityV1 {
45+
export interface IncidentEntityV2 extends Omit<IncidentEntityV1, "updates"> {
4646
title: string;
4747
components: number[];
4848
system: boolean;
49-
updates: UpdateEntityV2[];
49+
updates?: UpdateEntityV2[];
5050
}
5151

5252
interface UpdateEntityV1 {

src/Services/Status.Trans.ts renamed to src/Services/Status.Trans.V1.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import { EmptyDB } from "./Status";
66
import { NameEnum, StatusEntityV1, StatusEnum } from "./Status.Entities";
77
import { IStatusContext } from "./Status.Models";
88

9-
const log = new Logger("Service", "Status", "Transformer");
9+
const log = new Logger("Service", "Status", "TransformerV1");
1010

1111
/**
1212
* @author Aloento
1313
* @since 1.0.0
1414
* @version 0.1.0
1515
*/
16-
export function Transformer(list: StatusEntityV1[]): IStatusContext {
16+
export function TransformerV1(list: StatusEntityV1[]): IStatusContext {
1717
let id = 0;
1818
const db = EmptyDB();
1919

src/Services/Status.Trans.V2.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Logger } from "~/Helpers/Logger";
2+
import { EmptyDB } from "./Status";
3+
import { IncidentEntityV2, StatusEntityV2 } from "./Status.Entities";
4+
import { IStatusContext } from "./Status.Models";
5+
6+
const log = new Logger("Service", "Status", "TransformerV1");
7+
8+
/**
9+
* @author Aloento
10+
* @since 1.0.0
11+
* @version 0.1.0
12+
*/
13+
export function TransformerV2({ Components, Events }: { Components: StatusEntityV2[], Events: IncidentEntityV2[] }): IStatusContext {
14+
const db = EmptyDB();
15+
16+
if (!Components?.length || !Events?.length) {
17+
log.warn("Empty List.");
18+
return db;
19+
}
20+
21+
log.info("Status data loaded.", db);
22+
return db;
23+
}

src/Services/Status.tsx

+26-9
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ 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 { StatusEntityV1 } from "./Status.Entities";
6+
import { IncidentEntityV2, StatusEntityV1, StatusEntityV2 } from "./Status.Entities";
77
import { IStatusContext } from "./Status.Models";
8-
import { Transformer } from "./Status.Trans";
8+
import { TransformerV1 } from "./Status.Trans.V1";
9+
import { TransformerV2 } from "./Status.Trans.V2";
910

1011
/**
1112
* @author Aloento
@@ -94,24 +95,40 @@ export function useStatus() {
9495
*/
9596
export function StatusContext({ children }: { children: JSX.Element }) {
9697
const [db, setDB] = useState(DB);
98+
9799
const url = process.env.SD_BACKEND_URL;
98100
const uri = process.env.SD_BACKEND_API;
99101
const file = process.env.SD_BACKEND_FILE === "true";
102+
const v2 = process.env.SD_BACKEND_V2 === "true";
100103

101104
useRequest(
102105
async () => {
103-
log.info("Loading status data...");
106+
log.info(`Loading status data from ${v2 ? "v2" : "v1"}...`);
107+
108+
const compLink = file ? "/mock.json" : `${url}${uri}/${v2 ? "components" : "component_status"}`;
109+
const compRes = await fetch(compLink);
110+
const compData = await compRes.json();
111+
112+
log.debug("Components Status loaded.", compData);
113+
114+
if (v2) {
115+
const eventLink = file ? "/event.json" : `${url}${uri}/incidents`;
116+
const eventRes = await fetch(eventLink);
117+
const eventData = await eventRes.json();
118+
119+
log.debug("Events loaded.", eventData);
104120

105-
const link = file ? "/mock.json" : `${url}${uri}/component_status`;
106-
const response = await fetch(link);
107-
const data = await response.json();
121+
return {
122+
Components: compData as StatusEntityV2[],
123+
Events: eventData as IncidentEntityV2[]
124+
};
125+
}
108126

109-
log.debug("Status data loaded.", data);
110-
return data as StatusEntityV1[];
127+
return compData as StatusEntityV1[];
111128
},
112129
{
113130
cacheKey: log.namespace,
114-
onSuccess: (list) => update(Transformer(list)),
131+
onSuccess: (res: any) => update(v2 ? TransformerV2(res) : TransformerV1(res)),
115132
}
116133
);
117134

0 commit comments

Comments
 (0)