Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
fix: 🐛 android form validation didn't check the variant name
Browse files Browse the repository at this point in the history
When creating a new android variant, only serverKey and serverID where
validated. Added some utility function to make dealing with the
validator simpler.
  • Loading branch information
ziccardi authored and secondsun committed Sep 16, 2020
1 parent 08948d0 commit 81b6130
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
35 changes: 15 additions & 20 deletions src/application/VariantForms/AndroidVariantForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component } from 'react';

import { TextInput, Button, Form, FormGroup } from '@patternfly/react-core';
import { Button, Form } from '@patternfly/react-core';
import { AndroidVariant, Variant } from '@aerogear/unifiedpush-admin-client';
import { FormField } from '../ApplicationDetail/panels/FormField';
import {
Expand All @@ -10,6 +10,7 @@ import {
validatorBuilder,
} from 'json-data-validator';
import { MultiEvaluationResult } from 'json-data-validator/build/src/Rule';
import { formIsValid, validatorToPF4Status } from '../../utils/ValidatorUtils';

interface State {
serverKey: string;
Expand Down Expand Up @@ -90,44 +91,38 @@ export class AndroidVariantForm extends Component<Props, State> {
<Form className="AndroidVariantForm" isHorizontal>
<FormField
fieldId={'server-key'}
component="textinput"
label={'Push Network'}
helperText={'Server Key1'}
helperText={'Server Key'}
onChange={value => updateField('serverKey', value)}
helperTextInvalid={
this.state.formValidation?.getEvaluationResult('serverKey')?.message
}
validated={
this.state.formValidation
? this.state.formValidation.isValid('serverKey')
? 'success'
: 'error'
: 'default'
}
validated={validatorToPF4Status(
this.state.formValidation,
'serverKey'
)}
/>

<FormField
fieldId={'server-key'}
component="textinput"
helperText={'Sender ID1'}
helperText={'Sender ID'}
onChange={value => updateField('senderID', value)}
helperTextInvalid={
this.state.formValidation?.getEvaluationResult('senderID')?.message
}
validated={
this.state.formValidation
? this.state.formValidation.isValid('senderID')
? 'success'
: 'error'
: 'default'
}
validated={validatorToPF4Status(
this.state.formValidation,
'senderID'
)}
/>
<div className="variantFormButtons">
<Button
className="dialogBtn"
onClick={save}
isDisabled={
!this.state.formValidation || !this.state.formValidation.valid
!this.props.variantName ||
this.props.variantName.length === 0 ||
!formIsValid(this.state.formValidation)
}
>
Create
Expand Down
14 changes: 14 additions & 0 deletions src/utils/ValidatorUtils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MultiEvaluationResult } from 'json-data-validator/build/src/Rule';

export const validatorToPF4Status: (
status: MultiEvaluationResult | undefined,
field: string
) => 'success' | 'error' | 'default' = (
status: MultiEvaluationResult | undefined,
field: string
) => (status ? (status.isValid(field) ? 'success' : 'error') : 'default');

export const formIsValid: (
status: MultiEvaluationResult | undefined
) => boolean = (status: MultiEvaluationResult | undefined) =>
!!(status?.valid && status.valid);

0 comments on commit 81b6130

Please sign in to comment.