2019-08-14 09:48:44 +00:00
|
|
|
/* eslint no-param-reassign: 0 */
|
|
|
|
import axios from 'axios';
|
|
|
|
import Vue from 'vue';
|
|
|
|
import * as types from '../mutation-types';
|
|
|
|
import authAPI from '../../api/auth';
|
|
|
|
import createAxios from '../../helper/APIHelper';
|
2019-10-24 20:07:01 +00:00
|
|
|
import actionCable from '../../helper/actionCable';
|
2020-02-16 11:50:38 +00:00
|
|
|
import { setUser, getHeaderExpiry, clearCookiesOnLogout } from '../utils/api';
|
2021-05-28 13:51:16 +00:00
|
|
|
import { DEFAULT_REDIRECT_URL } from '../../constants';
|
2020-02-16 11:50:38 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
const state = {
|
|
|
|
currentUser: {
|
|
|
|
id: null,
|
|
|
|
account_id: null,
|
|
|
|
channel: null,
|
|
|
|
email: null,
|
|
|
|
name: null,
|
|
|
|
provider: null,
|
|
|
|
uid: null,
|
|
|
|
subscription: {
|
|
|
|
state: null,
|
|
|
|
expiry: null,
|
|
|
|
},
|
|
|
|
},
|
2020-05-26 17:08:48 +00:00
|
|
|
currentAccountId: null,
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// getters
|
2020-02-16 11:50:38 +00:00
|
|
|
export const getters = {
|
|
|
|
isLoggedIn($state) {
|
|
|
|
return !!$state.currentUser.id;
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getCurrentUserID(_state) {
|
|
|
|
return _state.currentUser.id;
|
|
|
|
},
|
|
|
|
|
2021-01-10 13:55:33 +00:00
|
|
|
getUISettings(_state) {
|
|
|
|
return _state.currentUser.ui_settings || {};
|
|
|
|
},
|
|
|
|
|
2020-07-04 14:33:16 +00:00
|
|
|
getCurrentUserAvailabilityStatus(_state) {
|
|
|
|
return _state.currentUser.availability_status;
|
|
|
|
},
|
|
|
|
|
2020-05-26 17:08:48 +00:00
|
|
|
getCurrentAccountId(_state) {
|
|
|
|
return _state.currentAccountId;
|
|
|
|
},
|
|
|
|
|
|
|
|
getCurrentRole(_state) {
|
|
|
|
const { accounts = [] } = _state.currentUser;
|
|
|
|
const [currentAccount = {}] = accounts.filter(
|
|
|
|
account => account.id === _state.currentAccountId
|
|
|
|
);
|
|
|
|
return currentAccount.role;
|
|
|
|
},
|
|
|
|
|
2020-02-16 11:50:38 +00:00
|
|
|
getCurrentUser(_state) {
|
|
|
|
return _state.currentUser;
|
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// actions
|
2020-02-16 11:50:38 +00:00
|
|
|
export const actions = {
|
2019-08-14 09:48:44 +00:00
|
|
|
login({ commit }, credentials) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-08-21 07:29:56 +00:00
|
|
|
authAPI
|
|
|
|
.login(credentials)
|
|
|
|
.then(() => {
|
|
|
|
commit(types.default.SET_CURRENT_USER);
|
|
|
|
window.axios = createAxios(axios);
|
2019-10-24 20:07:01 +00:00
|
|
|
actionCable.init(Vue);
|
2021-05-28 13:51:16 +00:00
|
|
|
window.location = DEFAULT_REDIRECT_URL;
|
2019-08-21 07:29:56 +00:00
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
},
|
2020-02-16 11:50:38 +00:00
|
|
|
async validityCheck(context) {
|
|
|
|
try {
|
|
|
|
const response = await authAPI.validityCheck();
|
|
|
|
setUser(response.data.payload.data, getHeaderExpiry(response));
|
|
|
|
context.commit(types.default.SET_CURRENT_USER);
|
|
|
|
} catch (error) {
|
2021-04-13 12:51:46 +00:00
|
|
|
if (error?.response?.status === 401) {
|
2020-02-16 11:50:38 +00:00
|
|
|
clearCookiesOnLogout();
|
|
|
|
}
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
},
|
2020-02-16 11:50:38 +00:00
|
|
|
setUser({ commit, dispatch }) {
|
2019-08-14 09:48:44 +00:00
|
|
|
if (authAPI.isLoggedIn()) {
|
|
|
|
commit(types.default.SET_CURRENT_USER);
|
2020-02-16 11:50:38 +00:00
|
|
|
dispatch('validityCheck');
|
2019-08-14 09:48:44 +00:00
|
|
|
} else {
|
|
|
|
commit(types.default.CLEAR_USER);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
logout({ commit }) {
|
|
|
|
commit(types.default.CLEAR_USER);
|
|
|
|
},
|
2021-01-10 13:55:33 +00:00
|
|
|
|
2020-02-16 11:50:38 +00:00
|
|
|
updateProfile: async ({ commit }, params) => {
|
2021-06-02 12:22:24 +00:00
|
|
|
// eslint-disable-next-line no-useless-catch
|
2020-02-16 11:50:38 +00:00
|
|
|
try {
|
|
|
|
const response = await authAPI.profileUpdate(params);
|
|
|
|
setUser(response.data, getHeaderExpiry(response));
|
|
|
|
commit(types.default.SET_CURRENT_USER);
|
|
|
|
} catch (error) {
|
2021-06-02 12:22:24 +00:00
|
|
|
throw error;
|
2020-02-16 11:50:38 +00:00
|
|
|
}
|
|
|
|
},
|
2020-05-26 17:08:48 +00:00
|
|
|
|
2021-01-10 13:55:33 +00:00
|
|
|
updateUISettings: async ({ commit }, params) => {
|
|
|
|
try {
|
|
|
|
commit(types.default.SET_CURRENT_USER_UI_SETTINGS, params);
|
|
|
|
const response = await authAPI.updateUISettings(params);
|
|
|
|
setUser(response.data, getHeaderExpiry(response));
|
|
|
|
commit(types.default.SET_CURRENT_USER);
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-21 17:13:27 +00:00
|
|
|
updateAvailability: ({ commit, dispatch }, { availability }) => {
|
2020-10-02 05:46:12 +00:00
|
|
|
authAPI.updateAvailability({ availability }).then(response => {
|
2020-11-21 17:13:27 +00:00
|
|
|
const userData = response.data;
|
|
|
|
const { id, availability_status: availabilityStatus } = userData;
|
|
|
|
setUser(userData, getHeaderExpiry(response));
|
2020-10-02 05:46:12 +00:00
|
|
|
commit(types.default.SET_CURRENT_USER);
|
2020-11-21 17:13:27 +00:00
|
|
|
dispatch('agents/updatePresence', { [id]: availabilityStatus });
|
2020-10-02 05:46:12 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-05-26 17:08:48 +00:00
|
|
|
setCurrentAccountId({ commit }, accountId) {
|
|
|
|
commit(types.default.SET_CURRENT_ACCOUNT_ID, accountId);
|
|
|
|
},
|
2020-07-04 14:33:16 +00:00
|
|
|
|
|
|
|
setCurrentUserAvailabilityStatus({ commit, state: $state }, data) {
|
|
|
|
if (data[$state.currentUser.id]) {
|
|
|
|
commit(
|
|
|
|
types.default.SET_CURRENT_USER_AVAILABILITY,
|
|
|
|
data[$state.currentUser.id]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// mutations
|
2021-01-10 13:55:33 +00:00
|
|
|
export const mutations = {
|
2020-07-04 14:33:16 +00:00
|
|
|
[types.default.SET_CURRENT_USER_AVAILABILITY](_state, status) {
|
|
|
|
Vue.set(_state.currentUser, 'availability_status', status);
|
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
[types.default.CLEAR_USER](_state) {
|
|
|
|
_state.currentUser.id = null;
|
|
|
|
},
|
|
|
|
[types.default.SET_CURRENT_USER](_state) {
|
2020-02-16 11:50:38 +00:00
|
|
|
const currentUser = {
|
|
|
|
...authAPI.getAuthData(),
|
|
|
|
...authAPI.getCurrentUser(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Vue.set(_state, 'currentUser', currentUser);
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
2021-01-10 13:55:33 +00:00
|
|
|
[types.default.SET_CURRENT_USER_UI_SETTINGS](_state, { uiSettings }) {
|
|
|
|
Vue.set(_state, 'currentUser', {
|
|
|
|
..._state.currentUser,
|
|
|
|
ui_settings: {
|
|
|
|
..._state.currentUser.ui_settings,
|
|
|
|
...uiSettings,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2020-05-26 17:08:48 +00:00
|
|
|
[types.default.SET_CURRENT_ACCOUNT_ID](_state, accountId) {
|
|
|
|
Vue.set(_state, 'currentAccountId', Number(accountId));
|
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|