All files / src/store/modules templates.module.js

100% Statements 61/61
93.54% Branches 29/31
100% Functions 14/14
100% Lines 59/59

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 167 168 169 170 171 172 173 174                      1x               2x     2x     2x     4x         2x 2x     9x 9x 9x   9x 9x 3x     9x   9x   6x   6x 1x 5x 3x 2x 1x   1x     5x   4x 4x   4x                   9x             7x 7x 7x   7x 7x 6x     7x 7x 4x   4x                       4x   3x 3x   3x                   3x   7x           6x 6x 6x   6x 6x   2x 2x                   4x 4x 4x                   6x         3x 1x 2x 3x      
import { createErrorObject, getErrorMessage } from '../../utils/errorUtils';
import {
  NOTIF_FQ,
  TEMPLATES_ACT,
  TEMPLATES_GET,
  TEMPLATES_MUT,
} from '../types';
 
export default {
  namespaced: true,
  state() {
    return {
      list: [],
      loading: false,
      error: null,
    };
  },
  mutations: {
    [TEMPLATES_MUT.set](state, items) {
      state.list = items || [];
    },
    [TEMPLATES_MUT.setLoading](state, val) {
      state.loading = !!val;
    },
    [TEMPLATES_MUT.setError](state, errorObject) {
      state.error = errorObject || null;
    },
    [TEMPLATES_MUT.removeTemplate](state, templateId) {
      state.list = state.list.filter((t) => t.id !== templateId);
    },
  },
  actions: {
    [TEMPLATES_ACT.hydrate]({ commit }, initial = {}) {
      const ssr = initial.pageData?.templates;
      if (Array.isArray(ssr)) commit(TEMPLATES_MUT.set, ssr);
    },
    async [TEMPLATES_ACT.load]({ commit, dispatch }, { api, env }) {
      try {
        commit(TEMPLATES_MUT.setLoading, true);
        commit(TEMPLATES_MUT.setError, null);
 
        const params = new URLSearchParams();
        if (env) {
          params.append('env', env);
        }
 
        const url = `/api/templates?${params.toString()}`;
 
        const { data: response } = await api.get(url);
 
        let templates = [];
 
        if (Array.isArray(response)) {
          templates = response;
        } else if (response.success !== undefined && response.data) {
          templates = response.data.templates || [];
        } else if (response.templates) {
          templates = response.templates;
        } else {
          throw new Error('Invalid response format: templates missing');
        }
 
        commit(TEMPLATES_MUT.set, templates);
      } catch (e) {
        const errorObject = createErrorObject(e, 'Failed to load templates');
        commit(TEMPLATES_MUT.setError, errorObject);
 
        dispatch(
          NOTIF_FQ.actions.showError,
          {
            message: errorObject.message,
            type: 'api_error',
            closable: true,
          },
          { root: true },
        );
      } finally {
        commit(TEMPLATES_MUT.setLoading, false);
      }
    },
    async [TEMPLATES_ACT.create](
      { commit, dispatch },
      { api, env, templateData },
    ) {
      try {
        commit(TEMPLATES_MUT.setLoading, true);
        commit(TEMPLATES_MUT.setError, null);
 
        const params = new URLSearchParams();
        if (env) {
          params.append('env', env);
        }
 
        const url = `/api/templates?${params.toString()}`;
        const { data } = await api.post(url, templateData);
        const payload = data.data || data;
 
        dispatch(
          NOTIF_FQ.actions.showSuccess,
          {
            message:
              payload.message
              || `Template "${templateData.name}" created successfully`,
            type: 'success',
            closable: true,
          },
          { root: true },
        );
 
        return payload.template;
      } catch (e) {
        const errorObject = createErrorObject(e, 'Failed to create template');
        commit(TEMPLATES_MUT.setError, errorObject);
 
        dispatch(
          NOTIF_FQ.actions.showError,
          {
            message: errorObject.message,
            type: 'api_error',
            closable: true,
          },
          { root: true },
        );
 
        return null;
      } finally {
        commit(TEMPLATES_MUT.setLoading, false);
      }
    },
    async [TEMPLATES_ACT.delete]({ commit, dispatch }, {
      api, env, id, name,
    }) {
      try {
        commit(TEMPLATES_MUT.setLoading, true);
        commit(TEMPLATES_MUT.setError, null);
 
        const params = new URLSearchParams({ env });
        await api.delete(`/api/templates/${id}?${params.toString()}`);
 
        commit(TEMPLATES_MUT.removeTemplate, id);
        dispatch(
          NOTIF_FQ.actions.showSuccess,
          {
            message: `Template "${name}" deleted successfully`,
            type: 'success',
            closable: true,
          },
          { root: true },
        );
      } catch (e) {
        const errorObject = createErrorObject(e, 'Failed to delete template');
        commit(TEMPLATES_MUT.setError, errorObject);
        dispatch(
          NOTIF_FQ.actions.showError,
          {
            message: errorObject.message,
            type: 'api_error',
            closable: true,
          },
          { root: true },
        );
      } finally {
        commit(TEMPLATES_MUT.setLoading, false);
      }
    },
  },
  getters: {
    [TEMPLATES_GET.templates]: (s, _g, root) => (s.list?.length ? s.list : root.pageData?.templates || []),
    [TEMPLATES_GET.isLoading]: (s) => !!s.loading,
    [TEMPLATES_GET.error]: (s) => s.error || null,
    [TEMPLATES_GET.errorMessage]: (s) => getErrorMessage(s.error),
  },
};