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 ServiceEditDialog to ts #2571

Closed
wants to merge 1 commit into from
Closed
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
77 changes: 0 additions & 77 deletions web/src/app/services/ServiceEditDialog.js

This file was deleted.

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

import FormDialog from '../dialogs/FormDialog'
import ServiceLabelForm from './ServiceLabelForm'
import Spinner from '../loading/components/Spinner'
import { Label } from '../../schema'

const mutation = gql`
mutation ($input: SetLabelInput!) {
setLabel(input: $input)
}
`
const query = gql`
query ($serviceID: ID!) {
service(id: $serviceID) {
id
labels {
key
value
}
}
}
`

export default function ServiceLabelEditDialog(props: {
serviceID: string
labelKey: string
onClose: () => void
}): JSX.Element {
const { onClose, labelKey, serviceID } = props
const [value, setValue] = useState<Label | null>(null)
const [{ data, fetching }] = useQuery({
query,
variables: {
serviceID: props.serviceID,
},
})

const [updateLabelStatus, updateLabel] = useMutation(mutation)

if (fetching || !data) return <Spinner />

const defaultValue = {
key: data.service.labels.find((l: Label) => l.key === labelKey).key,
value: data.service.labels.find((l: Label) => l.key === labelKey).value,
}

return (
<FormDialog
title='Update Label Value'
loading={updateLabelStatus.fetching}
errors={nonFieldErrors(updateLabelStatus.error)}
onClose={onClose}
onSubmit={() => {
if (!value) {
return onClose()
}
updateLabel(
{
input: {
...value,
target: { type: 'service', id: serviceID },
},
},
{ additionalTypenames: ['Service'] },
).then((res) => {
if (res.error) return
props.onClose()
})
}}
form={
<ServiceLabelForm
errors={fieldErrors(updateLabelStatus.error)}
editValueOnly
disabled={updateLabelStatus.fetching}
value={value || { key: defaultValue.key, value: defaultValue.value }}
onChange={(value) => setValue(value)}
/>
}
/>
)
}