open-consul/ui/packages/consul-ui/app/services/feedback.js
John Cowen 3372e6d14c
ui: Topology intention saving improvements (#9513)
* ui: Keep track of existing intentions and use those to save changes

Previously we risked overwriting existing data in an intention if we
tried to save an intention without having loaded it first, for example
Description and Metadata would have been overwritten.

This change loads in all the intentions for an origin service so we can
pick off the one we need to save and change to ensure that we don't
overwrite any existing data.
2021-01-19 15:40:39 +00:00

77 lines
2 KiB
JavaScript

import Service, { inject as service } from '@ember/service';
import callableType from 'consul-ui/utils/callable-type';
const TYPE_SUCCESS = 'success';
const TYPE_ERROR = 'error';
const defaultStatus = function(type, obj) {
return type;
};
const notificationDefaults = function() {
return {
timeout: 6000,
extendedTimeout: 300,
};
};
export default class FeedbackService extends Service {
@service('flashMessages') notify;
@service('logger') logger;
notification(action) {
return {
success: item => this.success(item, action),
error: e => this.error(e, action),
};
}
success(item, action, status = defaultStatus) {
const getAction = callableType(action);
const getStatus = callableType(status);
// returning exactly `false` for a feedback action means even though
// its successful, please skip this notification and don't display it
if (item !== false) {
this.notify.clearMessages();
// TODO right now the majority of `item` is a Transition
// but you can resolve an object
this.notify.add({
...notificationDefaults(),
type: getStatus(TYPE_SUCCESS),
// here..
action: getAction(),
item: item,
});
}
}
error(e, action, status = defaultStatus) {
const getAction = callableType(action);
const getStatus = callableType(status);
this.notify.clearMessages();
this.logger.execute(e);
if (e.name === 'TransitionAborted') {
this.notify.add({
...notificationDefaults(),
type: getStatus(TYPE_SUCCESS),
// and here
action: getAction(),
});
} else {
this.notify.add({
...notificationDefaults(),
type: getStatus(TYPE_ERROR, e),
action: getAction(),
error: e,
});
}
}
async execute(handle, action, status) {
let result;
try {
result = await handle();
this.success(result, action, status);
} catch (e) {
this.error(e, action, status);
}
}
}