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/schedules: convert ScheduleOverrideDeleteDialog to ts and use urql #2461

Merged
merged 10 commits into from
Jul 19, 2022
97 changes: 0 additions & 97 deletions web/src/app/schedules/ScheduleOverrideDeleteDialog.js

This file was deleted.

101 changes: 101 additions & 0 deletions web/src/app/schedules/ScheduleOverrideDeleteDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import React from 'react'

import { gql, useQuery, useMutation } from 'urql'
import { nonFieldErrors } from '../util/errutil'
import { Typography } from '@mui/material'
import FormDialog from '../dialogs/FormDialog'
import { useURLParam } from '../actions/hooks'
import { formatOverrideTime } from './util'
import { GenericError } from '../error-pages'
import Spinner from '../loading/components/Spinner'

const query = gql`
query ($id: ID!) {
userOverride(id: $id) {
id
start
end
addUser {
id
name
}
removeUser {
id
name
}
}
}
`

const mutation = gql`
mutation delete($input: [TargetInput!]!) {
deleteAll(input: $input)
}
`

export default function ScheduleOverrideDeleteDialog(props: {
overrideID: string
onClose: () => void
}): JSX.Element {
const [zone] = useURLParam('tz', 'local')

const [{ data, fetching, error }] = useQuery({
query,
variables: { id: props.overrideID },
})

const [deleteOverrideStatus, deleteOverride] = useMutation(mutation)

if (error) {
return <GenericError error={error.message} />
}

if (fetching && !data) {
return <Spinner />
}

const addUser = data.userOverride.addUser ? data.userOverride.addUser : ''
const removeUser = data.userOverride.removeUser
? data.userOverride.removeUser
: ''
const start = data.userOverride.start ? data.userOverride.start : ''
const end = data.userOverride.end ? data.userOverride.end : ''
Comment on lines +57 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a suggestion

Suggested change
const addUser = data.userOverride.addUser ? data.userOverride.addUser : ''
const removeUser = data.userOverride.removeUser
? data.userOverride.removeUser
: ''
const start = data.userOverride.start ? data.userOverride.start : ''
const end = data.userOverride.end ? data.userOverride.end : ''
const addUser = data.userOverride?.addUser ?? ''
const removeUser = data.userOverride?.removeUser ?? ''
const start = data.userOverride?.start ?? ''
const end = data.userOverride?.end ?? ''


const isReplace = addUser && removeUser
const verb = addUser ? 'Added' : 'Removed'
const time = formatOverrideTime(start, end, zone)

const caption = isReplace
? `Replaced ${removeUser.name} from ${time}`
: `${verb} from ${time}`

return (
<FormDialog
title='Are you sure?'
confirm
subTitle={`This will delete the override for: ${
addUser ? addUser.name : removeUser.name
}`}
loading={deleteOverrideStatus.fetching}
errors={nonFieldErrors(deleteOverrideStatus.error)}
onClose={props.onClose}
onSubmit={() =>
deleteOverride(
{
input: [
{
type: 'userOverride',
id: props.overrideID,
},
],
},
{ additionalTypenames: ['UserOverride'] },
).then((res) => {
if (res.error) return
props.onClose()
})
}
form={<Typography variant='caption'>{caption}</Typography>}
/>
)
}