Merge pull request #882 from ubavic/fix/machine_name_validation_missing
fix: add unique machine name validation
This commit is contained in:
commit
3102c2bef0
2 changed files with 40 additions and 15 deletions
|
|
@ -109,8 +109,13 @@ const initialValues = {
|
||||||
|
|
||||||
const validationSchema = Yup.object().shape({
|
const validationSchema = Yup.object().shape({
|
||||||
name: Yup.string()
|
name: Yup.string()
|
||||||
.required()
|
.required('Machine name is required.')
|
||||||
.max(50)
|
.max(50)
|
||||||
|
.test(
|
||||||
|
'unique-name',
|
||||||
|
'Machine name is already in use.',
|
||||||
|
(value, context) => !context.options.context.machineNames.includes(value)
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
const MachineNameComponent = ({ nextStep, classes, setQrCode, setName }) => {
|
const MachineNameComponent = ({ nextStep, classes, setQrCode, setName }) => {
|
||||||
|
|
@ -125,6 +130,19 @@ const MachineNameComponent = ({ nextStep, classes, setQrCode, setName }) => {
|
||||||
onError: e => console.log(e)
|
onError: e => console.log(e)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const { data } = useQuery(GET_MACHINES)
|
||||||
|
const machineNames = R.map(R.prop('name'), data?.machines || {})
|
||||||
|
|
||||||
|
const uniqueNameValidator = value => {
|
||||||
|
try {
|
||||||
|
validationSchema.validateSync(value, {
|
||||||
|
context: { machineNames: machineNames }
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
return error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Info2 className={classes.nameTitle}>
|
<Info2 className={classes.nameTitle}>
|
||||||
|
|
@ -134,23 +152,26 @@ const MachineNameComponent = ({ nextStep, classes, setQrCode, setName }) => {
|
||||||
validateOnBlur={false}
|
validateOnBlur={false}
|
||||||
validateOnChange={false}
|
validateOnChange={false}
|
||||||
initialValues={initialValues}
|
initialValues={initialValues}
|
||||||
validationSchema={validationSchema}
|
validate={uniqueNameValidator}
|
||||||
onSubmit={({ name }) => {
|
onSubmit={({ name }) => {
|
||||||
setName(name)
|
setName(name)
|
||||||
register({ variables: { name } })
|
register({ variables: { name } })
|
||||||
}}>
|
}}>
|
||||||
<Form className={classes.form}>
|
{({ errors }) => (
|
||||||
<div>
|
<Form className={classes.form}>
|
||||||
<FastField
|
<div>
|
||||||
name="name"
|
<FastField
|
||||||
label="Enter machine name"
|
name="name"
|
||||||
component={TextInput}
|
label="Enter machine name"
|
||||||
/>
|
component={TextInput}
|
||||||
</div>
|
/>
|
||||||
<div className={classes.button}>
|
</div>
|
||||||
<Button type="submit">Submit</Button>
|
{errors && <P className={classes.errorMessage}>{errors.message}</P>}
|
||||||
</div>
|
<div className={classes.button}>
|
||||||
</Form>
|
<Button type="submit">Submit</Button>
|
||||||
|
</div>
|
||||||
|
</Form>
|
||||||
|
)}
|
||||||
</Formik>
|
</Formik>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@ import {
|
||||||
primaryColor,
|
primaryColor,
|
||||||
mainWidth,
|
mainWidth,
|
||||||
spring2,
|
spring2,
|
||||||
spring3
|
spring3,
|
||||||
|
errorColor
|
||||||
} from 'src/styling/variables'
|
} from 'src/styling/variables'
|
||||||
|
|
||||||
const { tl2, p } = typographyStyles
|
const { tl2, p } = typographyStyles
|
||||||
|
|
@ -122,6 +123,9 @@ const styles = {
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
flexDirection: 'col',
|
flexDirection: 'col',
|
||||||
alignItems: 'center'
|
alignItems: 'center'
|
||||||
|
},
|
||||||
|
errorMessage: {
|
||||||
|
color: errorColor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue