2022-06-23 10:18:56 +00:00
|
|
|
import { sendMessage } 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';
|
2022-06-23 10:18:56 +00:00
|
|
|
import { setHeader } from '../../helpers/axios';
|
2021-03-20 12:14:20 +00:00
|
|
|
const state = {
|
|
|
|
currentUser: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
const SET_CURRENT_USER = 'SET_CURRENT_USER';
|
2022-06-23 10:18:56 +00:00
|
|
|
const parseErrorData = error =>
|
|
|
|
error && error.response && error.response.data ? error.response.data : error;
|
|
|
|
export const updateWidgetAuthToken = widgetAuthToken => {
|
|
|
|
if (widgetAuthToken) {
|
|
|
|
setHeader(widgetAuthToken);
|
|
|
|
sendMessage({
|
|
|
|
event: 'setAuthCookie',
|
|
|
|
data: { widgetAuthToken },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2021-03-20 12:14:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
},
|
2022-06-23 10:18:56 +00:00
|
|
|
update: async ({ dispatch }, { user }) => {
|
|
|
|
try {
|
|
|
|
await ContactsAPI.update(user);
|
|
|
|
dispatch('get');
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setUser: 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,
|
2022-06-23 10:18:56 +00:00
|
|
|
identifier_hash: identifierHash,
|
2022-02-28 06:40:55 +00:00
|
|
|
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,
|
2022-06-23 10:18:56 +00:00
|
|
|
identifier_hash: identifierHash,
|
2022-02-28 06:40:55 +00:00
|
|
|
phone_number,
|
|
|
|
additional_attributes: {
|
|
|
|
company_name,
|
|
|
|
city,
|
|
|
|
description,
|
|
|
|
country_code,
|
|
|
|
social_profiles,
|
|
|
|
},
|
|
|
|
custom_attributes,
|
2020-04-03 07:34:58 +00:00
|
|
|
};
|
2022-06-23 10:18:56 +00:00
|
|
|
const {
|
|
|
|
data: { widget_auth_token: widgetAuthToken },
|
|
|
|
} = await ContactsAPI.setUser(identifier, user);
|
|
|
|
updateWidgetAuthToken(widgetAuthToken);
|
2021-03-20 12:14:20 +00:00
|
|
|
dispatch('get');
|
2022-06-23 10:18:56 +00:00
|
|
|
if (identifierHash || widgetAuthToken) {
|
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-23 10:18:56 +00:00
|
|
|
const data = parseErrorData(error);
|
|
|
|
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
|
|
|
};
|