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

100% Statements 59/59
87.5% Branches 7/8
100% Functions 35/35
100% Lines 58/58

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 160 161 162 163 164 165 166        13x             1x     1x     1x     2x 2x 2x 2x             1x 1x     1x       1x     1x     1x     2x 2x 2x 2x     1x 1x     1x         1x       1x     1x     1x       2x 2x 2x     1x 1x     1x         1x     1x     1x       5x 5x 5x 2x     3x   3x       3x 3x     1x 1x     2x         1x     1x     1x       2x 2x 2x 2x             1x 1x     1x         1x       45x       2x      
import axios from '~/lib/utils/axios_utils';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import types from './mutation_types';
 
export const transformBackendBadge = (badge) => ({
  ...convertObjectPropsToCamelCase(badge, true),
  isDeleting: false,
});
 
export default {
  requestNewBadge({ commit }) {
    commit(types.REQUEST_NEW_BADGE);
  },
  receiveNewBadge({ commit }, newBadge) {
    commit(types.RECEIVE_NEW_BADGE, newBadge);
  },
  receiveNewBadgeError({ commit }) {
    commit(types.RECEIVE_NEW_BADGE_ERROR);
  },
  addBadge({ dispatch, state }) {
    const newBadge = state.badgeInAddForm;
    const endpoint = state.apiEndpointUrl;
    dispatch('requestNewBadge');
    return axios
      .post(endpoint, {
        name: newBadge.name,
        image_url: newBadge.imageUrl,
        link_url: newBadge.linkUrl,
      })
      .catch((error) => {
        dispatch('receiveNewBadgeError');
        throw error;
      })
      .then((res) => {
        dispatch('receiveNewBadge', transformBackendBadge(res.data));
      });
  },
  requestDeleteBadge({ commit }, badgeId) {
    commit(types.REQUEST_DELETE_BADGE, badgeId);
  },
  receiveDeleteBadge({ commit }, badgeId) {
    commit(types.RECEIVE_DELETE_BADGE, badgeId);
  },
  receiveDeleteBadgeError({ commit }, badgeId) {
    commit(types.RECEIVE_DELETE_BADGE_ERROR, badgeId);
  },
  deleteBadge({ dispatch, state }, badge) {
    const badgeId = badge.id;
    dispatch('requestDeleteBadge', badgeId);
    const endpoint = `${state.apiEndpointUrl}/${badgeId}`;
    return axios
      .delete(endpoint)
      .catch((error) => {
        dispatch('receiveDeleteBadgeError', badgeId);
        throw error;
      })
      .then(() => {
        dispatch('receiveDeleteBadge', badgeId);
      });
  },
 
  editBadge({ commit }, badge) {
    commit(types.START_EDITING, badge);
  },
 
  requestLoadBadges({ commit }, data) {
    commit(types.REQUEST_LOAD_BADGES, data);
  },
  receiveLoadBadges({ commit }, badges) {
    commit(types.RECEIVE_LOAD_BADGES, badges);
  },
  receiveLoadBadgesError({ commit }) {
    commit(types.RECEIVE_LOAD_BADGES_ERROR);
  },
 
  loadBadges({ dispatch, state }, data) {
    dispatch('requestLoadBadges', data);
    const endpoint = state.apiEndpointUrl;
    return axios
      .get(endpoint)
      .catch((error) => {
        dispatch('receiveLoadBadgesError');
        throw error;
      })
      .then((res) => {
        dispatch('receiveLoadBadges', res.data.map(transformBackendBadge));
      });
  },
 
  requestRenderedBadge({ commit }) {
    commit(types.REQUEST_RENDERED_BADGE);
  },
  receiveRenderedBadge({ commit }, renderedBadge) {
    commit(types.RECEIVE_RENDERED_BADGE, renderedBadge);
  },
  receiveRenderedBadgeError({ commit }) {
    commit(types.RECEIVE_RENDERED_BADGE_ERROR);
  },
 
  renderBadge({ dispatch, state }) {
    const badge = state.isEditing ? state.badgeInEditForm : state.badgeInAddForm;
    const { linkUrl, imageUrl } = badge;
    if (!linkUrl || linkUrl.trim() === '' || !imageUrl || imageUrl.trim() === '') {
      return Promise.resolve(badge);
    }
 
    dispatch('requestRenderedBadge');
 
    const parameters = [
      `link_url=${encodeURIComponent(linkUrl)}`,
      `image_url=${encodeURIComponent(imageUrl)}`,
    ].join('&');
    const renderEndpoint = `${state.apiEndpointUrl}/render?${parameters}`;
    return axios
      .get(renderEndpoint)
      .catch((error) => {
        dispatch('receiveRenderedBadgeError');
        throw error;
      })
      .then((res) => {
        dispatch('receiveRenderedBadge', transformBackendBadge(res.data));
      });
  },
 
  requestUpdatedBadge({ commit }) {
    commit(types.REQUEST_UPDATED_BADGE);
  },
  receiveUpdatedBadge({ commit }, updatedBadge) {
    commit(types.RECEIVE_UPDATED_BADGE, updatedBadge);
  },
  receiveUpdatedBadgeError({ commit }) {
    commit(types.RECEIVE_UPDATED_BADGE_ERROR);
  },
 
  saveBadge({ dispatch, state }) {
    const badge = state.badgeInEditForm;
    const endpoint = `${state.apiEndpointUrl}/${badge.id}`;
    dispatch('requestUpdatedBadge');
    return axios
      .put(endpoint, {
        name: badge.name,
        image_url: badge.imageUrl,
        link_url: badge.linkUrl,
      })
      .catch((error) => {
        dispatch('receiveUpdatedBadgeError');
        throw error;
      })
      .then((res) => {
        dispatch('receiveUpdatedBadge', transformBackendBadge(res.data));
      });
  },
 
  stopEditing({ commit }) {
    commit(types.STOP_EDITING);
  },
 
  updateBadgeInForm({ commit }, badge) {
    commit(types.UPDATE_BADGE_IN_FORM, badge);
  },
 
  updateBadgeInModal({ commit }, badge) {
    commit(types.UPDATE_BADGE_IN_MODAL, badge);
  },
};