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
Show file tree
Hide file tree
Changes from 1 commit
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
87 changes: 0 additions & 87 deletions web/src/app/admin/AdminDialog.js

This file was deleted.

111 changes: 111 additions & 0 deletions web/src/app/admin/AdminDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import React from 'react'
import List from '@mui/material/List'
import ListItem from '@mui/material/ListItem'
import ListItemText from '@mui/material/ListItemText'
import Typography from '@mui/material/Typography'
import { omit } from 'lodash'
import FormDialog from '../dialogs/FormDialog'
import { nonFieldErrors, fieldErrors } from '../util/errutil'
import Diff from '../util/Diff'
import {
DocumentNode,
OperationVariables,
TypedDocumentNode,
useMutation,
} from '@apollo/client'

interface Value {
id: string
oldValue: number
value: number
type: string
}

interface FieldValues {
[id: string]: string
}

interface AdminDialogProps {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
mutation: DocumentNode | TypedDocumentNode<any, OperationVariables>
tony-tvu marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/no-explicit-any
values: Value[] | any
tony-tvu marked this conversation as resolved.
Show resolved Hide resolved
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: { id: string }) => changeKeys.includes(v.id))
.map((orig: { id: string | number; value: number; type: string }) => ({
tony-tvu marked this conversation as resolved.
Show resolved Hide resolved
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: { value: string; type: string }) => {
c.value = c.value === '' && c.type === 'number' ? '0' : c.value
return omit(c, ['oldValue', 'type'])
}),
},
})
}
primaryActionLabel='Confirm'
errors={errs}
form={
<List data-cy='confirmation-diff'>
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
changes.map((c: any) => (
tony-tvu marked this conversation as resolved.
Show resolved Hide resolved
<ListItem divider key={c.id} data-cy={'diff-' + c.id}>
<ListItemText
disableTypography
secondary={
<Diff
oldValue={
c.type === 'stringList'
? c.oldValue.split(/\n/).join(', ')
: c.oldValue.toString()
}
newValue={
c.type === 'stringList'
? c.value.split(/\n/).join(', ')
: c.value.toString()
}
type={c.type === 'boolean' ? 'words' : 'chars'}
/>
}
>
<Typography>{c.id}</Typography>
</ListItemText>
</ListItem>
))
}
</List>
}
/>
)
}

export default AdminDialog