mirror of https://github.com/hashicorp/consul
parent
ea3e4a720a
commit
ee3a32d771
@ -1,35 +1,43 @@
|
||||
import Service, { inject as service } from '@ember/service';
|
||||
import { get, set } from '@ember/object';
|
||||
import callableType from 'consul-ui/utils/callable-type';
|
||||
|
||||
export default Service.extend({
|
||||
notify: service('flashMessages'),
|
||||
logger: service('logger'),
|
||||
execute: function(handle, success, error, controller) {
|
||||
set(controller, 'isLoading', true);
|
||||
const displaySuccess = callableType(success);
|
||||
const displayError = callableType(error);
|
||||
const notify = get(this, 'notify');
|
||||
return handle()
|
||||
.then(() => {
|
||||
notify.add({
|
||||
type: 'success',
|
||||
message: success,
|
||||
});
|
||||
})
|
||||
.catch(e => {
|
||||
get(this, 'logger').execute(e);
|
||||
if (e.name === 'TransitionAborted') {
|
||||
return (
|
||||
handle()
|
||||
//TODO: pass this through to display success..
|
||||
.then(() => {
|
||||
notify.add({
|
||||
type: 'success',
|
||||
message: success,
|
||||
// here..
|
||||
message: displaySuccess(),
|
||||
});
|
||||
} else {
|
||||
notify.add({
|
||||
type: 'error',
|
||||
message: error,
|
||||
});
|
||||
}
|
||||
})
|
||||
.finally(function() {
|
||||
set(controller, 'isLoading', false);
|
||||
});
|
||||
})
|
||||
.catch(e => {
|
||||
get(this, 'logger').execute(e);
|
||||
if (e.name === 'TransitionAborted') {
|
||||
notify.add({
|
||||
type: 'success',
|
||||
// and here
|
||||
message: displaySuccess(),
|
||||
});
|
||||
} else {
|
||||
notify.add({
|
||||
type: 'error',
|
||||
message: displayError(e),
|
||||
});
|
||||
}
|
||||
})
|
||||
.finally(function() {
|
||||
set(controller, 'isLoading', false);
|
||||
})
|
||||
);
|
||||
},
|
||||
});
|
||||
|
@ -0,0 +1,9 @@
|
||||
export default function(obj) {
|
||||
if (typeof obj !== 'function') {
|
||||
return function() {
|
||||
return obj;
|
||||
};
|
||||
} else {
|
||||
return obj;
|
||||
}
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
export const OK = 200;
|
||||
export const UNAUTHORIZED = 401;
|
||||
export const INTERNAL_SERVER_ERROR = 500;
|
||||
|
@ -0,0 +1,10 @@
|
||||
import callableType from 'consul-ui/utils/callable-type';
|
||||
import { module, test } from 'qunit';
|
||||
// TODO: Sure there's a better name for this
|
||||
module('Unit | Utility | callable type');
|
||||
|
||||
test('returns a function returning the string', function(assert) {
|
||||
const expected = 'hi';
|
||||
const actual = callableType(expected)();
|
||||
assert.equal(actual, expected);
|
||||
});
|
Loading…
Reference in new issue