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

feat(app): validate factory mode slideout input #15025

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
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
Expand Up @@ -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'

Expand All @@ -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'])

Expand All @@ -58,6 +61,8 @@ export function FactoryModeSlideout({
)
const isOEMMode = oemModeSetting?.value ?? null

const last = sn?.substring(sn.length - 4)
Copy link
Contributor

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?

Copy link
Contributor Author

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 if sn is null to prevent opening the slideout

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed


const [currentStep, setCurrentStep] = React.useState<number>(1)
const [toggleValue, setToggleValue] = React.useState<boolean>(false)
const [file, setFile] = React.useState<File | null>(null)
Expand Down Expand Up @@ -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)()
}

Expand Down Expand Up @@ -153,7 +190,11 @@ export function FactoryModeSlideout({
footer={
<>
{currentStep === 1 ? (
<PrimaryButton onClick={handleSubmitFactoryPassword} width="100%">
<PrimaryButton
disabled={errors.factoryModeInput != null}
Copy link
Contributor

Choose a reason for hiding this comment

The 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}
Expand Down Expand Up @@ -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 && ' '}
Expand All @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import { TertiaryButton } from '../../../../atoms/buttons'
interface FactoryModeProps {
isRobotBusy: boolean
setShowFactoryModeSlideout: React.Dispatch<React.SetStateAction<boolean>>
sn: string | null
}

export function FactoryMode({
isRobotBusy,
setShowFactoryModeSlideout,
sn,
}: FactoryModeProps): JSX.Element {
const { t } = useTranslation('device_settings')

Expand All @@ -37,7 +39,7 @@ export function FactoryMode({
</StyledText>
</Box>
<TertiaryButton
disabled={isRobotBusy}
disabled={isRobotBusy || sn == null}
marginLeft={SPACING_AUTO}
onClick={() => {
setShowFactoryModeSlideout(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { DeviceResetSlideout } from './AdvancedTab/AdvancedTabSlideouts/DeviceRe
import { DeviceResetModal } from './AdvancedTab/AdvancedTabSlideouts/DeviceResetModal'
import { FactoryModeSlideout } from './AdvancedTab/AdvancedTabSlideouts/FactoryModeSlideout'
import { handleUpdateBuildroot } from './UpdateBuildroot'
import { UNREACHABLE } from '../../../redux/discovery'
import { getRobotSerialNumber, UNREACHABLE } from '../../../redux/discovery'
import { getTopPortalEl } from '../../../App/portal'
import { useIsEstopNotDisengaged } from '../../../resources/devices/hooks/useIsEstopNotDisengaged'

Expand Down Expand Up @@ -89,6 +89,7 @@ export function RobotSettingsAdvanced({
getRobotSettings(state, robotName)
)
const reachable = robot?.status !== UNREACHABLE
const sn = robot?.status != null ? getRobotSerialNumber(robot) : null

const [isRobotReachable, setIsRobotReachable] = React.useState<boolean>(
reachable
Expand Down Expand Up @@ -143,6 +144,7 @@ export function RobotSettingsAdvanced({
isRobotBusy={isRobotBusy || isEstopNotDisengaged}
onCloseClick={() => setShowFactoryModeSlideout(false)}
robotName={robotName}
sn={sn}
/>
)}
{showDeviceResetSlideout && (
Expand Down Expand Up @@ -215,6 +217,7 @@ export function RobotSettingsAdvanced({
<FactoryMode
isRobotBusy={isRobotBusy || isEstopNotDisengaged}
setShowFactoryModeSlideout={setShowFactoryModeSlideout}
sn={sn}
/>
</>
) : null}
Expand Down
Loading