Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui/escalation-policies: convert PolicyStepDeleteDialog to ts #2535

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 0 additions & 94 deletions web/src/app/escalation-policies/PolicyStepDeleteDialog.js

This file was deleted.

80 changes: 80 additions & 0 deletions web/src/app/escalation-policies/PolicyStepDeleteDialog.tsx
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