Skip to content

Commit

Permalink
avniproject/avni-client#1530 | Prohibit removal of existing 'Subject …
Browse files Browse the repository at this point in the history
…Type' filter, when there are other dependent filters
  • Loading branch information
himeshr committed Nov 28, 2024
1 parent 3d28b27 commit 084cd8e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
15 changes: 14 additions & 1 deletion src/common/service/DashboardService.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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";
Expand Down
23 changes: 16 additions & 7 deletions src/formDesigner/components/Dashboard/CreateEditDashboard.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 (
<Box boxShadow={2} p={3} bgcolor="background.paper">
<Title title={"Create Offline Dashboard"} />
Expand Down Expand Up @@ -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} />
Expand Down

0 comments on commit 084cd8e

Please sign in to comment.