fix(striker-ui): duck-tape alert override update by wipe-n-create

main
Tsu-ba-me 11 months ago
parent 9a6a4f8b7c
commit 1991acfa1d
  1. 114
      striker-ui/components/ManageMailRecipient/AddMailRecipientForm.tsx
  2. 6
      striker-ui/components/ManageMailRecipient/AlertOverrideInputGroup.tsx
  3. 6
      striker-ui/components/ManageMailRecipient/ManageAlertOverride.tsx
  4. 2
      striker-ui/types/ManageMailRecipient.d.ts

@ -121,82 +121,36 @@ const getAlertOverrideRequestList = (
if (!mailRecipientUuid) return [];
return Object.values(current.alertOverrides).reduce<AlertOverrideRequest[]>(
(previous, value) => {
if (value.delete && value.uuids) {
/**
* 1 or more existing records marked for removal.
*/
(previous, { remove, level, target, uuids: existingOverrides }) => {
/**
* There's no update, just delete every record and create the new records.
*
* This is not optimal, but keep it until there's a better solution.
*/
if (existingOverrides) {
previous.push(
...Object.keys(value.uuids).map<AlertOverrideRequest>((uuid) => ({
method: 'delete',
url: `${urlPrefix}/${uuid}`,
})),
...Object.keys(existingOverrides).map<AlertOverrideRequest>(
(overrideUuid) => ({
method: 'delete',
url: `${urlPrefix}/${overrideUuid}`,
}),
),
);
return previous;
}
const { level, target, uuids } = value;
if (!target) return previous;
const hosts: string[] = target.subnodes ?? [target.uuid];
if (uuids) {
/**
* Found existing alert override UUIDs; the requests must be updates.
*/
const slots: string[] = Object.keys(uuids);
const longest = Math.max(slots.length, hosts.length);
if (target && !remove) {
const newHosts: string[] = target.subnodes ?? [target.uuid];
previous.push(
...Array.from({ length: longest }).map<AlertOverrideRequest>(
(ignore, i) => {
const host = hosts[i];
const slot = slots[i];
if (!slot) {
return {
body: { hostUuid: host, level, mailRecipientUuid },
method: 'post',
url: urlPrefix,
};
}
const url = `${urlPrefix}/${slot}`;
if (!host) {
return {
method: 'delete',
url,
};
}
return {
body: { hostUuid: host, level, mailRecipientUuid },
method: 'put',
url,
};
},
),
...newHosts.map<AlertOverrideRequest>((hostUuid) => ({
body: { hostUuid, level, mailRecipientUuid },
method: 'post',
url: urlPrefix,
})),
);
return previous;
}
/**
* No existing alert override UUIDs, meaning these are new records.
*/
previous.push(
...hosts.map<AlertOverrideRequest>((hostUuid) => ({
body: { hostUuid, level, mailRecipientUuid },
method: 'post',
url: urlPrefix,
})),
);
return previous;
},
[],
@ -263,18 +217,20 @@ const AddMailRecipientForm: FC<AddMailRecipientFormProps> = (props) => {
<>
<FormSummary entries={mrBody} />
<FormSummary
entries={Object.entries(alertOverrides).reduce<
Record<string, { level: number; name: string }>
>((previous, [valueId, value]) => {
if (!value.target) return previous;
previous[valueId] = {
level: value.level,
name: value.target.name,
};
return previous;
}, {})}
entries={{
alertOverrides: Object.entries(alertOverrides).reduce<
Record<string, { level: number; name: string }>
>((previous, [valueId, value]) => {
if (value.remove || !value.target) return previous;
previous[valueId] = {
level: value.level,
name: value.target.name,
};
return previous;
}, {}),
}}
/>
</>
),

@ -47,8 +47,8 @@ const AlertOverrideInputGroup: FC<AlertOverrideInputGroupProps> = (props) => {
[aoVid, mrUuid],
);
const deleteChain = useMemo<string>(
() => `${overrideChain}.delete`,
const removeChain = useMemo<string>(
() => `${overrideChain}.remove`,
[overrideChain],
);
const targetChain = useMemo<string>(
@ -116,7 +116,7 @@ const AlertOverrideInputGroup: FC<AlertOverrideInputGroupProps> = (props) => {
mapPreset="delete"
onClick={() => {
if (alertOverride.uuids) {
formik.setFieldValue(deleteChain, true, true);
formik.setFieldValue(removeChain, true, true);
} else {
formik.setValues((previous) => {
const shallow = { ...previous };

@ -3,7 +3,6 @@ import { v4 as uuidv4 } from 'uuid';
import AlertOverrideInputGroup from './AlertOverrideInputGroup';
import List from '../List';
import useChecklist from '../../hooks/useChecklist';
const ManageAlertOverride: FC<ManageAlertOverrideProps> = (props) => {
const {
@ -18,12 +17,9 @@ const ManageAlertOverride: FC<ManageAlertOverrideProps> = (props) => {
} = formik;
const { alertOverrides } = mailRecipient;
const { hasChecks } = useChecklist(alertOverrides);
return (
<List
allowAddItem
disableDelete={!hasChecks}
edit
header="Alert override rules"
listEmpty="No alert overrides(s)"
@ -48,7 +44,7 @@ const ManageAlertOverride: FC<ManageAlertOverrideProps> = (props) => {
});
}}
renderListItem={(valueId, value) =>
!value.delete && (
!value.remove && (
<AlertOverrideInputGroup
alertOverrideTargetOptions={alertOverrideTargetOptions}
alertOverrideValueId={valueId}

@ -18,8 +18,8 @@ type AlertOverrideTarget = {
};
type AlertOverrideFormikAlertOverride = {
delete?: boolean;
level: number;
remove?: boolean;
target: AlertOverrideTarget | null;
uuids?: Record<string, string>;
};

Loading…
Cancel
Save