-
Notifications
You must be signed in to change notification settings - Fork 178
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
feat(app): validate factory mode slideout input #15025
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import { FileUpload } from '../../../../../molecules/FileUpload' | |
import { UploadInput } from '../../../../../molecules/UploadInput' | ||
import { restartRobot } from '../../../../../redux/robot-admin' | ||
|
||
import type { FieldError, Resolver } from 'react-hook-form' | ||
import type { RobotSettingsField } from '@opentrons/api-client' | ||
import type { Dispatch } from '../../../../../redux/types' | ||
|
||
|
@@ -36,17 +37,19 @@ interface FactoryModeSlideoutProps { | |
isRobotBusy: boolean | ||
onCloseClick: () => void | ||
robotName: string | ||
sn: string | null | ||
} | ||
|
||
interface FormValues { | ||
passwordInput: string | ||
factoryModeInput: string | ||
} | ||
|
||
export function FactoryModeSlideout({ | ||
isExpanded, | ||
isRobotBusy, | ||
onCloseClick, | ||
robotName, | ||
sn, | ||
}: FactoryModeSlideoutProps): JSX.Element { | ||
const { t } = useTranslation(['device_settings', 'shared', 'branded']) | ||
|
||
|
@@ -58,6 +61,8 @@ export function FactoryModeSlideout({ | |
) | ||
const isOEMMode = oemModeSetting?.value ?? null | ||
|
||
const last = sn?.substring(sn.length - 4) | ||
|
||
const [currentStep, setCurrentStep] = React.useState<number>(1) | ||
const [toggleValue, setToggleValue] = React.useState<boolean>(false) | ||
const [file, setFile] = React.useState<File | null>(null) | ||
|
@@ -86,22 +91,54 @@ export function FactoryModeSlideout({ | |
}, | ||
}) | ||
|
||
const validate = ( | ||
data: FormValues, | ||
errors: Record<string, FieldError> | ||
): Record<string, FieldError> => { | ||
const factoryModeInput = data.factoryModeInput | ||
let errorMessage: string | undefined | ||
if (factoryModeInput !== last) { | ||
errorMessage = t('invalid_password') | ||
} | ||
|
||
const updatedErrors = | ||
errorMessage != null | ||
? { | ||
...errors, | ||
factoryModeInput: { | ||
type: 'error', | ||
message: errorMessage, | ||
}, | ||
} | ||
: errors | ||
return updatedErrors | ||
} | ||
|
||
const resolver: Resolver<FormValues> = values => { | ||
let errors = {} | ||
errors = validate(values, errors) | ||
return { values, errors } | ||
} | ||
|
||
const { | ||
handleSubmit, | ||
clearErrors, | ||
control, | ||
formState: { errors }, | ||
trigger, | ||
handleSubmit, | ||
} = useForm({ | ||
defaultValues: { | ||
passwordInput: '', | ||
factoryModeInput: '', | ||
}, | ||
mode: 'onSubmit', | ||
resolver, | ||
reValidateMode: 'onSubmit', | ||
}) | ||
const onSubmit = (data: FormValues): void => { | ||
|
||
const onSubmit = (): void => { | ||
setCurrentStep(2) | ||
} | ||
|
||
const handleSubmitFactoryPassword = (): void => { | ||
// TODO: validation and errors: PLAT-281 | ||
void handleSubmit(onSubmit)() | ||
} | ||
|
||
|
@@ -153,7 +190,11 @@ export function FactoryModeSlideout({ | |
footer={ | ||
<> | ||
{currentStep === 1 ? ( | ||
<PrimaryButton onClick={handleSubmitFactoryPassword} width="100%"> | ||
<PrimaryButton | ||
disabled={errors.factoryModeInput != null} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, so this handles empty input, cool |
||
onClick={handleSubmitFactoryPassword} | ||
width="100%" | ||
> | ||
{t('shared:next')} | ||
</PrimaryButton> | ||
) : null} | ||
|
@@ -182,15 +223,15 @@ export function FactoryModeSlideout({ | |
<Flex flexDirection={DIRECTION_COLUMN}> | ||
<Controller | ||
control={control} | ||
name="passwordInput" | ||
name="factoryModeInput" | ||
render={({ field, fieldState }) => ( | ||
<InputField | ||
id="passwordInput" | ||
name="passwordInput" | ||
id="factoryModeInput" | ||
name="factoryModeInput" | ||
type="text" | ||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => { | ||
field.onChange(e) | ||
trigger('passwordInput') | ||
clearErrors() | ||
}} | ||
value={field.value} | ||
error={fieldState.error?.message && ' '} | ||
|
@@ -199,13 +240,13 @@ export function FactoryModeSlideout({ | |
/> | ||
)} | ||
/> | ||
{errors.passwordInput != null ? ( | ||
{errors.factoryModeInput != null ? ( | ||
<StyledText | ||
as="label" | ||
color={COLORS.red50} | ||
marginTop={SPACING.spacing4} | ||
> | ||
{errors.passwordInput.message} | ||
{errors.factoryModeInput.message} | ||
</StyledText> | ||
) : null} | ||
</Flex> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this throw if
const sn
is null?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't think it's necessary -
last
will be undefined in that case and never pass validation. i think the better move is to disable the initial "Setup mode" button ifsn
is null to prevent opening the slideoutThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed