Skip to content

Commit

Permalink
Refactoring of dataset group selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Markionium committed Apr 6, 2017
1 parent 2f2e7b5 commit 6488b09
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 265 deletions.
35 changes: 18 additions & 17 deletions src/main/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ angular.module('PEPFAR.approvals')
function appController(periodService, $scope, currentUser, mechanismsService,
approvalLevelsService, toastr, AppManifest,
systemSettings, $translate, userApprovalLevels$,
organisationunitsService, $log, rx, workflowService) {
organisationunitsService, $log, rx, workflowService, dataSetGroupService) {
var self = this;
var vm = this;
var organisationUnit$ = new rx.ReplaySubject(1);
Expand Down Expand Up @@ -458,26 +458,27 @@ function appController(periodService, $scope, currentUser, mechanismsService,
}

//When the dataset group is changed update the filter types and the datasets
$scope.$on('DATASETGROUP.changed', function (event, dataSets) {
// Grab the period types from the Workflow
workflowService.setCurrentWorkflow(dataSets.get()[0].workflow);
dataSetGroupService.currentDataSetGroup$
.subscribe(function (dataSets) {
// Grab the period types from the Workflow
workflowService.setCurrentWorkflow(dataSets.get()[0].workflow);

$scope.details.dataSets = dataSets.get();
mechanismsService.categories = dataSets.getCategoryIds();
mechanismsService.dataSetIds = dataSets.getIds();
mechanismsService.dataSets = dataSets.get();
$scope.details.dataSets = dataSets.get();
mechanismsService.categories = dataSets.getCategoryIds();
mechanismsService.dataSetIds = dataSets.getIds();
mechanismsService.dataSets = dataSets.get();

// Reset the selection
$scope.details.currentSelection = [];
// Reset the selection
$scope.details.currentSelection = [];

// When the details are available load the mechanisms for the table
if (self.hasTableDetails()) {
self.showData = false;
self.deSelect();
}
// When the details are available load the mechanisms for the table
if (self.hasTableDetails()) {
self.showData = false;
self.deSelect();
}

self.updateViewButton();
});
self.updateViewButton();
});

function setOrganisationUnit() {
if (organisationunitsService.currentOrganisationUnit.name === 'Global') {
Expand Down
107 changes: 58 additions & 49 deletions src/main/datasets/datasetgroup-service.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
function dataSetGroupService($q, periodService, Restangular, workflowService, errorHandler) {
var service = this;
function dataSetGroupService($q, periodService, Restangular, workflowService, rx, errorHandler) {
var dataSetGroups = {};
var dataSetGroupNames = [];
var dataSetGroups$ = new rx.ReplaySubject(1);
var currentDataSetGroup$ = new rx.ReplaySubject(1);

this.getGroups = function () {
return dataSetGroups;
workflowService.workflows$
.flatMap(function (workflows) {
return rx.Observable.fromPromise(
loadDataSetsForWorkflows(workflows)
.then(matchDataSetsToWorkflows)
.then(setWorkflowsOntoService)
);
})
.subscribe(
function () {},
function (error) {
errorHandler.error(error.message);
}
);

return {
setCurrentDataSetGroup: setCurrentDataSetGroup,
dataSetGroups$: dataSetGroups$,
currentDataSetGroup$: currentDataSetGroup$,
getDataSetsForGroup: getDataSetsForGroup
};

this.filterDataSetsForUser = function (workflows) {
function setCurrentDataSetGroup(selectedDataSetGroup) {
var dataSetGroup = dataSetGroupFactory(getDataSetsForGroup(selectedDataSetGroup));

currentDataSetGroup$.onNext(dataSetGroup);
}

function filterDataSetsForUser(workflows) {
function onlyWorkflowsWithDataSets(workflow) {
return Array.isArray(workflow.dataSets) && workflow.dataSets.length > 0;
}
Expand All @@ -31,18 +56,16 @@ function dataSetGroupService($q, periodService, Restangular, workflowService, er

if (!(dataSetGroups && dataSetGroupNames[0] && dataSetGroups[dataSetGroupNames[0]])) {
errorHandler.warning('No dataset groups were found that your account can access. This could be the result of your account not having access to these datasets.', true);
} else {
dataSetGroups$.onNext(dataSetGroupNames);
}
};

this.getDataSetGroupNames = function () {
return dataSetGroupNames;
};
}

this.getDataSetsForGroup = function (dataSetGroupName) {
function getDataSetsForGroup(dataSetGroupName) {
if (dataSetGroups[dataSetGroupName]) {
return dataSetGroups[dataSetGroupName].dataSets;
}
};
}

function pickId(value) {
return value.id;
Expand Down Expand Up @@ -103,46 +126,32 @@ function dataSetGroupService($q, periodService, Restangular, workflowService, er
if (!Array.isArray(workflows)) {
return $q.reject('Could not properly load the Workflows from the api.');
}
service.filterDataSetsForUser(workflows);
}

workflowService.workflows$
.subscribe(
function (workflows) {
loadDataSetsForWorkflows(workflows)
.then(matchDataSetsToWorkflows)
.then(setWorkflowsOntoService);
},
function (error) {
errorHandler.error(error.message);
}
);
filterDataSetsForUser(workflows);
}
}

function dataSetGroupFactory() {
return function (dataSets) {
return {
get: function () {
return dataSets;
},
getIds: function () {
return _.pluck(dataSets, 'id');
},
getPeriodTypes: function () {
return _.uniq(_.pluck(dataSets, 'periodType'));
},
getCategoryIds: function () {
var categoriesFromCategoryCombos;

categoriesFromCategoryCombos = _.pluck(_.pluck(dataSets, 'categoryCombo'), 'categories');
categoriesFromCategoryCombos = _.flatten(categoriesFromCategoryCombos);
categoriesFromCategoryCombos = _.pluck(categoriesFromCategoryCombos, 'id');

return _.uniq(categoriesFromCategoryCombos);
}
};
function dataSetGroupFactory(dataSets) {
return {
get: function () {
return dataSets;
},
getIds: function () {
return _.pluck(dataSets, 'id');
},
getPeriodTypes: function () {
return _.uniq(_.pluck(dataSets, 'periodType'));
},
getCategoryIds: function () {
var categoriesFromCategoryCombos;

categoriesFromCategoryCombos = _.pluck(_.pluck(dataSets, 'categoryCombo'), 'categories');
categoriesFromCategoryCombos = _.flatten(categoriesFromCategoryCombos);
categoriesFromCategoryCombos = _.pluck(categoriesFromCategoryCombos, 'id');

return _.uniq(categoriesFromCategoryCombos);
}
};
}

angular.module('PEPFAR.approvals').service('dataSetGroupService', dataSetGroupService);
angular.module('PEPFAR.approvals').factory('dataSetGroupFactory', dataSetGroupFactory);
angular.module('PEPFAR.approvals').factory('dataSetGroupService', dataSetGroupService);
44 changes: 17 additions & 27 deletions src/main/datasets/datasetgroupselector-directive.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function dataSetGroupSelectorDirective(dataSetGroupService, dataSetGroupFactory) {
function dataSetGroupSelectorDirective(dataSetGroupService) {
return {
restrict: 'E',
replace: true,
Expand All @@ -10,35 +10,25 @@ function dataSetGroupSelectorDirective(dataSetGroupService, dataSetGroupFactory)
selectedDataSetGroup: undefined
};

function updateDataSetGroups(datasetGroups) {
if (angular.isArray(datasetGroups)) {
scope.dataset.groups = datasetGroups;
scope.dataset.selectedDataSetGroup = scope.dataset.groups[0];
}
}

scope.$watch(function () {
return dataSetGroupService.getDataSetGroupNames();
}, function (newVal, oldVal) {
if (newVal !== oldVal) {
updateDataSetGroups(newVal);
}
});

scope.$watch(function () {
return scope.dataset.selectedDataSetGroup;
}, function (newVal, oldVal) {
if (newVal !== oldVal) {
scope.$emit(
'DATASETGROUP.changed',
dataSetGroupFactory(dataSetGroupService.getDataSetsForGroup(scope.dataset.selectedDataSetGroup))
);
}
});

scope.onChange = function ($item) {
// Set scope to keep dropdown in sync with dropdown
scope.dataset.selectedDataSetGroup = $item;
dataSetGroupService.setCurrentDataSetGroup($item);
};

dataSetGroupService
.dataSetGroups$
.take(1)
.map(function (datasetGroups) {
if (angular.isArray(datasetGroups)) {
scope.dataset.groups = datasetGroups;

// Fire onChange to set to the first group
scope.onChange(scope.dataset.groups[0]);
}
})
.safeApply(scope)
.subscribe();
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/manifest.webapp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.3.1",
"version": "1.4.0",
"name": "Data Approval",
"description": "Approvals app for PEPFAR",
"icons": {
Expand All @@ -11,7 +11,7 @@
"company": "DHIS2 Core Team",
"email": "[email protected]"
},
"launch_path": "index.html?v=1.3.1",
"launch_path": "index.html?v=1.4.0",
"default_locale": "en",
"activities": {
"dhis": {
Expand Down
15 changes: 6 additions & 9 deletions src/test/mocks/datasetgroup-service_mock.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
function dataSetGroupServiceMock() {
function dataSetGroupServiceMock(rx) {
return {
datasetGroups: [ 'MER', 'EA' ],
getDataSetGroupNames: function () {
return this.datasetGroups;
},
getDataSetsForGroup: function () {
return [];
}
}
setCurrentDataSetGroup: function () {},
dataSetGroups$: rx.Observable.just(['MER', 'EA'])
// currentDataSetGroup$: currentDataSetGroup$,
// getDataSetsForGroup: getDataSetsForGroup
};
}
39 changes: 0 additions & 39 deletions src/test/specs/dataset/datasetgroup-factory_spec.js

This file was deleted.

Loading

0 comments on commit 6488b09

Please sign in to comment.