Newer
Older
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
import { ErrorService } from '@/services/error-service.js';
import usergroupsAPI from '@/api/usergroupsAPI';
import axios from 'axios';
const DEV_AUTH = process.env.NODE_ENV === 'development' ? true : false;
const AUTH = {
username: process.env.VUE_APP_LOGIN_API_USERNAME,
password: process.env.VUE_APP_LOGIN_API_PASSWORD
};
const path = require('path');
const DOMAIN = process.env.VUE_APP_DOMAIN;
const USERGROUP_API_PATH = process.env.VUE_APP_USERGROUP_API_PATH;
// MUTATIONS
export const SET_USERGROUPS_SPHERES_LIST = 'SET_USERGROUPS_SPHERES_LIST';
export const SET_ERROR = 'SET_ERROR';
export const SET_IS_SPHERES_SEARCHED = 'SET_IS_SPHERES_SEARCHED';
// ACTIONS
export const GET_SPHERES_LIST = 'GET_SPHERES_LIST';
export const SEARCH_SPHERES = 'SEARCH_SPHERES';
/**************** STATE *******************/
const state = {
spheresCount: 0,
spheres: [],
usergroupsError: null,
isSpheresSearched: false
};
/**************** GETTERS *****************/
const getters = {
};
/*************** MUTATIONS ****************/
const mutations = {
[SET_USERGROUPS_SPHERES_LIST]: (state, payload) => {
if (payload) {
state.spheresCount = payload.count;
state.spheres = payload.results;
}
},
[SET_ERROR]: (state, error) => {
if (error) {
ErrorService.onError(error);
state.usergroupsError = error;
} else {
state.usergroupsError = error;
}
},
[SET_IS_SPHERES_SEARCHED]: (state, payload) => {
state.isSpheresSearched = payload;
}
};
/**************** ACTIONS *****************/
const actions = {
[GET_SPHERES_LIST]: async ({ commit }) => {
try {
const spheres = await usergroupsAPI.getFilteredUsergroupsList()
if (spheres) {
commit('SET_ERROR', null);
commit('SET_USERGROUPS_SPHERES_LIST', spheres);
}
} catch (error) {
commit('SET_ERROR', error);
}
},
[SEARCH_SPHERES]: async ({ rootState, commit }, text) => {

Florent Lavelle
committed
if (rootState.abortControllers.length > 0) {
commit('USE_ABORT_CONTROLLER', 'search_spheres', { root: true });

Florent Lavelle
committed
const controller = new AbortController();
commit('SET_ABORT_CONTROLLER', {
id: 'search_spheres',
controller: controller
}, { root: true });
const url = new URL(path.join(USERGROUP_API_PATH, `user-groups/?page=1&search=${text}&usergroup_types=group-of-organisation`), DOMAIN);
try {
const response = await axios.get(
url,
{

Florent Lavelle
committed
...controller && { signal: controller.signal },
...DEV_AUTH && { auth: AUTH }
}
);
if (response.status === 200) {

Florent Lavelle
committed
commit('REMOVE_ABORT_CONTROLLER', 'search_spheres', { root: true });
const usergroups = response.data;
if (usergroups) {
commit('SET_ERROR', null);
commit('SET_USERGROUPS_SPHERES_LIST', usergroups);
commit('SET_IS_SPHERES_SEARCHED', true);
}
}
} catch(err) {

Florent Lavelle
committed
commit('REMOVE_ABORT_CONTROLLER', 'search_spheres', { root: true });