5ea0436051
* Add draft support * Fixes issue with draft loading * Adds draft for private notes * Use localstorage helper * .remove instead of .clear * Remove timestamp * clearLocalStorageOnLogout * Fix draft save on refresh * Remove usage of delete operator * Adds autosave for draft messages * Remove setinterval and add debounce * Removes draft redundancy check * Adds test cases for debouncer * Update app/javascript/shared/helpers/specs/TimeHelpers.spec.js Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com> * Update app/javascript/shared/helpers/specs/TimeHelpers.spec.js Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com> * Review fixes * Fixes issue with debouncer * FIxes debouncer issue * Fixes issue with draft empty message * Removes empty keys from local storage drafts * Fixes error with empty draft Co-authored-by: Pranav Raj S <pranav@chatwoot.com> Co-authored-by: Fayaz Ahmed <15716057+fayazara@users.noreply.github.com> Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Co-authored-by: iamsivin <iamsivin@gmail.com>
231 lines
5.8 KiB
JavaScript
231 lines
5.8 KiB
JavaScript
/* 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';
|
|
import actionCable from '../../helper/actionCable';
|
|
import {
|
|
setUser,
|
|
getHeaderExpiry,
|
|
clearCookiesOnLogout,
|
|
clearLocalStorageOnLogout,
|
|
} from '../utils/api';
|
|
import { getLoginRedirectURL } from '../../helper/URLHelper';
|
|
|
|
const state = {
|
|
currentUser: {
|
|
id: null,
|
|
account_id: null,
|
|
channel: null,
|
|
email: null,
|
|
name: null,
|
|
provider: null,
|
|
uid: null,
|
|
subscription: {
|
|
state: null,
|
|
expiry: null,
|
|
},
|
|
},
|
|
currentAccountId: null,
|
|
};
|
|
|
|
// getters
|
|
export const getters = {
|
|
isLoggedIn($state) {
|
|
return !!$state.currentUser.id;
|
|
},
|
|
|
|
getCurrentUserID(_state) {
|
|
return _state.currentUser.id;
|
|
},
|
|
|
|
getUISettings(_state) {
|
|
return _state.currentUser.ui_settings || {};
|
|
},
|
|
|
|
getCurrentUserAvailability(_state) {
|
|
const { accounts = [] } = _state.currentUser;
|
|
const [currentAccount = {}] = accounts.filter(
|
|
account => account.id === _state.currentAccountId
|
|
);
|
|
return currentAccount.availability;
|
|
},
|
|
|
|
getCurrentAccountId(_, __, rootState) {
|
|
if (rootState.route.params && rootState.route.params.accountId) {
|
|
return Number(rootState.route.params.accountId);
|
|
}
|
|
return null;
|
|
},
|
|
|
|
getCurrentRole(_state) {
|
|
const { accounts = [] } = _state.currentUser;
|
|
const [currentAccount = {}] = accounts.filter(
|
|
account => account.id === _state.currentAccountId
|
|
);
|
|
return currentAccount.role;
|
|
},
|
|
|
|
getCurrentUser(_state) {
|
|
return _state.currentUser;
|
|
},
|
|
|
|
getMessageSignature(_state) {
|
|
const { message_signature: messageSignature } = _state.currentUser;
|
|
|
|
return messageSignature || '';
|
|
},
|
|
|
|
getCurrentAccount(_state) {
|
|
const { accounts = [] } = _state.currentUser;
|
|
const [currentAccount = {}] = accounts.filter(
|
|
account => account.id === _state.currentAccountId
|
|
);
|
|
return currentAccount || {};
|
|
},
|
|
|
|
getUserAccounts(_state) {
|
|
const { accounts = [] } = _state.currentUser;
|
|
return accounts;
|
|
},
|
|
};
|
|
|
|
// actions
|
|
export const actions = {
|
|
login({ commit }, { ssoAccountId, ...credentials }) {
|
|
return new Promise((resolve, reject) => {
|
|
authAPI
|
|
.login(credentials)
|
|
.then(response => {
|
|
commit(types.default.SET_CURRENT_USER);
|
|
clearLocalStorageOnLogout();
|
|
window.axios = createAxios(axios);
|
|
actionCable.init(Vue);
|
|
window.location = getLoginRedirectURL(ssoAccountId, response.data);
|
|
resolve();
|
|
})
|
|
.catch(error => {
|
|
reject(error);
|
|
});
|
|
});
|
|
},
|
|
async validityCheck(context) {
|
|
try {
|
|
const response = await authAPI.validityCheck();
|
|
setUser(response.data.payload.data, getHeaderExpiry(response), {
|
|
setUserInSDK: true,
|
|
});
|
|
context.commit(types.default.SET_CURRENT_USER);
|
|
} catch (error) {
|
|
if (error?.response?.status === 401) {
|
|
clearCookiesOnLogout();
|
|
}
|
|
}
|
|
},
|
|
setUser({ commit, dispatch }) {
|
|
if (authAPI.isLoggedIn()) {
|
|
commit(types.default.SET_CURRENT_USER);
|
|
dispatch('validityCheck');
|
|
} else {
|
|
commit(types.default.CLEAR_USER);
|
|
}
|
|
},
|
|
logout({ commit }) {
|
|
commit(types.default.CLEAR_USER);
|
|
},
|
|
|
|
updateProfile: async ({ commit }, params) => {
|
|
// eslint-disable-next-line no-useless-catch
|
|
try {
|
|
const response = await authAPI.profileUpdate(params);
|
|
setUser(response.data, getHeaderExpiry(response));
|
|
commit(types.default.SET_CURRENT_USER);
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
deleteAvatar: async ({ commit }) => {
|
|
try {
|
|
await authAPI.deleteAvatar();
|
|
commit(types.default.SET_CURRENT_USER);
|
|
} catch (error) {
|
|
// Ignore error
|
|
}
|
|
},
|
|
|
|
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
|
|
}
|
|
},
|
|
|
|
updateAvailability: async ({ commit, dispatch }, params) => {
|
|
try {
|
|
const response = await authAPI.updateAvailability(params);
|
|
const userData = response.data;
|
|
const { id } = userData;
|
|
setUser(userData, getHeaderExpiry(response));
|
|
commit(types.default.SET_CURRENT_USER);
|
|
dispatch('agents/updatePresence', { [id]: params.availability });
|
|
} catch (error) {
|
|
// Ignore error
|
|
}
|
|
},
|
|
|
|
setCurrentAccountId({ commit }, accountId) {
|
|
commit(types.default.SET_CURRENT_ACCOUNT_ID, accountId);
|
|
},
|
|
|
|
setCurrentUserAvailability({ commit, state: $state }, data) {
|
|
if (data[$state.currentUser.id]) {
|
|
commit(
|
|
types.default.SET_CURRENT_USER_AVAILABILITY,
|
|
data[$state.currentUser.id]
|
|
);
|
|
}
|
|
},
|
|
};
|
|
|
|
// mutations
|
|
export const mutations = {
|
|
[types.default.SET_CURRENT_USER_AVAILABILITY](_state, availability) {
|
|
Vue.set(_state.currentUser, 'availability', availability);
|
|
},
|
|
[types.default.CLEAR_USER](_state) {
|
|
_state.currentUser.id = null;
|
|
},
|
|
[types.default.SET_CURRENT_USER](_state) {
|
|
const currentUser = {
|
|
...authAPI.getAuthData(),
|
|
...authAPI.getCurrentUser(),
|
|
};
|
|
|
|
Vue.set(_state, 'currentUser', currentUser);
|
|
},
|
|
[types.default.SET_CURRENT_USER_UI_SETTINGS](_state, { uiSettings }) {
|
|
Vue.set(_state, 'currentUser', {
|
|
..._state.currentUser,
|
|
ui_settings: {
|
|
..._state.currentUser.ui_settings,
|
|
...uiSettings,
|
|
},
|
|
});
|
|
},
|
|
[types.default.SET_CURRENT_ACCOUNT_ID](_state, accountId) {
|
|
Vue.set(_state, 'currentAccountId', Number(accountId));
|
|
},
|
|
};
|
|
|
|
export default {
|
|
state,
|
|
getters,
|
|
actions,
|
|
mutations,
|
|
};
|