All files / app/assets/javascripts/badges/store mutations.js

95% Statements 38/40
100% Branches 12/12
92% Functions 23/25
94.87% Lines 37/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159      4x 4x 19x 5x     14x         3x               2x         2x           2x 6x 2x   4x   2x                                       1x           1x         1x                 3x 1x         1x 3x 1x           2x   1x         1x 3x 1x           2x   1x           1x     1x     1x       1x             1x               2x 1x       1x             1x          
import { PROJECT_BADGE } from '../constants';
import types from './mutation_types';
 
const reorderBadges = (badges) =>
  badges.sort((a, b) => {
    if (a.kind !== b.kind) {
      return a.kind === PROJECT_BADGE ? 1 : -1;
    }
 
    return a.id - b.id;
  });
 
export default {
  [types.RECEIVE_NEW_BADGE](state, newBadge) {
    Object.assign(state, {
      badgeInAddForm: null,
      badges: reorderBadges(state.badges.concat(newBadge)),
      isSaving: false,
      renderedBadge: null,
    });
  },
  [types.RECEIVE_NEW_BADGE_ERROR](state) {
    Object.assign(state, {
      isSaving: false,
    });
  },
  [types.REQUEST_NEW_BADGE](state) {
    Object.assign(state, {
      isSaving: true,
    });
  },
 
  [types.RECEIVE_UPDATED_BADGE](state, updatedBadge) {
    const badges = state.badges.map((badge) => {
      if (badge.id === updatedBadge.id) {
        return updatedBadge;
      }
      return badge;
    });
    Object.assign(state, {
      badgeInEditForm: null,
      badges,
      isEditing: false,
      isSaving: false,
      renderedBadge: null,
    });
  },
  [types.RECEIVE_UPDATED_BADGE_ERROR](state) {
    Object.assign(state, {
      isSaving: false,
    });
  },
  [types.REQUEST_UPDATED_BADGE](state) {
    Object.assign(state, {
      isSaving: true,
    });
  },
 
  [types.RECEIVE_LOAD_BADGES](state, badges) {
    Object.assign(state, {
      badges: reorderBadges(badges),
      isLoading: false,
    });
  },
  [types.RECEIVE_LOAD_BADGES_ERROR](state) {
    Object.assign(state, {
      isLoading: false,
    });
  },
  [types.REQUEST_LOAD_BADGES](state, data) {
    Object.assign(state, {
      kind: data.kind, // project or group
      apiEndpointUrl: data.apiEndpointUrl,
      docsUrl: data.docsUrl,
      isLoading: true,
    });
  },
 
  [types.RECEIVE_DELETE_BADGE](state, badgeId) {
    const badges = state.badges.filter((badge) => badge.id !== badgeId);
    Object.assign(state, {
      badges,
    });
  },
  [types.RECEIVE_DELETE_BADGE_ERROR](state, badgeId) {
    const badges = state.badges.map((badge) => {
      if (badge.id === badgeId) {
        return {
          ...badge,
          isDeleting: false,
        };
      }
 
      return badge;
    });
    Object.assign(state, {
      badges,
    });
  },
  [types.REQUEST_DELETE_BADGE](state, badgeId) {
    const badges = state.badges.map((badge) => {
      if (badge.id === badgeId) {
        return {
          ...badge,
          isDeleting: true,
        };
      }
 
      return badge;
    });
    Object.assign(state, {
      badges,
    });
  },
 
  [types.RECEIVE_RENDERED_BADGE](state, renderedBadge) {
    Object.assign(state, { isRendering: false, renderedBadge });
  },
  [types.RECEIVE_RENDERED_BADGE_ERROR](state) {
    Object.assign(state, { isRendering: false });
  },
  [types.REQUEST_RENDERED_BADGE](state) {
    Object.assign(state, { isRendering: true });
  },
 
  [types.START_EDITING](state, badge) {
    Object.assign(state, {
      badgeInEditForm: { ...badge },
      isEditing: true,
      renderedBadge: { ...badge },
    });
  },
  [types.STOP_EDITING](state) {
    Object.assign(state, {
      badgeInEditForm: null,
      isEditing: false,
      renderedBadge: null,
    });
  },
 
  [types.UPDATE_BADGE_IN_FORM](state, badge) {
    if (state.isEditing) {
      Object.assign(state, {
        badgeInEditForm: badge,
      });
    } else {
      Object.assign(state, {
        badgeInAddForm: badge,
      });
    }
  },
 
  [types.UPDATE_BADGE_IN_MODAL](state, badge) {
    Object.assign(state, {
      badgeInModal: badge,
    });
  },
};