Skip to content

Commit

Permalink
ui: combine create and edit uik dialogs (#3994)
Browse files Browse the repository at this point in the history
* combine create and edit uik dialogs

* pr comments
  • Loading branch information
Forfold authored Jul 16, 2024
1 parent 3874e7d commit eb3d9f3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 116 deletions.
18 changes: 7 additions & 11 deletions web/src/app/services/UniversalKey/UniversalKeyRuleConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import React, { Suspense, useState } from 'react'
import { Button, Card } from '@mui/material'
import { Add } from '@mui/icons-material'
import FlatList, { FlatListListItem } from '../../lists/FlatList'
import UniversalKeyRuleCreateDialog from './UniversalKeyRuleCreateDialog'
import UniversalKeyRuleEditDialog from './UniversalKeyRuleEditDialog'
import UniversalKeyRuleDialog from './UniversalKeyRuleDialog'
import UniversalKeyRuleRemoveDialog from './UniversalKeyRuleRemoveDialog'
import OtherActions from '../../util/OtherActions'
import { gql, useQuery } from 'urql'
Expand Down Expand Up @@ -93,15 +92,12 @@ export default function UniversalKeyRuleList(
</Card>

<Suspense>
{create && (
<UniversalKeyRuleCreateDialog
onClose={() => setCreate(false)}
keyID={props.keyID}
/>
)}
{edit && (
<UniversalKeyRuleEditDialog
onClose={() => setEdit('')}
{(create || edit) && (
<UniversalKeyRuleDialog
onClose={() => {
setCreate(false)
setEdit('')
}}
keyID={props.keyID}
ruleID={edit}
/>
Expand Down
78 changes: 0 additions & 78 deletions web/src/app/services/UniversalKey/UniversalKeyRuleCreateDialog.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import React, { useState } from 'react'
import { gql, useMutation, useQuery } from 'urql'
import FormDialog from '../../dialogs/FormDialog'
import UniversalKeyRuleForm from './UniversalKeyRuleForm'
import { gql, useMutation, useQuery } from 'urql'
import { IntegrationKey, KeyRuleInput } from '../../../schema'
import { getNotice } from './utils'
import { useErrorConsumer } from '../../util/ErrorConsumer'

interface UniversalKeyRuleEditDialogProps {
interface UniversalKeyRuleDialogProps {
keyID: string
ruleID: string
onClose: () => void
default?: boolean

ruleID?: string // if present, we are editing
default?: boolean // used when creating default action
}

const query = gql`
const editQuery = gql`
query UniversalKeyPage($keyID: ID!, $ruleID: ID!) {
integrationKey(id: $keyID) {
id
Expand Down Expand Up @@ -43,50 +44,60 @@ const mutation = gql`
}
`

export default function UniversalKeyRuleEditDialog(
props: UniversalKeyRuleEditDialogProps,
export default function UniversalKeyRuleDialogProps(
props: UniversalKeyRuleDialogProps,
): JSX.Element {
const [q] = useQuery<{
integrationKey: IntegrationKey
}>({
query,
query: editQuery,
variables: {
keyID: props.keyID,
ruleID: props.ruleID,
},
pause: !props.ruleID,
})
if (q.error) throw q.error

// shouldn't happen due to suspense
if (!q.data) throw new Error('failed to load data')
if (props.ruleID && !q.data) throw new Error('failed to load data')

const rule = q.data.integrationKey.config.oneRule
if (!rule) throw new Error('rule not found')
const rule = q.data?.integrationKey.config.oneRule
if (props.ruleID && !rule) throw new Error('rule not found')

const [value, setValue] = useState<KeyRuleInput>(rule)
const [value, setValue] = useState<KeyRuleInput>({
id: rule?.id ?? undefined,
name: rule?.name ?? '',
description: rule?.description ?? '',
conditionExpr: rule?.conditionExpr ?? '',
continueAfterMatch: rule?.continueAfterMatch ?? false,
actions: rule?.actions ?? [],
})
const [m, commit] = useMutation(mutation)

const [hasConfirmed, setHasConfirmed] = useState(false)
const [hasSubmitted, setHasSubmitted] = useState(false)
const noActionsNoConf = value.actions.length === 0 && !hasConfirmed

const errs = useErrorConsumer(m.error)
const form = (
<UniversalKeyRuleForm
value={value}
onChange={setValue}
nameError={errs.getErrorByField(/Rules.+\.Name/)}
descriptionError={errs.getErrorByField(/Rules.+\.Description/)}
conditionError={errs.getErrorByPath(
'updateKeyConfig.input.setRule.conditionExpr',
)}
/>
const unknownErrors = errs.remainingLegacyCallback()
const nameError = errs.getErrorByField(/Rules.+\.Name/)
const descError = errs.getErrorByField(/Rules.+\.Description/)
const conditionError = errs.getErrorByPath(
'updateKeyConfig.input.setRule.conditionExpr',
)

return (
<FormDialog
title={props.default ? 'Edit Default Actions' : 'Edit Rule'}
maxWidth={props.default ? 'sm' : 'lg'}
title={
props.ruleID
? props.default
? 'Edit Default Actions'
: 'Edit Rule'
: 'Create Rule'
}
maxWidth='lg'
onClose={props.onClose}
errors={unknownErrors}
onSubmit={() => {
if (noActionsNoConf) {
setHasSubmitted(true)
Expand All @@ -107,8 +118,15 @@ export default function UniversalKeyRuleEditDialog(
props.onClose()
})
}}
form={form}
errors={errs.remainingLegacyCallback()}
form={
<UniversalKeyRuleForm
value={value}
onChange={setValue}
nameError={nameError}
descriptionError={descError}
conditionError={conditionError}
/>
}
notices={getNotice(hasSubmitted, hasConfirmed, setHasConfirmed)}
/>
)
Expand Down

0 comments on commit eb3d9f3

Please sign in to comment.