summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/registry/explorer/stores/actions.js
blob: 7f80bc21d6edba5b1734978fc45cef5ad61d46af (plain)
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
import axios from '~/lib/utils/axios_utils';
import createFlash from '~/flash';
import * as types from './mutation_types';
import {
  FETCH_IMAGES_LIST_ERROR_MESSAGE,
  DEFAULT_PAGE,
  DEFAULT_PAGE_SIZE,
  FETCH_TAGS_LIST_ERROR_MESSAGE,
} from '../constants';
import { decodeAndParse } from '../utils';

export const setInitialState = ({ commit }, data) => commit(types.SET_INITIAL_STATE, data);
export const setShowGarbageCollectionTip = ({ commit }, data) =>
  commit(types.SET_SHOW_GARBAGE_COLLECTION_TIP, data);

export const receiveImagesListSuccess = ({ commit }, { data, headers }) => {
  commit(types.SET_IMAGES_LIST_SUCCESS, data);
  commit(types.SET_PAGINATION, headers);
};

export const receiveTagsListSuccess = ({ commit }, { data, headers }) => {
  commit(types.SET_TAGS_LIST_SUCCESS, data);
  commit(types.SET_TAGS_PAGINATION, headers);
};

export const requestImagesList = (
  { commit, dispatch, state },
  { pagination = {}, name = null } = {},
) => {
  commit(types.SET_MAIN_LOADING, true);
  const { page = DEFAULT_PAGE, perPage = DEFAULT_PAGE_SIZE } = pagination;

  return axios
    .get(state.config.endpoint, { params: { page, per_page: perPage, name } })
    .then(({ data, headers }) => {
      dispatch('receiveImagesListSuccess', { data, headers });
    })
    .catch(() => {
      createFlash(FETCH_IMAGES_LIST_ERROR_MESSAGE);
    })
    .finally(() => {
      commit(types.SET_MAIN_LOADING, false);
    });
};

export const requestTagsList = ({ commit, dispatch }, { pagination = {}, params }) => {
  commit(types.SET_MAIN_LOADING, true);
  const { tags_path } = decodeAndParse(params);

  const { page = DEFAULT_PAGE, perPage = DEFAULT_PAGE_SIZE } = pagination;
  return axios
    .get(tags_path, { params: { page, per_page: perPage } })
    .then(({ data, headers }) => {
      dispatch('receiveTagsListSuccess', { data, headers });
    })
    .catch(() => {
      createFlash(FETCH_TAGS_LIST_ERROR_MESSAGE);
    })
    .finally(() => {
      commit(types.SET_MAIN_LOADING, false);
    });
};

export const requestDeleteTag = ({ commit, dispatch, state }, { tag, params }) => {
  commit(types.SET_MAIN_LOADING, true);
  return axios
    .delete(tag.destroy_path)
    .then(() => {
      dispatch('setShowGarbageCollectionTip', true);
      return dispatch('requestTagsList', { pagination: state.tagsPagination, params });
    })
    .finally(() => {
      commit(types.SET_MAIN_LOADING, false);
    });
};

export const requestDeleteTags = ({ commit, dispatch, state }, { ids, params }) => {
  commit(types.SET_MAIN_LOADING, true);
  const { tags_path } = decodeAndParse(params);

  const url = tags_path.replace('?format=json', '/bulk_destroy');

  return axios
    .delete(url, { params: { ids } })
    .then(() => {
      dispatch('setShowGarbageCollectionTip', true);
      return dispatch('requestTagsList', { pagination: state.tagsPagination, params });
    })
    .finally(() => {
      commit(types.SET_MAIN_LOADING, false);
    });
};

export const requestDeleteImage = ({ commit }, image) => {
  commit(types.SET_MAIN_LOADING, true);
  return axios
    .delete(image.destroy_path)
    .then(() => {
      commit(types.UPDATE_IMAGE, { ...image, deleting: true });
    })
    .finally(() => {
      commit(types.SET_MAIN_LOADING, false);
    });
};

export default () => {};