reduce delete prompting cruft for app ui

This commit is contained in:
John Mitchell
2018-03-12 14:35:03 -04:00
parent 3cf447c49b
commit 80db90b34c
2 changed files with 42 additions and 65 deletions

View File

@@ -7,7 +7,6 @@ function ListApplicationsController (
$filter, $filter,
$scope, $scope,
$state, $state,
Alert,
Dataset, Dataset,
ProcessErrors, ProcessErrors,
Prompt, Prompt,
@@ -57,82 +56,56 @@ function ListApplicationsController (
return html; return html;
}; };
vm.deleteApplication = app => { vm.deleteApplication = (app) => {
if (!app) { const action = () => {
Alert(strings.get('error.DELETE'), strings.get('alert.MISSING_PARAMETER')); $('#prompt-modal').modal('hide');
return; Wait('start');
} application.request('delete', app.id)
.then(() => {
application.getDependentResourceCounts(application.id)
.then(counts => displayApplicationDeletePrompt(app, counts));
};
function createErrorHandler (path, action) {
return ({ data, status }) => {
const hdr = strings.get('error.HEADER');
const msg = strings.get('error.CALL', { path, action, status });
ProcessErrors($scope, data, status, null, { hdr, msg });
};
}
function handleSuccessfulDelete (app) {
const { page } = _.get($state.params, 'application_search');
let reloadListStateParams = null; let reloadListStateParams = null;
if ($scope.applications.length === 1 && !_.isEmpty(page) && page !== '1') { if ($scope.applications.length === 1 && $state.params.application_search &&
!_.isEmpty($state.params.application_search.page) &&
$state.params.application_search.page !== '1') {
const page = `${(parseInt(reloadListStateParams
.application_search.page, 10) - 1)}`;
reloadListStateParams = _.cloneDeep($state.params); reloadListStateParams = _.cloneDeep($state.params);
const pageNumber = (parseInt(reloadListStateParams.application_search.page, 0) - 1); reloadListStateParams.application_search.page = page;
reloadListStateParams.application_search.page = pageNumber.toString();
} }
if (parseInt($state.params.application_id, 0) === app.id) { if (parseInt($state.params.application_id, 10) === app.id) {
$state.go('applications', reloadListStateParams, { reload: true }); $state.go('^', reloadListStateParams, { reload: true });
} else { } else {
$state.go('.', reloadListStateParams, { reload: true }); $state.go('.', reloadListStateParams, { reload: true });
} }
} })
.catch(({ data, status }) => {
function displayApplicationDeletePrompt (app, counts) { ProcessErrors($scope, data, status, null, {
Prompt({ hdr: strings.get('error.HEADER'),
action () { msg: strings.get('error.CALL', { path: `${application.path}${app.id}`, status })
$('#prompt-modal').modal('hide');
Wait('start');
application
.request('delete', app.id)
.then(() => handleSuccessfulDelete(app))
.catch(createErrorHandler('delete application', 'DELETE'))
.finally(() => Wait('stop'));
},
hdr: strings.get('DELETE'),
resourceName: $filter('sanitize')(app.name),
body: buildApplicationDeletePromptHTML(counts),
}); });
} })
.finally(() => {
Wait('stop');
});
};
function buildApplicationDeletePromptHTML (counts) { const deleteModalBody = `<div class="Prompt-bodyQuery">${strings.get('deleteResource.CONFIRM', 'application')}</div>`;
const buildCount = count => `<span class="badge List-titleBadge">${count}</span>`;
const buildLabel = label => `<span class="Prompt-warningResourceTitle">
${$filter('sanitize')(label)}</span>`;
const buildCountLabel = ({ count, label }) => `<div>
${buildLabel(label)}${buildCount(count)}</div>`;
const displayedCounts = counts.filter(({ count }) => count > 0); Prompt({
hdr: strings.get('deleteResource.HEADER'),
const html = ` resourceName: $filter('sanitize')(app.name),
${displayedCounts.length ? strings.get('deleteResource.USED_BY', 'application') : ''} body: deleteModalBody,
${strings.get('deleteResource.CONFIRM', 'application')} action,
${displayedCounts.map(buildCountLabel).join('')} actionText: 'DELETE'
`; });
};
return html;
}
} }
ListApplicationsController.$inject = [ ListApplicationsController.$inject = [
'$filter', '$filter',
'$scope', '$scope',
'$state', '$state',
'Alert',
'Dataset', 'Dataset',
'ProcessErrors', 'ProcessErrors',
'Prompt', 'Prompt',

View File

@@ -48,9 +48,13 @@
</at-row-item> </at-row-item>
</div> </div>
<div class="at-Row-actions"> <div class="at-Row-actions">
<at-row-action icon="fa-trash" ng-click="vm.deleteApplication(application)" <!-- TODO: delete application should be based on user capabilities but that's not
ng-show="application.summary_fields.user_capabilities.delete"> implemented in the api yet -->
<at-row-action icon="fa-trash" ng-click="vm.deleteApplication(application)">
</at-row-action> </at-row-action>
<!-- <at-row-action icon="fa-trash" ng-click="vm.deleteApplication(application)"
ng-show="application.summary_fields.user_capabilities.delete">
</at-row-action> -->
</div> </div>
</at-row> </at-row>
</at-list> </at-list>