Skip to content

Commit

Permalink
#1235 - fixed reordering logic using drag and drop. refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
petmongrels committed Jun 5, 2024
1 parent ca1455e commit 0eb8d07
Show file tree
Hide file tree
Showing 17 changed files with 2,876 additions and 228 deletions.
10 changes: 6 additions & 4 deletions src/common/model/reports/WebDashboard.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import _ from "lodash";
import WebDashboardSection from "./WebDashboardSection";
import CollectionUtil from "../../utils/CollectionUtil";

class WebDashboard {
static createNew() {
return { name: "", description: "", sections: [], filters: [] };
}

static getSections(dashboard) {
return _.sortBy(dashboard.sections.filter(x => !x.voided), "displayOrder");
}

static removeSection(dashboard, section) {
const dashboardSection = _.find(dashboard.sections, x => x.uuid === section.uuid);
dashboardSection.voided = true;
Expand All @@ -20,10 +25,7 @@ class WebDashboard {
}

static reOrderSections(dashboard, sourceIndex, destIndex) {
const result = [...dashboard.sections];
const [removed] = result.splice(sourceIndex, 1);
result.splice(destIndex, 0, removed);
dashboard.sections = result;
CollectionUtil.switchItemPosition(dashboard.sections, sourceIndex, destIndex, "displayOrder");
return { ...dashboard };
}

Expand Down
23 changes: 14 additions & 9 deletions src/common/model/reports/WebDashboardSection.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { ModelGeneral as General } from "openchs-models";
import _ from "lodash";
import WebDashboardSectionCardMapping from "./WebDashboardSectionCardMapping";
import CollectionUtil from "../../utils/CollectionUtil";

class WebDashboardSection {
dashboardSectionCardMappings;

static getReportCardMappings(section) {
return _.sortBy(section.dashboardSectionCardMappings.filter(x => !x.voided), "displayOrder");
}

static getReportCards(section) {
return section.dashboardSectionCardMappings.filter(x => !x.voided).map(x => x.card);
return WebDashboardSection.getReportCardMappings(section).map(x => x.card);
}

static getReportCardMapping(section, cardId) {
return _.find(section.dashboardSectionCardMappings, x => x.card.id === cardId);
}

static newSection() {
Expand All @@ -24,15 +33,14 @@ class WebDashboardSection {
static removeCard(section, card) {
const mapping = _.find(section.dashboardSectionCardMappings, x => x.card.uuid === card.uuid);
mapping.voided = true;
mapping.displayOrder = -1;
section.dashboardSectionCardMappings = [...section.dashboardSectionCardMappings];
return { ...section };
}

static reorderCards(section, startIndex, endIndex) {
const result = [...section.dashboardSectionCardMappings];
const [removed] = result.splice(startIndex, 1);
result.splice(endIndex, 0, removed);
section.dashboardSectionCardMappings = result;
const reportCardMappings = WebDashboardSection.getReportCardMappings(section);
CollectionUtil.switchItemPosition(reportCardMappings, startIndex, endIndex, "displayOrder");
return { ...section };
}

Expand All @@ -48,10 +56,7 @@ class WebDashboardSection {
) + 1;

cards.forEach(card => {
section.dashboardSectionCardMappings.push({
card: card,
displayOrder: nextDisplayOrder
});
section.dashboardSectionCardMappings.push(WebDashboardSectionCardMapping.newCard(card, nextDisplayOrder));
nextDisplayOrder++;
});

Expand Down
38 changes: 38 additions & 0 deletions src/common/model/reports/WebDashboardSection.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import WebDashboardSection from "./WebDashboardSection";
import WebDashboardSectionCardMapping from "./WebDashboardSectionCardMapping";
import WebReportCardFactory from "./WebReportCardFactory";
import { assert } from "chai";

it("should reorder cards down to up", function() {
const section = {
dashboardSectionCardMappings: [
WebDashboardSectionCardMapping.newCard(WebReportCardFactory.create({ id: 2 }), 1),
WebDashboardSectionCardMapping.newCard(WebReportCardFactory.create({ id: 3 }), 2),
WebDashboardSectionCardMapping.newCard(WebReportCardFactory.create({ id: 1 }), 3),
WebDashboardSectionCardMapping.newCard(WebReportCardFactory.create({ id: 4 }), 4)
]
};

const reOrderedSection = WebDashboardSection.reorderCards(section, 2, 1);
assert.equal(WebDashboardSection.getReportCardMapping(reOrderedSection, 2).displayOrder, 1);
assert.equal(WebDashboardSection.getReportCardMapping(reOrderedSection, 1).displayOrder, 2);
assert.equal(WebDashboardSection.getReportCardMapping(reOrderedSection, 3).displayOrder, 3);
assert.equal(WebDashboardSection.getReportCardMapping(reOrderedSection, 4).displayOrder, 4);
});

it("should reorder cards up to down", function() {
const section = {
dashboardSectionCardMappings: [
WebDashboardSectionCardMapping.newCard(WebReportCardFactory.create({ id: 2 }), 1),
WebDashboardSectionCardMapping.newCard(WebReportCardFactory.create({ id: 3 }), 2),
WebDashboardSectionCardMapping.newCard(WebReportCardFactory.create({ id: 1 }), 3),
WebDashboardSectionCardMapping.newCard(WebReportCardFactory.create({ id: 4 }), 4)
]
};

const reOrderedSection = WebDashboardSection.reorderCards(section, 1, 2);
assert.equal(WebDashboardSection.getReportCardMapping(reOrderedSection, 2).displayOrder, 1);
assert.equal(WebDashboardSection.getReportCardMapping(reOrderedSection, 1).displayOrder, 2);
assert.equal(WebDashboardSection.getReportCardMapping(reOrderedSection, 3).displayOrder, 3);
assert.equal(WebDashboardSection.getReportCardMapping(reOrderedSection, 4).displayOrder, 4);
});
8 changes: 8 additions & 0 deletions src/common/model/reports/WebDashboardSectionCardMapping.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
class WebDashboardSectionCardMapping {
static newCard(card, displayOrder) {
return {
card: card,
displayOrder: displayOrder,
voided: false
};
}

static toResources(dashboardSectionCardMappings) {
return dashboardSectionCardMappings.map(dashboardSectionCardMapping => {
return {
Expand Down
13 changes: 13 additions & 0 deletions src/common/model/reports/WebReportCardFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import WebReportCard from "../WebReportCard";

class WebReportCardFactory {
static create({ id, uuid, name }) {
const webReportCard = new WebReportCard();
webReportCard.id = id;
webReportCard.uuid = uuid;
webReportCard.name = name;
return webReportCard;
}
}

export default WebReportCardFactory;
13 changes: 6 additions & 7 deletions src/common/service/DashboardService.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { find, isEmpty, sortBy } from "lodash";
import { find, isEmpty } from "lodash";
import http from "../utils/httpClient";
import { DashboardFilterConfig, GroupSubjectTypeFilter, ObservationBasedFilter, CustomFilter } from "openchs-models";
import { CustomFilter, DashboardFilterConfig, GroupSubjectTypeFilter, ObservationBasedFilter } from "openchs-models";
import EntityService from "./EntityService";
import _ from "lodash";
import WebReportCard from "../model/WebReportCard";
import WebStandardReportCardType from "../model/WebStandardReportCardType";
import WebDashboard from "../model/reports/WebDashboard";
Expand Down Expand Up @@ -51,10 +50,6 @@ class DashboardService {

static mapDashboardFromResource(dashboardResponse, operationalModules) {
const dashboard = { ...dashboardResponse };
_.forEach(dashboard.sections, section => {
section.cards = sortBy(section.cards, "displayOrder");
});

dashboard.filters = dashboardResponse.filters.map(x => {
const filter = { ...x };
const filterConfig = new DashboardFilterConfig();
Expand Down Expand Up @@ -113,6 +108,10 @@ class DashboardService {
const methodName = card.isNew() ? "post" : "put";
return http[methodName](url, card.toResource());
}

static getAllReportCards() {
return http.get(`/web/reportCard`).then(res => res.data);
}
}

export default DashboardService;
20 changes: 20 additions & 0 deletions src/common/utils/CollectionUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ class CollectionUtil {
{}
);
}

static switchItemPosition(array, startIndex, endIndex, fieldToUpdate) {
const workingItems = [...array];
let removedElement;
array.forEach((mapping, index) => {
if (index === startIndex) {
removedElement = _.pullAt(workingItems, index)[0];
}
});

array.forEach((mapping, index) => {
if (index === endIndex) {
workingItems.splice(index, 0, removedElement);
}
});

workingItems.forEach((item, index) => {
item[fieldToUpdate] = index + 1;
});
}
}

export default CollectionUtil;
5 changes: 3 additions & 2 deletions src/formDesigner/common/ResourceShowView.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ const ResourceShowView = ({
render,
mapResource = _.identity,
userInfo,
editPrivilegeType
editPrivilegeType,
defaultResource
}) => {
const [resource, setResource] = React.useState({});
const [resource, setResource] = React.useState(defaultResource);
const [editAlert, setEditAlert] = useState(false);

useEffect(() => {
Expand Down
7 changes: 5 additions & 2 deletions src/formDesigner/components/Dashboard/CreateEditDashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ const CreateEditDashboard = ({ edit, history, operationalModules, getOperational

const onSave = () => {
const errors = DashboardService.validate(dashboard);
if (!isEmpty(errors)) setError(errors);
if (!isEmpty(errors)) {
setError(errors);
return;
}

DashboardService.save(dashboard, edit, props.match.params.id)
.then(data => setId(data.id))
Expand Down Expand Up @@ -141,7 +144,7 @@ const CreateEditDashboard = ({ edit, history, operationalModules, getOperational
{getErrorByKey(error, "EMPTY_SECTIONS")}
</Grid>
<Grid item>
<CreateEditDashboardSections sections={dashboard.sections} dispatch={dispatch} history={history} error={error} />
<CreateEditDashboardSections sections={WebDashboard.getSections(dashboard)} dispatch={dispatch} history={history} error={error} />
</Grid>
{getErrorByKey(error, "EMPTY_CARDS")}
<br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isEmpty } from "lodash";
import { DragNDropComponent } from "../../common/DragNDropComponent";
import PropTypes from "prop-types";
import WebDashboardSection from "../../../common/model/reports/WebDashboardSection";
import { dashboardReducerActions } from "./DashboardReducer";

class CreateEditDashboardSectionCards extends Component {
constructor(props) {
Expand All @@ -15,15 +16,17 @@ class CreateEditDashboardSectionCards extends Component {

static propTypes = {
section: PropTypes.object.isRequired,
sectionUpdated: PropTypes.func.isRequired
dispatch: PropTypes.func.isRequired
};

onDragEnd(result) {
if (!result.destination) {
return;
}
const section = WebDashboardSection.reorderCards(this.props.section, result.source.index, result.destination.index);
this.props.sectionUpdated(section);
this.props.dispatch({
type: dashboardReducerActions.reorderCards,
payload: { section: this.props.section, startIndex: result.source.index, endIndex: result.destination.index }
});
}

renderCard(card) {
Expand Down
Loading

0 comments on commit 0eb8d07

Please sign in to comment.