2022-02-21 04:10:11 +00:00
|
|
|
import { IFrameHelper } from 'widget/helpers/utils';
|
2020-04-03 07:34:58 +00:00
|
|
|
import ContactsAPI from '../../api/contacts';
|
2022-02-21 04:10:11 +00:00
|
|
|
import { SET_USER_ERROR } from '../../constants/errorTypes';
|
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 {
|
2022-02-28 06:40:55 +00:00
|
|
|
const {
|
|
|
|
email,
|
|
|
|
name,
|
|
|
|
avatar_url,
|
|
|
|
identifier_hash,
|
|
|
|
phone_number,
|
|
|
|
company_name,
|
|
|
|
city,
|
|
|
|
country_code,
|
|
|
|
description,
|
|
|
|
custom_attributes,
|
|
|
|
social_profiles,
|
|
|
|
} = userObject;
|
2020-04-03 07:34:58 +00:00
|
|
|
const user = {
|
2022-02-28 06:40:55 +00:00
|
|
|
email,
|
|
|
|
name,
|
|
|
|
avatar_url,
|
|
|
|
identifier_hash,
|
|
|
|
phone_number,
|
|
|
|
additional_attributes: {
|
|
|
|
company_name,
|
|
|
|
city,
|
|
|
|
description,
|
|
|
|
country_code,
|
|
|
|
social_profiles,
|
|
|
|
},
|
|
|
|
custom_attributes,
|
2020-04-03 07:34:58 +00:00
|
|
|
};
|
2021-11-22 18:02:17 +00:00
|
|
|
await ContactsAPI.update(identifier, user);
|
2021-01-17 17:14:03 +00:00
|
|
|
|
2021-03-20 12:14:20 +00:00
|
|
|
dispatch('get');
|
2022-02-28 06:40:55 +00:00
|
|
|
if (identifier_hash) {
|
2021-01-17 17:14:03 +00:00
|
|
|
dispatch('conversation/clearConversations', {}, { root: true });
|
|
|
|
dispatch('conversation/fetchOldConversations', {}, { root: true });
|
2022-03-29 14:21:51 +00:00
|
|
|
dispatch('conversationAttributes/getAttributes', {}, { root: true });
|
2021-01-17 17:14:03 +00:00
|
|
|
}
|
2020-04-03 07:34:58 +00:00
|
|
|
} catch (error) {
|
2022-06-01 05:43:10 +00:00
|
|
|
const data =
|
|
|
|
error && error.response && error.response.data
|
|
|
|
? error.response.data
|
|
|
|
: error;
|
2022-02-21 04:10:11 +00:00
|
|
|
IFrameHelper.sendMessage({
|
|
|
|
event: 'error',
|
|
|
|
errorType: SET_USER_ERROR,
|
|
|
|
data,
|
|
|
|
});
|
2020-04-03 07:34:58 +00:00
|
|
|
}
|
|
|
|
},
|
2020-08-21 14:00:27 +00:00
|
|
|
setCustomAttributes: async (_, customAttributes = {}) => {
|
|
|
|
try {
|
2021-11-15 09:26:35 +00:00
|
|
|
await ContactsAPI.setCustomAttributes(customAttributes);
|
2020-08-21 14:00:27 +00:00
|
|
|
} catch (error) {
|
2021-11-15 09:26:35 +00:00
|
|
|
// Ignore error
|
|
|
|
}
|
|
|
|
},
|
|
|
|
deleteCustomAttribute: async (_, customAttribute) => {
|
|
|
|
try {
|
|
|
|
await ContactsAPI.deleteCustomAttribute(customAttribute);
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
2020-08-21 14:00:27 +00:00
|
|
|
}
|
|
|
|
},
|
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
|
|
|
};
|