Skip to content

Commit

Permalink
[frontend] do not execute mutation if empty field
Browse files Browse the repository at this point in the history
  • Loading branch information
SarahBocognano committed Jun 3, 2024
1 parent 5e93376 commit e5c4838
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import TextField from '../../../../components/TextField';
import { commitMutation, defaultCommitMutation } from '../../../../relay/environment';
import useUserMetric from '../../../../utils/hooks/useUserMetric';
import DateTimePickerField from '../../../../components/DateTimePickerField';
import { isNotEmptyField } from '../../../../utils/utils';

export const individualHeightMutation = graphql`
mutation HeightFieldIndividualMutation($id: ID!, $input: [EditInput]!) {
Expand Down Expand Up @@ -73,39 +74,43 @@ export const HeightFieldEdit: FunctionComponent<HeightFieldEditProps> = ({
name={`${name}.${index}.measure`}
label={t_i18n(`Height (${lengthPrimaryUnit})`)}
onSubmit={(_: string, measure: string) => {
commitMutation({
...defaultCommitMutation,
mutation: individualHeightMutation,
variables: {
id,
input: {
key: 'height',
value: [heightToPivotFormat(measure)],
object_path: `/height/${height.index}/measure`,
operation: 'replace',
if (isNotEmptyField(measure)) {
commitMutation({
...defaultCommitMutation,
mutation: individualHeightMutation,
variables: {
id,
input: {
key: 'height',
value: [heightToPivotFormat(measure)],
object_path: `/height/${height.index}/measure`,
operation: 'replace',
},
},
},
});
});
}
}}
/>
<Field
component={DateTimePickerField}
id={`height_date_${index}`}
name={`${name}.${index}.date_seen`}
onSubmit={(_: string, date_seen: string) => {
commitMutation({
...defaultCommitMutation,
mutation: individualHeightMutation,
variables: {
id,
input: {
key: 'height',
value: [date_seen],
object_path: `/height/${height.index}/date_seen`,
operation: 'replace',
if (isNotEmptyField(date_seen)) {
commitMutation({
...defaultCommitMutation,
mutation: individualHeightMutation,
variables: {
id,
input: {
key: 'height',
value: [date_seen],
object_path: `/height/${height.index}/date_seen`,
operation: 'replace',
},
},
},
});
});
}
}}
textFieldProps={{
label: t_i18n('Date Seen'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { commitMutation, defaultCommitMutation } from '../../../../relay/environ
import useUserMetric from '../../../../utils/hooks/useUserMetric';
import DateTimePickerField from '../../../../components/DateTimePickerField';
import { GenericContext } from '../model/GenericContextModel';
import { isNotEmptyField } from '../../../../utils/utils';

export const individualWeightMutation = graphql`
mutation WeightFieldIndividualMutation($id: ID!, $input: [EditInput]!) {
Expand Down Expand Up @@ -160,39 +161,43 @@ export const WeightFieldEdit: FunctionComponent<WeightFieldEditProps> = ({
name={`${name}.${index}.measure`}
label={t_i18n(`Weight (${weightPrimaryUnit})`)}
onSubmit={(_: string, measure: string) => {
commitMutation({
...defaultCommitMutation,
mutation: individualWeightMutation,
variables: {
id,
input: {
key: 'weight',
value: [weightToPivotFormat(measure)],
object_path: `/weight/${weight.index}/measure`,
operation: 'replace',
if (isNotEmptyField(measure)) {
commitMutation({
...defaultCommitMutation,
mutation: individualWeightMutation,
variables: {
id,
input: {
key: 'weight',
value: [weightToPivotFormat(measure)],
object_path: `/weight/${weight.index}/measure`,
operation: 'replace',
},
},
},
});
});
}
}}
/>
<Field
component={DateTimePickerField}
name={`${name}.${index}.date_seen`}
id={`weight_date_${index}`}
onSubmit={(_: string, date_seen: string) => {
commitMutation({
...defaultCommitMutation,
mutation: individualWeightMutation,
variables: {
id,
input: {
key: 'weight',
value: [date_seen],
object_path: `/weight/${weight.index}/date_seen`,
operation: 'replace',
if (isNotEmptyField(date_seen)) {
commitMutation({
...defaultCommitMutation,
mutation: individualWeightMutation,
variables: {
id,
input: {
key: 'weight',
value: [date_seen],
object_path: `/weight/${weight.index}/date_seen`,
operation: 'replace',
},
},
},
});
});
}
}}
textFieldProps={{
label: t_i18n('Date Seen'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ ThreatActorIndividualEditionBiographicsComponentProps
threatActorIndividualEditionBiographicsFragment,
threatActorIndividualRef,
);
const dateSeenValidator = Yup.date()
.when('measure', {
is: (value: number) => value !== undefined && value !== null,
then: (schema) => schema.required(t_i18n('This field is required')),
})
.typeError(t_i18n('The value must be a datetime (yyyy-MM-dd hh:mm (a|p)m)'));

const basicShape = {
eye_color: Yup.string()
Expand All @@ -107,13 +101,15 @@ ThreatActorIndividualEditionBiographicsComponentProps
weight: Yup.array().of(
Yup.object().shape({
measure: Yup.number().required(t_i18n('This field is required')),
date_seen: dateSeenValidator,
date_seen: Yup.date().required(t_i18n('This field is required'))
.typeError(t_i18n('The value must be a datetime (yyyy-MM-dd hh:mm (a|p)m)')),
}),
),
height: Yup.array().of(
Yup.object().shape({
measure: Yup.number().required(t_i18n('This field is required')),
date_seen: dateSeenValidator,
date_seen: Yup.date().required(t_i18n('This field is required'))
.typeError(t_i18n('The value must be a datetime (yyyy-MM-dd hh:mm (a|p)m)')),
}),
),
};
Expand Down

0 comments on commit e5c4838

Please sign in to comment.