import { IFrameHelper } from 'widget/helpers/utils'; import ContactsAPI from '../../api/contacts'; import { SET_USER_ERROR } from '../../constants/errorTypes'; const state = { currentUser: {}, }; const SET_CURRENT_USER = 'SET_CURRENT_USER'; export const getters = { getCurrentUser(_state) { return _state.currentUser; }, }; export const actions = { get: async ({ commit }) => { try { const { data } = await ContactsAPI.get(); commit(SET_CURRENT_USER, data); } catch (error) { // Ignore error } }, update: async ({ dispatch }, { identifier, user: userObject }) => { try { const { email, name, avatar_url, identifier_hash, phone_number, company_name, city, country_code, description, custom_attributes, social_profiles, } = userObject; const user = { email, name, avatar_url, identifier_hash, phone_number, additional_attributes: { company_name, city, description, country_code, social_profiles, }, custom_attributes, }; await ContactsAPI.update(identifier, user); dispatch('get'); if (identifier_hash) { dispatch('conversation/clearConversations', {}, { root: true }); dispatch('conversation/fetchOldConversations', {}, { root: true }); dispatch('conversationAttributes/getAttributes', {}, { root: true }); } } catch (error) { const { response: { data }, } = error; IFrameHelper.sendMessage({ event: 'error', errorType: SET_USER_ERROR, data, }); } }, setCustomAttributes: async (_, customAttributes = {}) => { try { await ContactsAPI.setCustomAttributes(customAttributes); } catch (error) { // Ignore error } }, deleteCustomAttribute: async (_, customAttribute) => { try { await ContactsAPI.deleteCustomAttribute(customAttribute); } catch (error) { // Ignore error } }, }; export const mutations = { [SET_CURRENT_USER]($state, user) { const { currentUser } = $state; $state.currentUser = { ...currentUser, ...user }; }, }; export default { namespaced: true, state, getters, actions, mutations, };