github.com/akzi/consul@v1.4.5/ui-v2/app/services/feedback.js (about)

     1  import Service, { inject as service } from '@ember/service';
     2  import { get, set } from '@ember/object';
     3  import callableType from 'consul-ui/utils/callable-type';
     4  
     5  const TYPE_SUCCESS = 'success';
     6  const TYPE_ERROR = 'error';
     7  const defaultStatus = function(type, obj) {
     8    return type;
     9  };
    10  const notificationDefaults = function() {
    11    return {
    12      timeout: 6000,
    13      extendedTimeout: 300,
    14    };
    15  };
    16  export default Service.extend({
    17    notify: service('flashMessages'),
    18    logger: service('logger'),
    19    execute: function(handle, action, status = defaultStatus, controller) {
    20      set(controller, 'isLoading', true);
    21      const getAction = callableType(action);
    22      const getStatus = callableType(status);
    23      const notify = get(this, 'notify');
    24      return (
    25        handle()
    26          //TODO: pass this through to getAction..
    27          .then(item => {
    28            // returning exactly `false` for a feedback action means even though
    29            // its successful, please skip this notification and don't display it
    30            if (item !== false) {
    31              notify.clearMessages();
    32              // TODO right now the majority of `item` is a Transition
    33              // but you can resolve an object
    34              notify.add({
    35                ...notificationDefaults(),
    36                type: getStatus(TYPE_SUCCESS),
    37                // here..
    38                action: getAction(),
    39                item: item,
    40              });
    41            }
    42          })
    43          .catch(e => {
    44            notify.clearMessages();
    45            get(this, 'logger').execute(e);
    46            if (e.name === 'TransitionAborted') {
    47              notify.add({
    48                ...notificationDefaults(),
    49                type: getStatus(TYPE_SUCCESS),
    50                // and here
    51                action: getAction(),
    52              });
    53            } else {
    54              notify.add({
    55                ...notificationDefaults(),
    56                type: getStatus(TYPE_ERROR, e),
    57                action: getAction(),
    58              });
    59            }
    60          })
    61          .finally(function() {
    62            set(controller, 'isLoading', false);
    63          })
    64      );
    65    },
    66  });