Add OnCallDutyScheduleView component to

OnCallPolicyDelete
This commit is contained in:
Simon Larsen 2023-12-05 13:53:50 +00:00
parent b187773f99
commit 606c0cb69d
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
4 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,41 @@
import React, { FunctionComponent, ReactElement } from 'react';
import OnCallDutySchedule from 'Model/Models/OnCallDutyPolicySchedule';
import Link from 'CommonUI/src/Components/Link/Link';
import Route from 'Common/Types/API/Route';
export interface ComponentProps {
schedule: OnCallDutySchedule;
onNavigateComplete?: (() => void) | undefined;
}
const OnCallDutyScheduleElement: FunctionComponent<ComponentProps> = (
props: ComponentProps
): ReactElement => {
if (
props.schedule._id &&
(props.schedule.projectId || (props.schedule.project && props.schedule.project._id))
) {
const projectId: string | undefined = props.schedule.projectId
? props.schedule.projectId.toString()
: props.schedule.project
? props.schedule.project._id
: '';
return (
<Link
onNavigateComplete={props.onNavigateComplete}
className="hover:underline"
to={
new Route(
`/dashboard/${projectId?.toString()}/on-call-duty/schedules/${props.schedule._id.toString()}`
)
}
>
<span>{props.schedule.name}</span>
</Link>
);
}
return <span>{props.schedule.name}</span>;
};
export default OnCallDutyScheduleElement;

View File

@ -0,0 +1,34 @@
import OnCallDutySchedule from 'Model/Models/OnCallDutyPolicySchedule';
import React, { FunctionComponent, ReactElement } from 'react';
import OnCallDutyScheduleElement from './ScheduleElement';
export interface ComponentProps {
schedules: Array<OnCallDutySchedule>;
onNavigateComplete?: (() => void) | undefined;
}
const OnCallDutySchedulesElement: FunctionComponent<ComponentProps> = (
props: ComponentProps
): ReactElement => {
if (!props.schedules || props.schedules.length === 0) {
return <p>No on call schedules.</p>;
}
return (
<div>
{props.schedules.map((schedule: OnCallDutySchedule, i: number) => {
return (
<span key={i}>
<OnCallDutyScheduleElement
schedule={schedule}
onNavigateComplete={props.onNavigateComplete}
/>
{i !== props.schedules.length - 1 && <span>,&nbsp;</span>}
</span>
);
})}
</div>
);
};
export default OnCallDutySchedulesElement;

View File

@ -0,0 +1,76 @@
import React, { FunctionComponent, ReactElement, useState } from 'react';
import ObjectID from 'Common/Types/ObjectID';
import ComponentLoader from 'CommonUI/src/Components/ComponentLoader/ComponentLoader';
import ModelAPI, { ListResult } from 'CommonUI/src/Utils/ModelAPI/ModelAPI';
import OnCallDutyPolicyEscalationRuleOnCallSchedule from 'Model/Models/OnCallDutyPolicyEscalationRuleOnCallSchedule';
import { LIMIT_PER_PROJECT } from 'Common/Types/Database/LimitMax';
import useAsyncEffect from 'use-async-effect';
import API from 'CommonUI/src/Utils/API/API';
import ErrorMessage from 'CommonUI/src/Components/ErrorMessage/ErrorMessage';
import SchedulesElement from '../../OnCallDutySchedule/SchedulesElement';
import OnCallDutyPolicySchedule from 'Model/Models/OnCallDutyPolicySchedule';
export interface ComponentProps {
escalationRuleId: ObjectID;
}
const OnCallDutyScheduleView: FunctionComponent<ComponentProps> = (
props: ComponentProps
): ReactElement => {
const [schedules, setSchedules] = useState<Array<OnCallDutyPolicySchedule>>([]);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string>('');
useAsyncEffect(async () => {
try {
setIsLoading(true);
const onCallSchedules: ListResult<OnCallDutyPolicyEscalationRuleOnCallSchedule> =
await ModelAPI.getList(
OnCallDutyPolicyEscalationRuleOnCallSchedule,
{
onCallDutyPolicyEscalationRuleId:
props.escalationRuleId,
},
LIMIT_PER_PROJECT,
0,
{
onCallDutyPolicySchedule: {
name: true,
},
},
{}
);
const schedules: Array<OnCallDutyPolicySchedule> = onCallSchedules.data.map(
(onCallUser: OnCallDutyPolicyEscalationRuleOnCallSchedule) => {
return onCallUser.onCallDutyPolicySchedule!;
}
);
setSchedules(schedules);
} catch (err) {
setError(API.getFriendlyMessage(err));
}
setIsLoading(false);
}, []);
if (isLoading) {
return (
<div className="flex justify-center w-full">
<ComponentLoader />
</div>
);
}
if (error) {
return <ErrorMessage error={error} />;
}
return <SchedulesElement schedules={schedules} />;
};
export default OnCallDutyScheduleView;

View File

@ -24,6 +24,7 @@ import { JSONObject } from 'Common/Types/JSON';
import TeamView from '../../../Components/OnCallPolicy/EscalationRule/TeamView';
import UserView from '../../../Components/OnCallPolicy/EscalationRule/UserView';
import OnCallDutyPolicySchedule from 'Model/Models/OnCallDutyPolicySchedule';
import OnCallDutyScheduleView from '../../../Components/OnCallPolicy/EscalationRule/OnCallScheduleView';
const OnCallPolicyDelete: FunctionComponent<PageComponentProps> = (
props: PageComponentProps
@ -264,6 +265,24 @@ const OnCallPolicyDelete: FunctionComponent<PageComponentProps> = (
);
},
},
{
field: {
name: true,
},
title: 'On Call Schedules',
description:
'On call schedules which will be executed when incident is triggered.',
type: FieldType.Element,
getElement: (item: JSONObject): ReactElement => {
return (
<OnCallDutyScheduleView
escalationRuleId={
new ObjectID(item['_id'] as string)
}
/>
);
},
},
{
field: {
escalateAfterInMinutes: true,