summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/frequent_items/store/actions.js
blob: e5ef49ec40286e5e0b25c6389d5bbb909b652798 (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
107
108
109
110
111
112
import AccessorUtilities from '~/lib/utils/accessor';
import { isLoggedIn } from '~/lib/utils/common_utils';
import { getGroups, getProjects } from '~/rest_api';
import { getTopFrequentItems } from '../utils';
import * as types from './mutation_types';

export const setNamespace = ({ commit }, namespace) => {
  commit(types.SET_NAMESPACE, namespace);
};

export const setStorageKey = ({ commit }, key) => {
  commit(types.SET_STORAGE_KEY, key);
};

export const toggleItemsListEditablity = ({ commit }) => {
  commit(types.TOGGLE_ITEMS_LIST_EDITABILITY);
};

export const requestFrequentItems = ({ commit }) => {
  commit(types.REQUEST_FREQUENT_ITEMS);
};
export const receiveFrequentItemsSuccess = ({ commit }, data) => {
  commit(types.RECEIVE_FREQUENT_ITEMS_SUCCESS, data);
};
export const receiveFrequentItemsError = ({ commit }) => {
  commit(types.RECEIVE_FREQUENT_ITEMS_ERROR);
};

export const fetchFrequentItems = ({ state, dispatch }) => {
  dispatch('requestFrequentItems');

  if (AccessorUtilities.canUseLocalStorage()) {
    const storedFrequentItems = JSON.parse(localStorage.getItem(state.storageKey));

    dispatch(
      'receiveFrequentItemsSuccess',
      !storedFrequentItems ? [] : getTopFrequentItems(storedFrequentItems),
    );
  } else {
    dispatch('receiveFrequentItemsError');
  }
};

export const requestSearchedItems = ({ commit }) => {
  commit(types.REQUEST_SEARCHED_ITEMS);
};
export const receiveSearchedItemsSuccess = ({ commit }, data) => {
  commit(types.RECEIVE_SEARCHED_ITEMS_SUCCESS, data);
};
export const receiveSearchedItemsError = ({ commit }) => {
  commit(types.RECEIVE_SEARCHED_ITEMS_ERROR);
};
export const fetchSearchedItems = ({ state, dispatch }, searchQuery) => {
  dispatch('requestSearchedItems');

  const params = {
    simple: true,
    per_page: 20,
    membership: isLoggedIn(),
  };

  let searchFunction;
  if (state.namespace === 'projects') {
    searchFunction = getProjects;
    params.order_by = 'last_activity_at';
  } else {
    searchFunction = getGroups;
  }

  return searchFunction(searchQuery, params)
    .then((results) => {
      dispatch('receiveSearchedItemsSuccess', results);
    })
    .catch(() => {
      dispatch('receiveSearchedItemsError');
    });
};

export const setSearchQuery = ({ commit, dispatch }, query) => {
  commit(types.SET_SEARCH_QUERY, query);

  if (query) {
    dispatch('fetchSearchedItems', query);
  } else {
    dispatch('fetchFrequentItems');
  }
};

export const removeFrequentItemSuccess = ({ commit }, itemId) => {
  commit(types.RECEIVE_REMOVE_FREQUENT_ITEM_SUCCESS, itemId);
};

export const removeFrequentItemError = ({ commit }) => {
  commit(types.RECEIVE_REMOVE_FREQUENT_ITEM_ERROR);
};

export const removeFrequentItem = ({ state, dispatch }, itemId) => {
  if (AccessorUtilities.canUseLocalStorage()) {
    try {
      const storedRawItems = JSON.parse(localStorage.getItem(state.storageKey));
      localStorage.setItem(
        state.storageKey,
        JSON.stringify(storedRawItems.filter((item) => item.id !== itemId)),
      );
      dispatch('removeFrequentItemSuccess', itemId);
    } catch {
      dispatch('removeFrequentItemError');
    }
  } else {
    dispatch('removeFrequentItemError');
  }
};