2019-08-14 09:48:44 +00:00
|
|
|
/* eslint no-param-reassign: 0 */
|
|
|
|
import axios from 'axios';
|
|
|
|
import moment from 'moment';
|
|
|
|
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-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
|
|
|
getSubscription(_state) {
|
2019-08-21 07:29:56 +00:00
|
|
|
return _state.currentUser.subscription === undefined
|
|
|
|
? null
|
|
|
|
: _state.currentUser.subscription;
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getTrialLeft(_state) {
|
2019-08-21 07:29:56 +00:00
|
|
|
const createdAt =
|
|
|
|
_state.currentUser.subscription === undefined
|
|
|
|
? moment()
|
|
|
|
: _state.currentUser.subscription.expiry * 1000;
|
2019-08-14 09:48:44 +00:00
|
|
|
const daysLeft = moment(createdAt).diff(moment(), 'days');
|
|
|
|
return daysLeft < 0 ? 0 : daysLeft;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
setCurrentAccountId({ commit }, accountId) {
|
|
|
|
commit(types.default.SET_CURRENT_ACCOUNT_ID, accountId);
|
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// mutations
|
|
|
|
const mutations = {
|
|
|
|
[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,
|
|
|
|
};
|