Skip to content

Commit

Permalink
refactor: standardize fetch functions to use PromiseVoidFunction type…
Browse files Browse the repository at this point in the history
… and improve error handling in incident severity, monitors, monitor statuses, and on-call policies components
  • Loading branch information
simlarsen committed Jan 27, 2025
1 parent a7f8aa4 commit 33c4943
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 57 deletions.
53 changes: 27 additions & 26 deletions Dashboard/src/Components/IncidentSeverity/FetchIncidentSeverity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,36 @@ const FetchIncidentSeverities: FunctionComponent<ComponentProps> = (
Array<IncidentSeverity>
>([]);

const fetchIncidentSeverities: PromiseVoidFunction = async (): Promise<void> => {
setIsLoading(true);
setError("");
const fetchIncidentSeverities: PromiseVoidFunction =
async (): Promise<void> => {
setIsLoading(true);
setError("");

try {
const incidentSeverities: ListResult<IncidentSeverity> =
await ModelAPI.getList({
modelType: IncidentSeverity,
query: {
_id: new Includes(props.onCallDutyPolicyIds),
},
skip: 0,
limit: LIMIT_PER_PROJECT,
select: {
name: true,
_id: true,
},
sort: {
name: SortOrder.Ascending,
},
});
try {
const incidentSeverities: ListResult<IncidentSeverity> =
await ModelAPI.getList({
modelType: IncidentSeverity,
query: {
_id: new Includes(props.onCallDutyPolicyIds),
},
skip: 0,
limit: LIMIT_PER_PROJECT,
select: {
name: true,
_id: true,
},
sort: {
name: SortOrder.Ascending,
},
});

setIncidentSeverities(incidentSeverities.data);
} catch (err) {
setError(API.getFriendlyMessage(err));
}
setIncidentSeverities(incidentSeverities.data);
} catch (err) {
setError(API.getFriendlyMessage(err));
}

setIsLoading(false);
};
setIsLoading(false);
};

useEffect(() => {
fetchIncidentSeverities().catch((err: Exception) => {
Expand Down
6 changes: 4 additions & 2 deletions Dashboard/src/Components/Monitor/FetchMonitors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import ListResult from "Common/UI/Utils/BaseDatabase/ListResult";
import ErrorMessage from "Common/UI/Components/ErrorMessage/ErrorMessage";
import ComponentLoader from "Common/UI/Components/ComponentLoader/ComponentLoader";
import MonitorsElement from "./Monitors";
import { PromiseVoidFunction } from "Common/Types/FunctionTypes";
import Exception from "Common/Types/Exception/Exception";

export interface ComponentProps {
monitorIds: Array<ObjectID>;
Expand All @@ -22,7 +24,7 @@ const FetchMonitors: FunctionComponent<ComponentProps> = (
const [error, setError] = React.useState<string>("");
const [monitor, setMonitor] = React.useState<Array<Monitor>>([]);

const fetchMonitor = async () => {
const fetchMonitor: PromiseVoidFunction = async (): Promise<void> => {
setIsLoading(true);
setError("");

Expand Down Expand Up @@ -52,7 +54,7 @@ const FetchMonitors: FunctionComponent<ComponentProps> = (
};

useEffect(() => {
fetchMonitor().catch((err) => {
fetchMonitor().catch((err: Exception) => {
setError(API.getFriendlyMessage(err));
});
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import ListResult from "Common/UI/Utils/BaseDatabase/ListResult";
import ErrorMessage from "Common/UI/Components/ErrorMessage/ErrorMessage";
import ComponentLoader from "Common/UI/Components/ComponentLoader/ComponentLoader";
import MonitorStatusesElement from "./MonitorStatusesElement";
import { PromiseVoidFunction } from "Common/Types/FunctionTypes";
import Exception from "Common/Types/Exception/Exception";

export interface ComponentProps {
monitorStatusIds: Array<ObjectID>;
Expand All @@ -24,7 +26,7 @@ const FetchMonitorStatuses: FunctionComponent<ComponentProps> = (
Array<MonitorStatus>
>([]);

const fetchMonitorStatus = async () => {
const fetchMonitorStatus: PromiseVoidFunction = async (): Promise<void> => {
setIsLoading(true);
setError("");

Expand Down Expand Up @@ -54,7 +56,7 @@ const FetchMonitorStatuses: FunctionComponent<ComponentProps> = (
};

useEffect(() => {
fetchMonitorStatus().catch((err) => {
fetchMonitorStatus().catch((err: Exception) => {
setError(API.getFriendlyMessage(err));
});
}, []);
Expand Down
57 changes: 30 additions & 27 deletions Dashboard/src/Components/OnCallPolicy/FetchOnCallPolicies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import ListResult from "Common/UI/Utils/BaseDatabase/ListResult";
import ErrorMessage from "Common/UI/Components/ErrorMessage/ErrorMessage";
import ComponentLoader from "Common/UI/Components/ComponentLoader/ComponentLoader";
import OnCallPoliciesElement from "./OnCallPolicies";
import { PromiseVoidFunction } from "Common/Types/FunctionTypes";
import Exception from "Common/Types/Exception/Exception";

export interface ComponentProps {
onCallDutyPolicyIds: Array<ObjectID>;
Expand All @@ -24,38 +26,39 @@ const FetchOnCallDutyPolicies: FunctionComponent<ComponentProps> = (
Array<OnCallDutyPolicy>
>([]);

const fetchOnCallDutyPolicies = async () => {
setIsLoading(true);
setError("");
const fetchOnCallDutyPolicies: PromiseVoidFunction =
async (): Promise<void> => {
setIsLoading(true);
setError("");

try {
const onCallDutyPolicies: ListResult<OnCallDutyPolicy> =
await ModelAPI.getList({
modelType: OnCallDutyPolicy,
query: {
_id: new Includes(props.onCallDutyPolicyIds),
},
skip: 0,
limit: LIMIT_PER_PROJECT,
select: {
name: true,
_id: true,
},
sort: {
name: SortOrder.Ascending,
},
});
try {
const onCallDutyPolicies: ListResult<OnCallDutyPolicy> =
await ModelAPI.getList({
modelType: OnCallDutyPolicy,
query: {
_id: new Includes(props.onCallDutyPolicyIds),
},
skip: 0,
limit: LIMIT_PER_PROJECT,
select: {
name: true,
_id: true,
},
sort: {
name: SortOrder.Ascending,
},
});

setOnCallDutyPolicies(onCallDutyPolicies.data);
} catch (err) {
setError(API.getFriendlyMessage(err));
}
setOnCallDutyPolicies(onCallDutyPolicies.data);
} catch (err) {
setError(API.getFriendlyMessage(err));
}

setIsLoading(false);
};
setIsLoading(false);
};

useEffect(() => {
fetchOnCallDutyPolicies().catch((err) => {
fetchOnCallDutyPolicies().catch((err: Exception) => {
setError(API.getFriendlyMessage(err));
});
}, []);
Expand Down

0 comments on commit 33c4943

Please sign in to comment.