Chatwoot/app/javascript/widget/store/modules/contactV2/index.js

110 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-09-23 06:50:53 +00:00
import ContactPublicAPI from 'widget/api/contacts';
import { refreshActionCableConnector } from '../../../helpers/actionCable';
const state = {
currentUser: {},
uiFlags: {
isFetching: false,
isCreating: false,
isUpdating: false,
},
};
export const getters = {
getCurrentUser(_state) {
return _state.currentUser;
},
};
export const actions = {
create: async ({ commit }, { inboxIdentifier, user: userObject }) => {
try {
commit('setUIFlag', { isCreating: true });
const user = {
email: userObject.email,
name: userObject.name,
avatar_url: userObject.avatar_url,
identifier_hash: userObject.identifier_hash,
phone_number: userObject.phone_number,
};
2021-09-29 08:58:32 +00:00
const { data } = await ContactPublicAPI.create(inboxIdentifier, user);
2021-09-23 06:50:53 +00:00
const { pubsub_token: pubsubToken } = data;
commit('setCurrentUser', data);
refreshActionCableConnector(pubsubToken);
2021-09-29 08:58:32 +00:00
return data;
2021-09-23 06:50:53 +00:00
} catch (error) {
throw new Error(error);
} finally {
commit('setUIFlag', { isCreating: false });
}
},
get: async ({ commit }, { inboxIdentifier, contactIdentifier }) => {
try {
commit('setUIFlag', { isFetching: true });
const { data } = await ContactPublicAPI.get(
inboxIdentifier,
contactIdentifier
);
2021-09-29 08:58:32 +00:00
const { pubsub_token: pubsubToken } = data;
2021-09-23 06:50:53 +00:00
commit('setCurrentUser', data);
2021-09-29 08:58:32 +00:00
refreshActionCableConnector(pubsubToken);
2021-09-23 06:50:53 +00:00
} catch (error) {
throw new Error(error);
} finally {
commit('setUIFlag', { isFetching: false });
}
},
update: async ({ commit }, { identifier, user: userObject }) => {
try {
commit('setUIFlag', { isUpdating: false });
const user = {
email: userObject.email,
name: userObject.name,
avatar_url: userObject.avatar_url,
identifier_hash: userObject.identifier_hash,
phone_number: userObject.phone_number,
};
const {
data: { pubsub_token: pubsubToken },
} = await ContactPublicAPI.update(identifier, user);
refreshActionCableConnector(pubsubToken);
} catch (error) {
throw new Error(error);
} finally {
commit('setUIFlag', { isUpdating: false });
}
},
setCustomAttributes: async (_, customAttributes = {}) => {
try {
await ContactPublicAPI.setCustomAttibutes(customAttributes);
} catch (error) {
// Ingore error
}
},
};
export const mutations = {
setCurrentUser($state, user) {
const { currentUser } = $state;
$state.currentUser = { ...currentUser, ...user };
},
setUIFlag($state, uiFlags) {
$state.uiFlags = {
...$state.uiFlags,
...uiFlags,
};
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations,
};