diff options
author | John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> | 2018-09-27 18:21:37 +0200 |
---|---|---|
committer | John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> | 2018-09-27 18:21:37 +0200 |
commit | dbe5ea7e99af190bd37c669edd8fd134e1fc08a8 (patch) | |
tree | 06e4697bf62becaec00ebe64b0fe3d6f21649a86 /src | |
parent | a9840fdd6415add9d9364a3f784b3d7b30cd7244 (diff) |
Properly update groups on import
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/store/addressbooks.js | 4 | ||||
-rw-r--r-- | src/store/groups.js | 73 |
2 files changed, 34 insertions, 43 deletions
diff --git a/src/store/addressbooks.js b/src/store/addressbooks.js index 8d93594b..58e3c212 100644 --- a/src/store/addressbooks.js +++ b/src/store/addressbooks.js @@ -311,7 +311,7 @@ const actions = { }) context.commit('appendContactsToAddressbook', { addressbook, contacts }) context.commit('appendContacts', contacts) - context.commit('appendGroupsFromContacts', contacts) + context.commit('extractGroupsFromContacts', contacts) context.commit('sortContacts') return contacts }) @@ -348,7 +348,7 @@ const actions = { // success, update store context.commit('addContact', contact) context.commit('addContactToAddressbook', contact) - context.commit('appendGroupsFromContacts', [contact]) + context.commit('extractGroupsFromContacts', [contact]) context.commit('incrementAccepted') }) .catch((error) => { diff --git a/src/store/groups.js b/src/store/groups.js index abd4599f..dd7a2393 100644 --- a/src/store/groups.js +++ b/src/store/groups.js @@ -27,40 +27,29 @@ const state = { const mutations = { /** * Extract all the groups from the provided contacts + * and add the contacts to their respective groups * * @param {Object} state the store data * @param {Contact[]} contacts the contacts to add - * TODO: create single contact mutation */ - appendGroupsFromContacts(state, contacts) { - // init groups list - let groups = Object.values(contacts) - // iterate on every contacts - .reduce((groups, contact) => { - // transform group names into Object - contact.groups.map(groupName => { - // overriding existing groups: remove duplicates - groups[groupName] = { - name: groupName, - contacts: [] + extractGroupsFromContacts(state, contacts) { + // iterate contacts + contacts.forEach(contact => { + if (contact.groups) { + contact.groups.forEach(groupName => { + let group = state.groups.find(search => search.name === groupName) + // nothing? create a new one + if (!group) { + state.groups.push({ + name: groupName, + contacts: [] + }) + group = state.groups.find(search => search.name === groupName) } + group.contacts.push(contact.key) }) - return groups - }, {}) - - // store in state - state.groups = Object.values(groups) - - // append keys to groups - Object.values(contacts) - .forEach(contact => { - if (contact.groups) { - contact.groups.forEach(groupName => { - let group = state.groups.find(search => search.name === groupName) - group.contacts.push(contact.key) - }) - } - }) + } + }) }, /** @@ -68,20 +57,22 @@ const mutations = { * * @param {Object} state the store data * @param {Object} data destructuring object - * @param {String} data.groupName the name of the group + * @param {Array<string>} data.groupNames the names of the group * @param {Contact} data.contact the contact */ - addContactToGroup(state, { groupName, contact }) { - let group = state.groups.find(search => search.name === groupName) - // nothing? create a new one - if (!group) { - state.groups.push({ - name: groupName, - contacts: [] - }) - group = state.groups.find(search => search.name === groupName) - } - group.contacts.push(contact.key) + addContactToGroups(state, { groupNames, contact }) { + groupNames.forEach(groupName => { + let group = state.groups.find(search => search.name === groupName) + // nothing? create a new one + if (!group) { + state.groups.push({ + name: groupName, + contacts: [] + }) + group = state.groups.find(search => search.name === groupName) + } + group.contacts.push(contact.key) + }) }, /** @@ -116,7 +107,7 @@ const actions = { * @param {Contact} data.contact the contact */ addContactToGroup(context, { groupName, contact }) { - context.commit('addContactToGroup', { groupName, contact }) + context.commit('addContactToGroups', { groupNames: [groupName], contact }) }, /** |