diff --git a/src/common/service/DashboardService.js b/src/common/service/DashboardService.js index b6b7f19c6..f613c443b 100644 --- a/src/common/service/DashboardService.js +++ b/src/common/service/DashboardService.js @@ -1,4 +1,4 @@ -import { find, isEmpty } from "lodash"; +import { every, find, isEmpty, some } from "lodash"; import http from "../utils/httpClient"; import { CustomFilter, DashboardFilterConfig, GroupSubjectTypeFilter, ObservationBasedFilter } from "openchs-models"; import EntityService from "./EntityService"; @@ -42,9 +42,22 @@ class DashboardService { message: `Standard report cards of types related to Approval, Comments, Tasks and Checklist currently doesn't support any filter other than Address. Incompatible "Card <=> Filter" combinations are as follows: ${message}` }); } + this.validateForMissingSubjectTypeFilter(dashboard.filters, errors); return errors; } + static validateForMissingSubjectTypeFilter(dashboardFilters, errors) { + if ( + some(dashboardFilters, x => x.filterConfig.subjectType) && + every(dashboardFilters, x => x.filterConfig.type !== CustomFilter.type.SubjectType) + ) { + errors.push({ + key: "MISSING_SUBJECT_TYPE_FILTER", + message: "One or more filters configured are dependent on the SubjectType filter." + }); + } + } + static save(dashboard, edit, id) { const url = edit ? `${dashboardEndpoint}/${id}` : "/web/dashboard"; const methodName = edit ? "put" : "post"; diff --git a/src/formDesigner/components/Dashboard/CreateEditDashboard.js b/src/formDesigner/components/Dashboard/CreateEditDashboard.js index 5405ea781..5166ac0cd 100644 --- a/src/formDesigner/components/Dashboard/CreateEditDashboard.js +++ b/src/formDesigner/components/Dashboard/CreateEditDashboard.js @@ -1,7 +1,7 @@ import React from "react"; import { DashboardReducer } from "./DashboardReducer"; import http from "../../../common/utils/httpClient"; -import { isEmpty, isNil } from "lodash"; +import { isEmpty, isNil, reject } from "lodash"; import { DocumentationContainer } from "../../../common/components/DocumentationContainer"; import Grid from "@material-ui/core/Grid"; import Button from "@material-ui/core/Button"; @@ -91,6 +91,19 @@ const CreateEditDashboard = ({ edit, history, operationalModules, getOperational if (!OperationalModules.isLoaded(operationalModules)) return null; + const onFilterDelete = selectedFilter => { + const errors = []; + DashboardService.validateForMissingSubjectTypeFilter(reject(dashboard.filters, x => x.uuid === selectedFilter.uuid), errors); + if (!isEmpty(errors)) { + setError(errors); + return; + } + dispatch({ + type: "deleteFilter", + payload: { selectedFilter } + }); + }; + return ( @@ -169,16 +182,12 @@ const CreateEditDashboard = ({ edit, history, operationalModules, getOperational editAction={filter => { setSelectedFilter(filter); }} - deleteAction={selectedFilter => - dispatch({ - type: "deleteFilter", - payload: { selectedFilter } - }) - } + deleteAction={onFilterDelete} /> </Grid> <p /> {getErrorByKey(error, "SERVER_ERROR")} + {getErrorByKey(error, "MISSING_SUBJECT_TYPE_FILTER")} <Grid container direction={"row"}> <Grid item xs={1}> <SaveComponent name="save" onSubmit={onSave} />