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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
<template>
<!-- Bulk contacts edit modal -->
<Modal v-if="isProcessing || isProcessDone"
:clear-view-delay="-1"
:can-close="isProcessDone"
@close="closeProcess">
<AddToGroupView v-bind="processStatus" @close="closeProcess" />
</Modal>
<!-- contacts picker -->
<EntityPicker v-else-if="showPicker"
:confirm-label="t('contacts', 'Add to {group}', { group: pickerforGroup.name })"
:data-types="pickerTypes"
:data-set="pickerData"
@close="onContactPickerClose"
@submit="onContactPickerPick" />
</template>
<script>
import { subscribe } from '@nextcloud/event-bus'
import pLimit from 'p-limit'
import { NcModal as Modal } from '@nextcloud/vue'
import AddToGroupView from '../../views/Processing/AddToGroupView.vue'
import appendContactToGroup from '../../services/appendContactToGroup.js'
import EntityPicker from './EntityPicker.vue'
export default {
name: 'ContactsPicker',
components: {
AddToGroupView,
EntityPicker,
Modal,
},
data() {
return {
// Entity picker
showPicker: false,
pickerforGroup: null,
pickerData: [],
pickerTypes: [{
id: 'contact',
label: t('contacts', 'Contacts'),
}],
// Bulk processing
isProcessing: false,
isProcessDone: false,
processStatus: {
failed: 0,
progress: 0,
success: 0,
total: 0,
name: '',
},
}
},
computed: {
contacts() {
return this.$store.getters.getContacts
},
groups() {
return this.$store.getters.getGroups
},
sortedContacts() {
return this.$store.getters.getSortedContacts
},
},
mounted() {
// Watch for a add-to-group event
subscribe('contacts:group:append', this.addContactsToGroup)
},
methods: {
// Bulk contacts group management handlers
addContactsToGroup(group) {
console.debug('Contacts picker opened for group', group)
// Get the full group if we provided the group name only
if (typeof group === 'string') {
group = this.groups.find(a => a.name === group)
if (!group) {
console.error('Cannot add contact to an undefined group', group)
return
}
}
// Init data set
this.pickerData = this.sortedContacts
.map(({ key }) => {
const contact = this.contacts[key]
return {
id: contact.key,
label: contact.displayName,
type: 'contact',
readOnly: contact.addressbook.readOnly,
groups: contact.groups,
}
})
// No read only contacts
.filter(contact => !contact.readOnly)
// No contacts already present in group
.filter(contact => contact.groups.indexOf(group.name) === -1)
this.showPicker = true
this.pickerforGroup = group
},
onContactPickerClose() {
this.pickerData = []
this.showPicker = false
},
onContactPickerPick(selection) {
console.debug('Adding', selection, 'to group', this.pickerforGroup)
const groupName = this.pickerforGroup.name
this.isProcessing = true
this.showPicker = false
this.processStatus.total = selection.length
this.processStatus.name = this.pickerforGroup.name
this.processStatus.progress = 0
this.processStatus.failed = 0
// max simultaneous requests
const limit = pLimit(3)
const requests = []
// create the array of requests to send
selection.map(async entity => {
try {
// Get contact
const contact = this.contacts[entity.id]
// push contact to server and use limit
requests.push(limit(() => appendContactToGroup(contact, groupName)
.then(() => {
this.$store.dispatch('addContactToGroup', { contact, groupName })
this.processStatus.progress++
this.processStatus.success++
})
.catch((error) => {
this.processStatus.progress++
this.processStatus.error++
console.error(error)
}),
))
} catch (e) {
console.error(e)
}
})
Promise.all(requests).then(() => {
this.isProcessDone = true
this.showPicker = false
// Auto close after 3 seconds if no errors
if (this.processStatus.failed === 0) {
setTimeout(this.closeProcess, 3000)
}
})
},
closeProcess() {
this.pickerforGroup = null
this.isProcessing = false
this.isProcessDone = false
// Reset
this.processStatus.failed = 0
this.processStatus.progress = 0
this.processStatus.success = 0
this.processStatus.total = 0
},
},
}
</script>
|