-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2535 from target/ts-PolicyStepDeleteDialog
ui/escalation-policies: convert PolicyStepDeleteDialog to ts
- Loading branch information
Showing
2 changed files
with
80 additions
and
94 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
web/src/app/escalation-policies/PolicyStepDeleteDialog.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React from 'react' | ||
import { gql, useMutation, useQuery } from 'urql' | ||
import { nonFieldErrors } from '../util/errutil' | ||
import FormDialog from '../dialogs/FormDialog' | ||
import Spinner from '../loading/components/Spinner' | ||
import { GenericError } from '../error-pages' | ||
import { EscalationPolicyStep } from '../../schema' | ||
|
||
const query = gql` | ||
query ($id: ID!) { | ||
escalationPolicy(id: $id) { | ||
id | ||
steps { | ||
id | ||
} | ||
} | ||
} | ||
` | ||
|
||
const mutation = gql` | ||
mutation ($input: UpdateEscalationPolicyInput!) { | ||
updateEscalationPolicy(input: $input) | ||
} | ||
` | ||
|
||
function PolicyStepDeleteDialog(props: { | ||
escalationPolicyID: string | ||
stepID: string | ||
onClose: () => void | ||
}): JSX.Element { | ||
const [{ fetching, data, error }] = useQuery({ | ||
query, | ||
variables: { id: props.escalationPolicyID }, | ||
}) | ||
|
||
const [deleteStepMutationStatus, deleteStepMutation] = useMutation(mutation) | ||
|
||
if (fetching && !data) return <Spinner /> | ||
if (error) return <GenericError error={error.message} /> | ||
|
||
// get array of step ids without the step to delete | ||
const sids = data.escalationPolicy.steps.map( | ||
(s: EscalationPolicyStep) => s.id, | ||
) | ||
const toDel = sids.indexOf(props.stepID) | ||
sids.splice(toDel, 1) | ||
|
||
return ( | ||
<FormDialog | ||
title='Are you sure?' | ||
confirm | ||
subTitle={ | ||
'This will delete step #' + | ||
(data.escalationPolicy.steps | ||
.map((s: EscalationPolicyStep) => s.id) | ||
.indexOf(props.stepID) + | ||
1) + | ||
' on this escalation policy.' | ||
} | ||
loading={deleteStepMutationStatus.fetching} | ||
errors={nonFieldErrors(deleteStepMutationStatus.error)} | ||
onClose={props.onClose} | ||
onSubmit={() => { | ||
deleteStepMutation( | ||
{ | ||
input: { | ||
id: data.escalationPolicy.id, | ||
stepIDs: sids, | ||
}, | ||
}, | ||
{ additionalTypenames: ['EscalationPolicy'] }, | ||
).then((result) => { | ||
if (!result.error) props.onClose() | ||
}) | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
export default PolicyStepDeleteDialog |