2020-04-03 07:34:58 +00:00
|
|
|
import ContactsAPI from '../../api/contacts';
|
|
|
|
import { refreshActionCableConnector } from '../../helpers/actionCable';
|
|
|
|
|
2021-03-20 12:14:20 +00:00
|
|
|
const state = {
|
|
|
|
currentUser: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
const SET_CURRENT_USER = 'SET_CURRENT_USER';
|
|
|
|
|
|
|
|
export const getters = {
|
|
|
|
getCurrentUser(_state) {
|
|
|
|
return _state.currentUser;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
export const actions = {
|
2021-03-20 12:14:20 +00:00
|
|
|
get: async ({ commit }) => {
|
|
|
|
try {
|
|
|
|
const { data } = await ContactsAPI.get();
|
|
|
|
commit(SET_CURRENT_USER, data);
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
}
|
|
|
|
},
|
2021-01-17 17:14:03 +00:00
|
|
|
update: async ({ dispatch }, { identifier, user: userObject }) => {
|
2020-04-03 07:34:58 +00:00
|
|
|
try {
|
|
|
|
const user = {
|
|
|
|
email: userObject.email,
|
|
|
|
name: userObject.name,
|
|
|
|
avatar_url: userObject.avatar_url,
|
2021-01-17 17:14:03 +00:00
|
|
|
identifier_hash: userObject.identifier_hash,
|
2020-04-03 07:34:58 +00:00
|
|
|
};
|
|
|
|
const {
|
|
|
|
data: { pubsub_token: pubsubToken },
|
|
|
|
} = await ContactsAPI.update(identifier, user);
|
2021-01-17 17:14:03 +00:00
|
|
|
|
2021-03-20 12:14:20 +00:00
|
|
|
dispatch('get');
|
2021-01-17 17:14:03 +00:00
|
|
|
if (userObject.identifier_hash) {
|
|
|
|
dispatch('conversation/clearConversations', {}, { root: true });
|
|
|
|
dispatch('conversation/fetchOldConversations', {}, { root: true });
|
|
|
|
}
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
refreshActionCableConnector(pubsubToken);
|
|
|
|
} catch (error) {
|
|
|
|
// Ingore error
|
|
|
|
}
|
|
|
|
},
|
2020-08-21 14:00:27 +00:00
|
|
|
setCustomAttributes: async (_, customAttributes = {}) => {
|
|
|
|
try {
|
|
|
|
await ContactsAPI.setCustomAttibutes(customAttributes);
|
|
|
|
} catch (error) {
|
|
|
|
// Ingore error
|
|
|
|
}
|
|
|
|
},
|
2020-04-03 07:34:58 +00:00
|
|
|
};
|
|
|
|
|
2021-03-20 12:14:20 +00:00
|
|
|
export const mutations = {
|
|
|
|
[SET_CURRENT_USER]($state, user) {
|
|
|
|
const { currentUser } = $state;
|
|
|
|
$state.currentUser = { ...currentUser, ...user };
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
export default {
|
|
|
|
namespaced: true,
|
2021-03-20 12:14:20 +00:00
|
|
|
state,
|
|
|
|
getters,
|
2020-04-03 07:34:58 +00:00
|
|
|
actions,
|
2021-03-20 12:14:20 +00:00
|
|
|
mutations,
|
2020-04-03 07:34:58 +00:00
|
|
|
};
|