Skip to content

Commit

Permalink
simplified logic to load last view program in setup modal
Browse files Browse the repository at this point in the history
  • Loading branch information
kflemin committed Jan 11, 2024
1 parent 7c9520f commit 4d4f91a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 55 deletions.
42 changes: 15 additions & 27 deletions seed/static/seed/js/controllers/insights_program_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,42 +82,30 @@ angular.module('BE.seed.controller.insights_program', []).controller('insights_p
});
// on modal close
modalInstance.result.then((program) => {
// 1) change selection if no programs existed before
// 2) change selection if the selected one in insights no longer exists
// 3) reload if the selected one still exists (just in case)
// 4) do nothing
// re-fetch compliance metrics
compliance_metric_service.get_compliance_metrics($scope.organization.id).then((data) => {
$scope.compliance_metrics = data;
const metric_ids = _.map($scope.compliance_metrics, 'id');
if ($scope.selected_metric === null && $scope.compliance_metrics.length > 0) {
// case 1
// change selection to last selected in modal and reload
if ($scope.compliance_metrics.length > 0) {
if (program != null) {
$scope.compliance_metric = $scope.compliance_metrics.find((cm) => cm.id === program.id);
$scope.selected_metric = program.id;
} else {
// this should not happen, but just in case use the 1st one
$scope.compliance_metric = $scope.compliance_metrics[0];
$scope.selected_metric = $scope.compliance_metric.id;
}
} else if ($scope.selected_metric && metric_ids.indexOf($scope.selected_metric) === -1) {
// case 2
if (program != null) {
$scope.selected_metric = program.id;
// attempt to keep the selected metric
$scope.compliance_metric = $scope.compliance_metrics.find((cm) => cm.id === $scope.selected_metric);
} else if ($scope.compliance_metrics.length > 0) {
// this should not happen, but just in case use the 1st one
$scope.compliance_metric = $scope.compliance_metrics[0];
$scope.selected_metric = $scope.compliance_metric.id;
} else {
// load nothing
$scope.compliance_metric = {};
$scope.selected_metric = null;
if ($scope.compliance_metric == null) {
// load first metric b/c selected metric no longer exists
$scope.compliance_metric = $scope.compliance_metrics[0];
$scope.selected_metric = $scope.compliance_metric.id;
}
}
} else if ($scope.selected_metric) {
// case 3
// otherwise reload the data for selected one in case it changed
$scope.compliance_metric = $scope.compliance_metrics.find((cm) => cm.id === $scope.selected_metric);
} else {
// load nothing
$scope.compliance_metric = {};
$scope.selected_metric = null;
$scope.data = null;
}

$scope.updateSelectedMetric();
});
});
Expand Down
44 changes: 16 additions & 28 deletions seed/static/seed/js/controllers/insights_property_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,42 +113,30 @@ angular.module('BE.seed.controller.insights_property', []).controller('insights_
});
// on modal close
modalInstance.result.then((program) => {
// 1) change selection if no programs existed before
// 2) change selection if the selected one in insights no longer exists
// 3) reload if the selected one still exists (just in case)
// 4) do nothing
// re-fetch compliance metrics
compliance_metric_service.get_compliance_metrics($scope.organization.id).then((data) => {
$scope.compliance_metrics = data;
const metric_ids = _.map($scope.compliance_metrics, 'id');
if ($scope.selected_metric === null && $scope.compliance_metrics.length > 0) {
// case 1
// change selection to last selected in modal and reload
if ($scope.compliance_metrics.length > 0) {
if (program != null) {
$scope.configs.compliance_metric = $scope.compliance_metrics.find((cm) => cm.id === program.id);
$scope.selected_metric = program.id;
} else {
// this should not happen, but just in case use the 1st one
$scope.configs.compliance_metric = $scope.compliance_metrics[0];
$scope.selected_metric = $scope.configs.compliance_metric.id;
}
} else if ($scope.selected_metric && metric_ids.indexOf($scope.selected_metric) === -1) {
// case 2
if (program != null) {
$scope.configs.compliance_metric = $scope.compliance_metrics.find((cm) => cm.id === program.id);
$scope.selected_metric = program.id;
} else if ($scope.compliance_metrics.length > 0) {
// this should not happen, but just in case use the 1st one
$scope.configs.compliance_metric = $scope.compliance_metrics[0];
$scope.selected_metric = $scope.configs.compliance_metric.id;
} else {
// load nothing
$scope.configs.compliance_metric = {};
$scope.selected_metric = null;
// attempt to keep the selected metric
$scope.configs.compliance_metric = $scope.compliance_metrics.find((cm) => cm.id === $scope.selected_metric);
if ($scope.configs.compliance_metric == null) {
// load first metric b/c selected metric no longer exists
$scope.configs.compliance_metric = $scope.compliance_metrics[0];
$scope.selected_metric = $scope.configs.compliance_metric.id;
}
}
} else if ($scope.selected_metric) {
// case 3
// otherwise reload the data for selected one in case it changed
$scope.configs.compliance_metric = $scope.compliance_metrics.find((cm) => cm.id === $scope.selected_metric);
} else {
// load nothing
$scope.configs.compliance_metric = {};
$scope.selected_metric = null;
$scope.data = null;
}

$scope.update_metric();
});
});
Expand Down

0 comments on commit 4d4f91a

Please sign in to comment.