Skip to content

Commit

Permalink
avniproject#1219|DEA fail if no option provided for duration
Browse files Browse the repository at this point in the history
  • Loading branch information
shraddha761 committed Jun 15, 2024
1 parent bbd85cf commit 1fd03d8
Show file tree
Hide file tree
Showing 4 changed files with 2,688 additions and 166 deletions.
38 changes: 24 additions & 14 deletions src/dataEntryApp/components/DurationFormElement.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Fragment, useState } from "react";
import { TextField, FormLabel, FormControl } from "@material-ui/core";
import { TextField, FormLabel, FormControl, FormHelperText } from "@material-ui/core";
import { isEmpty, find } from "lodash";
import { CompositeDuration } from "avni-models";
import { useTranslation } from "react-i18next";
Expand All @@ -11,24 +11,30 @@ const useStyles = makeStyles(theme => ({
margin: theme.spacing(1),
width: "20ch"
}
},
errorText: {
color: theme.palette.error.main,
margin: theme.spacing(1),
marginTop: 0
}
}));

const DurationFormElement = ({ duration, mandatory, name, update, validationResult }) => {
const DurationFormElement = ({ duration, mandatory, name, update, errorMessage }) => {
const [localVal, setLocalVal] = useState((duration && duration.durationValue) || "");

const { t } = useTranslation();
const classes = useStyles();

return (
<Fragment>
{!isEmpty(errorMessage) && <FormHelperText className={classes.errorText}>{t(errorMessage)}</FormHelperText>}
<TextField
label={t(duration.durationUnit)}
required={mandatory}
name={name}
type="number"
value={localVal}
helperText={validationResult && duration.isEmpty && t(validationResult.messageKey, validationResult.extra)}
error={validationResult && !validationResult.success && duration.isEmpty}
helperText={t(duration.durationUnit)}
error={!isEmpty(errorMessage)}
onChange={e => {
const value = e.target.value;
isEmpty(value) ? setLocalVal("") : setLocalVal(value);
Expand All @@ -42,12 +48,12 @@ const DurationFormElement = ({ duration, mandatory, name, update, validationResu
const CompositeDurationFormElement = ({ formElement: fe, value, update, validationResults, uuid }) => {
const compositeDuration = value ? CompositeDuration.fromObs(value) : CompositeDuration.fromOpts(fe.durationOptions);
const classes = useStyles();

const { t } = useTranslation();
const validationResult = find(
validationResults,
({ formIdentifier, questionGroupIndex }) => formIdentifier === uuid && questionGroupIndex === fe.questionGroupIndex
);

// Find the validation result for the current form element
const validationResult = find(validationResults, function({ formIdentifier, questionGroupIndex }) {
return formIdentifier === uuid && questionGroupIndex === fe.questionGroupIndex;
});

return (
<FormControl>
Expand All @@ -56,15 +62,19 @@ const CompositeDurationFormElement = ({ formElement: fe, value, update, validati
{fe.mandatory ? "*" : ""}
</FormLabel>
<form className={classes.root} noValidate autoComplete="off">
{fe.durationOptions.map((durationUnit, index) => {
{fe.durationOptions.map(function(durationUnit, index) {
var duration = compositeDuration.findByUnit(durationUnit);
// Get the specific error message for the current duration unit
var errorMessage = (validationResult && validationResult.durationErrors && validationResult.durationErrors[durationUnit]) || "";

return (
<DurationFormElement
key={index}
mandatory={fe.mandatory}
name={fe.name + index}
duration={compositeDuration.findByUnit(durationUnit)}
validationResult={validationResult}
update={val => {
duration={duration}
errorMessage={errorMessage}
update={function(val) {
isEmpty(val)
? update(compositeDuration.changeValueByUnit(durationUnit, ""))
: update(compositeDuration.changeValueByUnit(durationUnit, val));
Expand Down
88 changes: 19 additions & 69 deletions src/formDesigner/components/FormSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ class FormSettings extends Component {
}

static addEncounterTypeErrorIfMissing(errorsList, formMap, index) {
FormSettings.addErrorIfMissing(
errorsList,
formMap,
"encounterTypeUuid",
index,
"encounter type"
);
FormSettings.addErrorIfMissing(errorsList, formMap, "encounterTypeUuid", index, "encounter type");
}

static addErrorIfMissing(errorsList, formMap, fieldKey, index, fieldName) {
Expand Down Expand Up @@ -111,6 +105,7 @@ class FormSettings extends Component {
FormSettings.addSubjectTypeErrorIfMissing(errorsList, formMap, index);
FormSettings.addEncounterTypeErrorIfMissing(errorsList, formMap, index);
}

if (existingMappings.includes(uniqueString)) {
errorsList["existingMapping"][index] = "Same mapping already exist";
}
Expand Down Expand Up @@ -241,10 +236,7 @@ class FormSettings extends Component {
programNameElement(index) {
return (
<FormControl fullWidth margin="dense">
<AvniFormLabel
label={"Program Name"}
toolTipKey={"APP_DESIGNER_FORM_MAPPING_PROGRAM_NAME"}
/>
<AvniFormLabel label={"Program Name"} toolTipKey={"APP_DESIGNER_FORM_MAPPING_PROGRAM_NAME"} />
<Select
name="programUuid"
value={this.state.formMappings[index].programUuid}
Expand Down Expand Up @@ -297,10 +289,7 @@ class FormSettings extends Component {
subjectTypeElement(index) {
return (
<FormControl fullWidth margin="dense">
<AvniFormLabel
label={"Subject Type"}
toolTipKey={"APP_DESIGNER_FORM_MAPPING_SUBJECT_TYPE"}
/>
<AvniFormLabel label={"Subject Type"} toolTipKey={"APP_DESIGNER_FORM_MAPPING_SUBJECT_TYPE"} />
<Select
name="subjectTypeUuid"
value={this.state.formMappings[index].subjectTypeUuid}
Expand Down Expand Up @@ -332,16 +321,11 @@ class FormSettings extends Component {
return (
<FormControl fullWidth margin="dense">
<>
<AvniFormLabel
label={"Encounter Type"}
toolTipKey={"APP_DESIGNER_FORM_MAPPING_ENCOUNTER_TYPE"}
/>
<AvniFormLabel label={"Encounter Type"} toolTipKey={"APP_DESIGNER_FORM_MAPPING_ENCOUNTER_TYPE"} />
<Select
name="encounterTypeUuid"
value={this.state.formMappings[index].encounterTypeUuid}
onChange={event =>
this.handleMappingChange(index, "encounterTypeUuid", event.target.value)
}
onChange={event => this.handleMappingChange(index, "encounterTypeUuid", event.target.value)}
>
{this.state.data.encounterTypes != null &&
this.state.data.encounterTypes.map(encounterType => (
Expand All @@ -361,9 +345,7 @@ class FormSettings extends Component {
return (
this.state.errors.hasOwnProperty("unselectedData") &&
this.state.errors["unselectedData"].hasOwnProperty(propertyName + index) && (
<FormHelperText error>
{this.state.errors["unselectedData"][propertyName + index]}
</FormHelperText>
<FormHelperText error>{this.state.errors["unselectedData"][propertyName + index]}</FormHelperText>
)
);
}
Expand Down Expand Up @@ -421,29 +403,14 @@ class FormSettings extends Component {
</CopyToClipboard>
</FormControl>
)}
<AvniFormLabel
label={"Form name"}
style={{ fontSize: "12px" }}
toolTipKey={"APP_DESIGNER_FORM_MAPPING_FORM_NAME"}
/>
<AvniFormLabel label={"Form name"} style={{ fontSize: "12px" }} toolTipKey={"APP_DESIGNER_FORM_MAPPING_FORM_NAME"} />
{this.state.name}
<FormControl fullWidth margin="dense">
<AvniFormLabel
label={"Form Type"}
toolTipKey={"APP_DESIGNER_FORM_MAPPING_FORM_TYPE"}
/>
<Select
id="formType"
name="formType"
value={this.state.formTypeInfo}
onChange={this.onChangeField.bind(this)}
required
>
<AvniFormLabel label={"Form Type"} toolTipKey={"APP_DESIGNER_FORM_MAPPING_FORM_TYPE"} />
<Select id="formType" name="formType" value={this.state.formTypeInfo} onChange={this.onChangeField.bind(this)} required>
{this.formTypes()}
</Select>
{this.state.errors.formTypeInfo && (
<FormHelperText error>{this.state.errors.formTypeInfo.formType}</FormHelperText>
)}
{this.state.errors.formTypeInfo && <FormHelperText error>{this.state.errors.formTypeInfo.formType}</FormHelperText>}
</FormControl>

{notChecklistItemBased &&
Expand Down Expand Up @@ -476,47 +443,30 @@ class FormSettings extends Component {
<Grid item sm={3} style={{ marginTop: 40 }}>
<AvniSwitch
checked={this.state.formMappings[index].enableApproval}
onChange={event =>
this.handleMappingChange(
index,
"enableApproval",
event.target.checked
)
}
onChange={event => this.handleMappingChange(index, "enableApproval", event.target.checked)}
name="Enable Approval"
toolTipKey={"APP_DESIGNER_ENABLE_APPROVAL"}
/>
</Grid>
)}
<Grid item sm={1}>
<IconButton
aria-label="delete"
onClick={event => this.removeMapping(index)}
style={{ marginTop: 10 }}
>
<IconButton aria-label="delete" onClick={event => this.removeMapping(index)} style={{ marginTop: 10 }}>
<DeleteIcon fontSize="inherit" />
</IconButton>
</Grid>
</Grid>
{this.state.errors.hasOwnProperty("existingMapping") &&
this.state.errors["existingMapping"].hasOwnProperty(index) && (
<FormControl fullWidth margin="dense">
<FormHelperText error>
{this.state.errors["existingMapping"][index]}
</FormHelperText>
</FormControl>
)}
{this.state.errors.hasOwnProperty("existingMapping") && this.state.errors["existingMapping"].hasOwnProperty(index) && (
<FormControl fullWidth margin="dense">
<FormHelperText error>{this.state.errors["existingMapping"][index]}</FormHelperText>
</FormControl>
)}
</div>
)
);
})}
</form>
{notChecklistItemBased && (
<Button
color="primary"
onClick={event => this.addMapping(programBased, encounterTypes)}
style={{ marginTop: 10 }}
>
<Button color="primary" onClick={event => this.addMapping(programBased, encounterTypes)} style={{ marginTop: 10 }}>
Add mapping
</Button>
)}
Expand Down
6 changes: 6 additions & 0 deletions src/formDesigner/views/FormDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,11 @@ class FormDetails extends Component {
});
}
const { declarativeRuleHolder, validationError } = this.getDeclarativeRuleValidationError(fe.declarativeRule);

const isDurationOptionMissing =
(fe.concept.dataType === "Date" || fe.concept.dataType === "Duration") &&
(!fe.keyValues["durationOptions"] || fe.keyValues["durationOptions"].length === 0);

if (
!fe.voided &&
(fe.name === "" ||
Expand All @@ -695,6 +700,7 @@ class FormDetails extends Component {
(fe.concept.dataType === "Video" && parseInt(fe.keyValues.durationLimitInSecs) < 0) ||
(fe.concept.dataType === "Image" && parseInt(fe.keyValues.maxHeight) < 0) ||
(fe.concept.dataType === "Image" && parseInt(fe.keyValues.maxWidth) < 0) ||
isDurationOptionMissing ||
!areValidFormatValuesValid(fe) ||
!_.isEmpty(validationError))
) {
Expand Down
Loading

0 comments on commit 1fd03d8

Please sign in to comment.