Skip to content

Commit

Permalink
Ui/transform handle delete (#10035)
Browse files Browse the repository at this point in the history
* Add hasDataChanges hook on persist for delete and save

* Reverse order of disconnecting transformations then deleting role

* Update get functions to be in line with impending ember upgrade
  • Loading branch information
chelshaw authored Oct 1, 2020
1 parent 9978ba8 commit a473924
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
31 changes: 26 additions & 5 deletions ui/app/components/transform-edit-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default Component.extend(FocusOnInsertMixin, {
},

transitionToRoute() {
this.get('router').transitionTo(...arguments);
this.router.transitionTo(...arguments);
},

modelPrefixFromType(modelType) {
Expand All @@ -56,6 +56,15 @@ export default Component.extend(FocusOnInsertMixin, {
}
return modelPrefix;
},

listTabFromType(modelType) {
let tab;
if (modelType && modelType.startsWith('transform/')) {
tab = `${modelType.replace('transform/', '')}`;
}
return tab;
},

persist(method, successCallback) {
const model = get(this, 'model');
return model[method]()
Expand All @@ -68,6 +77,16 @@ export default Component.extend(FocusOnInsertMixin, {
});
},

applyDelete(callback = () => {}) {
const tab = this.listTabFromType(this.get('model.constructor.modelName'));
this.persist('destroyRecord', () => {
this.hasDataChanges();
callback();
// TODO: Investigate what is causing a console error after this point
this.transitionToRoute(LIST_ROOT_ROUTE, { queryParams: { tab } });
});
},

applyChanges(type, callback = () => {}) {
const modelId = this.get('model.id') || this.get('model.name'); // transform comes in as model.name
const modelPrefix = this.modelPrefixFromType(this.get('model.constructor.modelName'));
Expand All @@ -78,11 +97,16 @@ export default Component.extend(FocusOnInsertMixin, {
}

this.persist('save', () => {
this.hasDataChanges();
callback();
this.transitionToRoute(SHOW_ROUTE, `${modelPrefix}${modelId}`);
});
},

hasDataChanges() {
this.onDataChange(this.model?.hasDirtyAttributes);
},

actions: {
createOrUpdate(type, event) {
event.preventDefault();
Expand All @@ -99,10 +123,7 @@ export default Component.extend(FocusOnInsertMixin, {
},

delete() {
this.persist('destroyRecord', () => {
this.onDataChange();
this.transitionToRoute(LIST_ROOT_ROUTE);
});
this.applyDelete();
},
},
});
12 changes: 11 additions & 1 deletion ui/app/components/transform-role-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export default TransformBase.extend({

Promise.all(promises).then(res => {
let hasError = res.find(r => !!r.errorStatus);

if (hasError) {
let errorAdding = res.find(r => r.errorStatus === 403 && r.action === 'ADD');
let errorRemoving = res.find(r => r.errorStatus === 403 && r.action === 'REMOVE');
Expand Down Expand Up @@ -103,5 +102,16 @@ export default TransformBase.extend({
this.handleUpdateTransformations(updateTransformations, roleId);
});
},

delete() {
const roleId = this.model?.id;
const roleTransformations = this.model?.transformations || [];
const updateTransformations = roleTransformations.map(t => ({
id: t,
action: 'REMOVE',
}));
this.handleUpdateTransformations(updateTransformations, roleId);
this.applyDelete();
},
},
});
12 changes: 11 additions & 1 deletion ui/app/components/transformation-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ export default TransformBase.extend({
});
},

isWildcard(role) {
if (typeof role === 'string') {
return role.indexOf('*') >= 0;
}
if (role && role.id) {
return role.id.indexOf('*') >= 0;
}
return false;
},

actions: {
createOrUpdate(type, event) {
event.preventDefault();
Expand All @@ -88,7 +98,7 @@ export default TransformBase.extend({
const initialRoles = this.get('initialRoles') || [];

const updateRoles = [...newModelRoles, ...initialRoles]
.filter(r => r.indexOf('*') < 0) // TODO: expand wildcards into included roles instead
.filter(r => !this.isWildcard(r)) // TODO: expand wildcards into included roles instead
.map(role => {
if (initialRoles.indexOf(role) < 0) {
return {
Expand Down

0 comments on commit a473924

Please sign in to comment.