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/admin: convert file to typescript #2346

Merged
merged 2 commits into from
May 2, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react'
import p from 'prop-types'
import List from '@mui/material/List'
import ListItem from '@mui/material/ListItem'
import ListItemText from '@mui/material/ListItemText'
Expand All @@ -8,42 +7,59 @@ import { omit } from 'lodash'
import FormDialog from '../dialogs/FormDialog'
import { nonFieldErrors, fieldErrors } from '../util/errutil'
import Diff from '../util/Diff'
import { useMutation } from '@apollo/client'
import { DocumentNode, useMutation } from '@apollo/client'
import { ConfigValue } from '../../schema'

function AdminDialog(props) {
interface FieldValues {
[id: string]: string
}

interface AdminDialogProps {
mutation: DocumentNode
values: ConfigValue[]
fieldValues: FieldValues
onClose: () => void
onComplete: () => void
}

function AdminDialog(props: AdminDialogProps): JSX.Element {
const [commit, { error }] = useMutation(props.mutation, {
onCompleted: props.onComplete,
})
const changeKeys = Object.keys(props.fieldValues)
const changes = props.values
.filter((v) => changeKeys.includes(v.id))
.filter((v: { id: string }) => changeKeys.includes(v.id))
.map((orig) => ({
id: orig.id,
oldValue: orig.value,
value: props.fieldValues[orig.id],
type: orig.type || typeof orig.value,
}))

const nonFieldErrs = nonFieldErrors(error).map((e) => ({
message: e.message,
}))
const fieldErrs = fieldErrors(error).map((e) => ({
message: `${e.field}: ${e.message}`,
}))
const errs = nonFieldErrs.concat(fieldErrs)

return (
<FormDialog
title={`Apply Configuration Change${changes.length > 1 ? 's' : ''}?`}
onClose={props.onClose}
onSubmit={() =>
commit({
variables: {
input: changes.map((c) => {
input: changes.map((c: { value: string; type: string }) => {
c.value = c.value === '' && c.type === 'number' ? '0' : c.value
return omit(c, ['oldValue', 'type'])
}),
},
})
}
primaryActionLabel='Confirm'
errors={nonFieldErrors(error).concat(
fieldErrors(error).map((e) => ({
message: `${e.field}: ${e.message}`,
})),
)}
errors={errs}
form={
<List data-cy='confirmation-diff'>
{changes.map((c) => (
Expand Down Expand Up @@ -76,12 +92,4 @@ function AdminDialog(props) {
)
}

AdminDialog.propTypes = {
mutation: p.object.isRequired,
values: p.array.isRequired,
fieldValues: p.object.isRequired,
onClose: p.func.isRequired,
onComplete: p.func.isRequired,
}

export default AdminDialog