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/services: convert HeartbeatMonitorCreateDialog to ts and use urql #2480

Merged
merged 7 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
55 changes: 0 additions & 55 deletions web/src/app/services/HeartbeatMonitorCreateDialog.js

This file was deleted.

59 changes: 59 additions & 0 deletions web/src/app/services/HeartbeatMonitorCreateDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { useState } from 'react'
import { gql, useMutation } from 'urql'
import { fieldErrors, nonFieldErrors } from '../util/errutil'

import FormDialog from '../dialogs/FormDialog'
import HeartbeatMonitorForm, { Value } from './HeartbeatMonitorForm'

const createMutation = gql`
mutation ($input: CreateHeartbeatMonitorInput!) {
createHeartbeatMonitor(input: $input) {
id
}
}
`

export default function HeartbeatMonitorCreateDialog(props: {
serviceID: string
onClose: () => void
}): JSX.Element {
const [value, setValue] = useState<Value>({ name: '', timeoutMinutes: 15 })
const [createHeartbeatStatus, createHeartbeat] = useMutation(createMutation)

return (
<FormDialog
maxWidth='sm'
title='Create New Heartbeat Monitor'
loading={createHeartbeatStatus.fetching}
errors={nonFieldErrors(createHeartbeatStatus.error)}
onClose={props.onClose}
onSubmit={() =>
createHeartbeat(
{
input: {
name: value.name,
timeoutMinutes: value.timeoutMinutes,
serviceID: props.serviceID,
},
},
{ additionalTypenames: ['HeartbeatMonitor'] },
).then((result) => {
if (!result.error) {
props.onClose()
}
})
}
form={
<HeartbeatMonitorForm
errors={fieldErrors(createHeartbeatStatus.error).map((f) => ({
...f,
field: f.field === 'timeout' ? 'timeoutMinutes' : f.field,
}))}
disabled={createHeartbeatStatus.fetching}
value={value}
onChange={(value: Value) => setValue(value)}
/>
}
/>
)
}
13 changes: 7 additions & 6 deletions web/src/app/services/HeartbeatMonitorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Grid from '@mui/material/Grid'
import TextField from '@mui/material/TextField'
import { FormContainer, FormField } from '../forms'
import NumberField from '../util/NumberField'
import { FieldError } from '../util/errutil'

function clampTimeout(val: string): number | string {
if (!val) return ''
Expand All @@ -12,19 +13,19 @@ function clampTimeout(val: string): number | string {
// need to have the min be 1 here so you can type `10`
return Math.min(Math.max(1, num), 9000)
}
interface Value {
export interface Value {
name: string
timeoutMinutes: [number, string]
timeoutMinutes: [number, string] | number
tony-tvu marked this conversation as resolved.
Show resolved Hide resolved
}
interface HeartbeatMonitorFormProps {
value: Value

errors: {
field: 'name' | 'timeoutMinutes'
message: string
}[]
errors: FieldError[]

onChange: (val: Value) => void

// can be deleted when FormContainer.js is converted to ts
disabled: boolean
}

export default function HeartbeatMonitorForm(
Expand Down
7 changes: 6 additions & 1 deletion web/src/cypress/integration/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,16 @@ function testServices(screen: ScreenFormat): void {
it('should create a monitor', () => {
const name = c.word({ length: 5 }) + ' Monitor'
const timeoutMinutes = (Math.trunc(Math.random() * 10) + 5).toString()
const invalidName = 'a'

cy.pageFab()

cy.dialogForm({ name: invalidName, timeoutMinutes })
cy.dialogClick('Submit')
cy.get('body').should('contain', 'Must be at least 2 characters')

cy.dialogForm({ name, timeoutMinutes })
cy.dialogFinish('Submit')
cy.dialogFinish('Retry')

cy.get('li').should('contain', name).should('contain', timeoutMinutes)
})
Expand Down