/
contacts.js
73 lines (66 loc) 路 1.66 KB
/
contacts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import ContactsAPI from '../../api/contacts';
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 user = {
email: userObject.email,
name: userObject.name,
avatar_url: userObject.avatar_url,
identifier_hash: userObject.identifier_hash,
phone_number: userObject.phone_number,
};
await ContactsAPI.update(identifier, user);
dispatch('get');
if (userObject.identifier_hash) {
dispatch('conversation/clearConversations', {}, { root: true });
dispatch('conversation/fetchOldConversations', {}, { root: true });
}
} catch (error) {
// Ignore error
}
},
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,
};