-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathServiceLabelEditDialog.tsx
88 lines (80 loc) · 2.05 KB
/
ServiceLabelEditDialog.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 },
})
const [updateLabelStatus, updateLabel] = useMutation(mutation)
if (!data && fetching) {
return <Spinner />
}
const defaultValue = {
key: labelKey,
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: {
key: labelKey,
value: value?.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 || defaultValue}
onChange={(value: Label) => setValue(value)}
/>
}
/>
)
}