All files / src utils-undo-redo.ts

94.12% Statements 32/34
50% Branches 3/6
100% Functions 17/17
92.59% Lines 25/27
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 845x                     358x   358x                       44x 44x 73x     4x                         5x 52x 52x 52x       52x 52x 52x 52x         52x 52x 52x 52x         5x           52x 52x 52x   52x     52x        
import { UPDATE_CAN_UNDO_REDO } from "./constants";
 
/**
 * Piping async action calls secquentially using Array.prototype.reduce
 * to chain and initial, empty promise
 *
 * @module store/plugins/undoRedo:getConfig
 * @function
 * @param {String} namespace - The name of the store module
 * @returns {Object} config - The object containing the undo/redo stacks of the store module
 */
export const getConfig: UndoRedoOptions | any = (paths: UndoRedoOptions[]) => (
  namespace: string
): object => paths.find(path => path.namespace === namespace) || {};
 
// Based on https://gist.github.com/anvk/5602ec398e4fdc521e2bf9940fd90f84
/**
 * Piping async action calls secquentially using Array.prototype.reduce
 * to chain and initial, empty promise
 *
 * @module store/plugins/undoRedo:pipeActions
 * @function
 * @param {Array<Object>} actions - The array of objects containing the each
 * action's name and payload
 */
export const pipeActions = (store: any) => (actions: Array<any>) =>
  actions
    .filter(({ action }) => !!action)
    .reduce(
      (promise, { action, payload }) =>
        promise.then(() => store.dispatch(action, payload)),
      Promise.resolve()
    );
 
/**
 * Piping async action calls secquentially using Array.prototype.reduce
 * to chain and initial, empty promise
 *
 * @module store/plugins/undoRedo:setConfig
 * @function
 * @param {String} [namespace] - The name of the store module
 * @param {Object} config - The object containing the updated undo/redo stacks of the store module
 */
export const setConfig = (paths: UndoRedoOptions[]) => {
  return (namespace: string, config: any) => {
    const pathIndex = paths.findIndex(path => path.namespace === namespace);
    paths.splice(pathIndex, 1, config);
  };
};
 
const canRedo = (paths: UndoRedoOptions[]) => (namespace: string) => {
  const config = getConfig(paths)(namespace);
  Eif (Object.keys(config).length) {
    return config.undone.length > 0;
  }
  return false;
};
 
const canUndo = (paths: UndoRedoOptions[]) => (namespace: string) => {
  const config = getConfig(paths)(namespace);
  Eif (config) {
    return config.done.length > 0;
  }
  return false;
};
 
export const updateCanUndoRedo = ({
  paths,
  store
}: {
  paths: UndoRedoOptions[];
  store: any;
}) => (namespace: string) => {
  const undoEnabled = canUndo(paths)(namespace);
  const redoEnabled = canRedo(paths)(namespace);
 
  store.commit(`${namespace}${UPDATE_CAN_UNDO_REDO}`, {
    canUndo: undoEnabled
  });
  store.commit(`${namespace}${UPDATE_CAN_UNDO_REDO}`, {
    canRedo: redoEnabled
  });
};