All files / alert index.ts

100% Statements 19/19
100% Branches 8/8
100% Functions 2/2
100% Lines 18/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59  1x   1x           1x             1x         1x   2x 2x 2x 2x 2x                 4x 4x   1x   1x   1x   1x     1x             1x      
import {ALERT_CLOSE,  ALERT_OPEN, ALERT_REPLAY} from './types';
 
// alert styles
const colors = {
  PASS: '#73C990', // green
  FAIL: '#FF4081', // red
  NOTE: '#9DA5B4', // blue
};
 
// default alert
export const _alert: CR.Alert = {
  message: '',
  open: false,
  action: 'NOTE',
  duration: 1500,
  color: colors.NOTE
};
 
const open = {
  open: true,
  action: 'NOTE',
  duration: 1500
};
 
let current: CR.Alert = _alert;
 
function setAlert(a: CR.Alert): CR.Alert {
  a.color = colors[a.action] || colors.NOTE;
  let statusBarAlert = <HTMLElement>document.getElementsByClassName('cr-alert-replay')[0];
  statusBarAlert.style.color = a.color;
  current = a;
  return Object.assign({}, open, a);
}
 
/**
 * snackbar Alert reducer
 * @param  {} alert=_alert
 * @param  {Action} action
 * @returns CR
 */
export default function alert(
  alert = _alert, action: Action
): CR.Alert {
  switch (action.type) {
 
    case ALERT_REPLAY:
      return setAlert(current);
 
    case ALERT_OPEN:
      return setAlert(action.payload.alert);
 
    case ALERT_CLOSE:
      return Object.assign({}, alert, { open: false });
 
    default:
      return alert;
  }
}