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';
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
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);
|
2020-03-09 17:57:10 +00:00
|
|
|
window.location = '/';
|
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) {
|
|
|
|
if (error.response.status === 401) {
|
|
|
|
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);
|
|
|
|
},
|
2020-02-16 11:50:38 +00:00
|
|
|
updateProfile: async ({ commit }, params) => {
|
|
|
|
try {
|
|
|
|
const response = await authAPI.profileUpdate(params);
|
|
|
|
setUser(response.data, getHeaderExpiry(response));
|
|
|
|
commit(types.default.SET_CURRENT_USER);
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
}
|
|
|
|
},
|
2020-05-26 17:08:48 +00:00
|
|
|
|
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
|
|
|
|
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
|
|
|
},
|
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,
|
|
|
|
};
|