Skip to content

Commit

Permalink
Merge pull request #146 from EyeSeeTea/refactor/ou-subtree
Browse files Browse the repository at this point in the history
Refactor select org unit children
  • Loading branch information
adrianq authored Sep 17, 2019
2 parents 52cbffc + 900c7d5 commit 411aa12
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 69 deletions.
10 changes: 5 additions & 5 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2019-09-13T17:17:43.512Z\n"
"PO-Revision-Date: 2019-09-13T17:17:43.512Z\n"
"POT-Creation-Date: 2019-09-17T10:45:09.309Z\n"
"PO-Revision-Date: 2019-09-17T10:45:09.309Z\n"

msgid "Metadata Synchronization"
msgstr ""
Expand Down Expand Up @@ -187,6 +187,9 @@ msgstr ""
msgid "Last update"
msgstr ""

msgid "Select with children subtree"
msgstr ""

msgid "Metadata type"
msgstr ""

Expand Down Expand Up @@ -452,9 +455,6 @@ msgstr ""
msgid "Indicators Synchronization"
msgstr ""

msgid "Select with children subtree"
msgstr ""

msgid "Organisation Units Synchronization"
msgstr ""

Expand Down
35 changes: 30 additions & 5 deletions src/components/metadata-table/MetadataTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Checkbox, FormControlLabel, withStyles } from "@material-ui/core";
import Dropdown from "../dropdown/Dropdown";
import { d2ModelFactory } from "../../models/d2ModelFactory";
import { d2BaseModelDetails } from "../../utils/d2";
import { listByIds } from "../../logic/metadata";
import { listByIds, getOrgUnitSubtree } from "../../logic/metadata";

const styles = {
checkbox: {
Expand Down Expand Up @@ -62,17 +62,41 @@ class MetadataTable extends React.Component {
groupFilterData: [],
levelFilterData: [],
metadataIds: [],
orgUnitChildren: [],
showOnlySelectedItems: false,
tableKey: Math.random(),
};

selectChildren = async selectedOUs => {
const { d2 } = this.props;

const ids = new Set();
for (const selectedOU of selectedOUs) {
const subtree = await getOrgUnitSubtree(d2, selectedOU.id);
subtree.forEach(id => ids.add(id));
}

this.setState({ orgUnitChildren: Array.from(ids), tableKey: Math.random() });
};

actions = [
{
name: "details",
text: i18n.t("Details"),
multiple: false,
type: "details",
},
{
name: "select-children",
text: i18n.t("Select with children subtree"),
multiple: true,
onClick: this.selectChildren,
icon: "done_all",
isActive: () => {
const { model } = this.state;
return model.getMetadataType() === "organisationUnit";
},
},
];

parseModels = memoize(models => {
Expand Down Expand Up @@ -256,18 +280,19 @@ class MetadataTable extends React.Component {
};

list = (...params) => {
const { initialSelection } = this.props;
const { model, showOnlySelectedItems, metadataIds } = this.state;
if (!model.listMethod || showOnlySelectedItems) {
return listByIds(...params, _.union(initialSelection, metadataIds));
return listByIds(...params, metadataIds);
} else {
return model.listMethod(...params);
}
};

render() {
const { d2, initialSelection, ...rest } = this.props;
const { model, filters, tableKey } = this.state;
const { model, filters, tableKey, orgUnitChildren } = this.state;

const selection = _.union(initialSelection, orgUnitChildren);

return (
<ObjectsTable
Expand All @@ -278,12 +303,12 @@ class MetadataTable extends React.Component {
detailsFields={model.getDetails()}
pageSize={20}
initialSorting={model.getInitialSorting()}
initialSelection={selection}
actions={this.actions}
list={this.list}
onSelectionChange={this.onSelectionChange}
customFiltersComponent={this.renderCustomFilters}
customFilters={filters}
initialSelection={initialSelection}
forceSelectionColumn={true}
{...rest}
/>
Expand Down
6 changes: 5 additions & 1 deletion src/components/rules-list-page/SyncRulesPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ class SyncRulesPage extends React.Component {
sortable: true,
getValue: ({ enabled }) => (enabled ? i18n.t("Enabled") : i18n.t("Disabled")),
},
{ name: "lastExecuted", text: i18n.t("Last executed") },
{
name: "lastExecuted",
text: i18n.t("Last executed"),
sortable: true,
},
];

detailsFields = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from "react";
import PropTypes from "prop-types";
import i18n from "@dhis2/d2-i18n";
import _ from "lodash";
import { withSnackbar, withLoading } from "d2-ui-components";
import SyncIcon from "@material-ui/icons/Sync";
import { withRouter } from "react-router-dom";
Expand All @@ -22,7 +21,6 @@ class GenericSynchronizationPage extends React.Component {
history: PropTypes.object.isRequired,
title: PropTypes.string.isRequired,
isDelete: PropTypes.bool,
onSelectionChange: PropTypes.func,
};

state = {
Expand All @@ -41,16 +39,13 @@ class GenericSynchronizationPage extends React.Component {
};

changeSelection = metadataIds => {
const { onSelectionChange } = this.props;
if (onSelectionChange) onSelectionChange(metadataIds);
this.setState({ metadataIds });
};

startSynchronization = () => {
const { initialSelection } = this.props;
const { metadataIds } = this.state;

if (_.union(initialSelection, metadataIds).length > 0) {
if (metadataIds.length > 0) {
this.setState({ syncDialogOpen: true });
} else {
this.props.snackbar.error(
Expand All @@ -69,10 +64,9 @@ class GenericSynchronizationPage extends React.Component {
};

handleSynchronization = async targetInstances => {
const { isDelete, loading, d2, initialSelection } = this.props;
const { metadataIds: stateMetadata } = this.state;
const { isDelete, loading, d2 } = this.props;
const { metadataIds } = this.state;

const metadataIds = _.union(initialSelection, stateMetadata);
const action = isDelete ? startDelete : startSynchronization;
loading.show(true, i18n.t("Synchronizing metadata"));

Expand Down
50 changes: 1 addition & 49 deletions src/components/synchronization-page/OrganisationUnitPage.jsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,25 @@
import React from "react";
import PropTypes from "prop-types";
import i18n from "@dhis2/d2-i18n";

import {
OrganisationUnitGroupModel,
OrganisationUnitGroupSetModel,
OrganisationUnitModel,
} from "../../models/d2Model";
import { getOrgUnitSubtree } from "../../logic/metadata";
import GenericSynchronizationPage from "./GenericSynchronizationPage";

export default class OrganisationUnitPage extends React.Component {
static propTypes = {
d2: PropTypes.object.isRequired,
};

state = {
children: [],
metadataTableKey: Math.random(),
};

models = [OrganisationUnitModel, OrganisationUnitGroupModel, OrganisationUnitGroupSetModel];

onSelectionChange = children => this.setState({ children });

selectChildren = async selectedOUs => {
const { d2 } = this.props;
const { children } = this.state;

const ids = new Set(children);
for (const selectedOU of selectedOUs) {
const subtree = await getOrgUnitSubtree(d2, selectedOU.id);
subtree.forEach(id => ids.add(id));
}
this.setState({ children: Array.from(ids), metadataTableKey: Math.random() });
};

actions = [
{
name: "details",
text: i18n.t("Details"),
multiple: false,
type: "details",
},
{
name: "select-children",
text: i18n.t("Select with children subtree"),
multiple: true,
onClick: this.selectChildren,
icon: "done_all",
},
];

render() {
const { d2 } = this.props;
const { children, metadataTableKey } = this.state;

const title = i18n.t("Organisation Units Synchronization");

return (
<GenericSynchronizationPage
key={metadataTableKey}
d2={d2}
models={this.models}
title={title}
actions={this.actions}
initialSelection={children}
onSelectionChange={this.onSelectionChange}
/>
);
return <GenericSynchronizationPage d2={d2} models={this.models} title={title} />;
}
}

0 comments on commit 411aa12

Please sign in to comment.