|
| 1 | +<!-- |
| 2 | + - SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | + - SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | +--> |
| 5 | + |
| 6 | +<template> |
| 7 | + <div class="batch"> |
| 8 | + <div class="batch__title"> |
| 9 | + <h3 v-if="mode === 'grouping'"> |
| 10 | + {{ t('contacts', 'Add contacts to groups') }} |
| 11 | + </h3> |
| 12 | + <h3 v-if="mode === 'ab'"> |
| 13 | + {{ t('contacts', 'Move contacts to addressbook') }} |
| 14 | + </h3> |
| 15 | + </div> |
| 16 | + |
| 17 | + <NcSelect v-if="mode === 'grouping'" |
| 18 | + v-model="selectedGroups" |
| 19 | + :input-label="t('contacts', 'Select groups')" |
| 20 | + :multiple="true" |
| 21 | + :options="groupOptions" /> |
| 22 | + |
| 23 | + <!-- Addressbook selector for move mode --> |
| 24 | + <NcSelect v-if="mode === 'ab'" |
| 25 | + v-model="selectedAddressesBook" |
| 26 | + :input-label="t('contacts', 'Select addressbook')" |
| 27 | + :options="addressbookOptions" /> |
| 28 | + |
| 29 | + <h6>{{ t('contacts', 'Selected contacts') }}</h6> |
| 30 | + <NcNoteCard v-if="amountOfReadOnlyContacts > 0" type="info"> |
| 31 | + {{ t('contacts', 'Please note that {count} contact{p} readonly and will not be modified.', { count: amountOfReadOnlyContacts, p: amountOfReadOnlyContacts === 1 ? ' is' : 's are' }) }} |
| 32 | + </NcNoteCard> |
| 33 | + |
| 34 | + <div class="contacts-list"> |
| 35 | + <div v-for="(contact, index) in contactsLimited" :key="contact.key" class="contact-item"> |
| 36 | + <ContactsListItem :key="contact.key" |
| 37 | + :class="{ disabled: !contact.addressbook.canModifyCard }" |
| 38 | + :index="index" |
| 39 | + :source="contact" |
| 40 | + :reload-bus="reloadBus" |
| 41 | + :title="contact.addressbook.canModifyCard ? '' : t('contacts', 'This contact is read-only and cannot be modified.')" |
| 42 | + :is-static="true" /> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + |
| 46 | + <NcButton v-if="contacts.length > 9" |
| 47 | + variant="secondary" |
| 48 | + @click="showAllContacts = !showAllContacts"> |
| 49 | + <template #icon> |
| 50 | + <IconPlus :size="20" /> |
| 51 | + </template> |
| 52 | + {{ t('contacts', showAllContacts ? 'Show less' : 'Show all') }} |
| 53 | + </NcButton> |
| 54 | + |
| 55 | + <div class="batch__footer"> |
| 56 | + <NcButton v-if="mode === 'grouping'" |
| 57 | + variant="primary" |
| 58 | + :disabled="selectedGroups.length === 0" |
| 59 | + @click="submit"> |
| 60 | + <template #icon> |
| 61 | + <IconAccountPlus :size="20" /> |
| 62 | + </template> |
| 63 | + {{ t('contacts', 'Add') }} |
| 64 | + </NcButton> |
| 65 | + <NcButton v-if="mode === 'ab'" |
| 66 | + variant="primary" |
| 67 | + :disabled="!selectedAddressesBook" |
| 68 | + @click="submit"> |
| 69 | + <template #icon> |
| 70 | + <IconBookArrow :size="20" /> |
| 71 | + </template> |
| 72 | + {{ t('contacts', 'Move') }} |
| 73 | + </NcButton> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | +</template> |
| 77 | + |
| 78 | +<script> |
| 79 | +import ContactsListItem from './ContactsListItem.vue' |
| 80 | +import { NcButton, NcSelect, NcNoteCard } from '@nextcloud/vue' |
| 81 | +import IconPlus from 'vue-material-design-icons/Plus.vue' |
| 82 | +import IconAccountPlus from 'vue-material-design-icons/AccountMultiplePlusOutline.vue' |
| 83 | +import IconBookArrow from 'vue-material-design-icons/BookArrowRightOutline.vue' |
| 84 | +import appendContactToGroup from '../../services/appendContactToGroup.js' |
| 85 | +
|
| 86 | +export default { |
| 87 | + name: 'Batch', |
| 88 | +
|
| 89 | + components: { |
| 90 | + ContactsListItem, |
| 91 | + NcButton, |
| 92 | + NcSelect, |
| 93 | + IconPlus, |
| 94 | + IconAccountPlus, |
| 95 | + IconBookArrow, |
| 96 | + NcNoteCard, |
| 97 | + }, |
| 98 | +
|
| 99 | + props: { |
| 100 | + contacts: { |
| 101 | + type: Array, |
| 102 | + required: true, |
| 103 | + }, |
| 104 | + mode: { |
| 105 | + type: String, |
| 106 | + required: false, |
| 107 | + default: 'grouping', |
| 108 | + }, |
| 109 | + }, |
| 110 | +
|
| 111 | + emits: ['submit'], |
| 112 | +
|
| 113 | + data() { |
| 114 | + return { |
| 115 | + reloadBus: null, |
| 116 | + showAllContacts: false, |
| 117 | + selectedGroups: [], |
| 118 | + selectedAddressesBook: null, |
| 119 | + } |
| 120 | + }, |
| 121 | +
|
| 122 | + computed: { |
| 123 | + contactsLimited() { |
| 124 | + if (this.showAllContacts) { |
| 125 | + return this.contacts |
| 126 | + } |
| 127 | + return this.contacts.slice(0, 9) |
| 128 | + }, |
| 129 | + groupOptions() { |
| 130 | + return this.$store.getters.getGroups.map(group => ({ |
| 131 | + label: group.name, |
| 132 | + value: group.name, |
| 133 | + })) |
| 134 | + }, |
| 135 | + amountOfReadOnlyContacts() { |
| 136 | + return this.contacts.filter(contact => !contact.addressbook.canModifyCard).length |
| 137 | + }, |
| 138 | + addressbookOptions() { |
| 139 | + // Provide only enabled, writable addressbooks to move to |
| 140 | + return this.$store.getters.getAddressbooks |
| 141 | + .filter(ab => !ab.readOnly && ab.enabled) |
| 142 | + .map(ab => ({ label: ab.displayName || ab.label || ab.addressbook, value: ab.id || ab.addressbook })) |
| 143 | + }, |
| 144 | + }, |
| 145 | +
|
| 146 | + methods: { |
| 147 | + submit() { |
| 148 | + if (this.mode === 'grouping') { |
| 149 | + this.group() |
| 150 | + } |
| 151 | +
|
| 152 | + if (this.mode === 'ab') { |
| 153 | + this.moveToAddressbook() |
| 154 | + } |
| 155 | + }, |
| 156 | +
|
| 157 | + async group() { |
| 158 | + const allGroups = this.$store.getters.getGroups |
| 159 | +
|
| 160 | + // Add to groups |
| 161 | + this.selectedGroups.forEach(selectedGroup => { |
| 162 | + const group = allGroups.find(g => g.name === selectedGroup.value) |
| 163 | + if (!group) { |
| 164 | + console.error('Cannot add contact to an undefined group', selectedGroup) |
| 165 | + return |
| 166 | + } |
| 167 | + this.contacts.forEach(contact => { |
| 168 | + if (!contact.addressbook.canModifyCard) return // skip read-only for groups |
| 169 | + if (contact.groups && contact.groups.includes(group.name)) return |
| 170 | + appendContactToGroup(contact, group.name) |
| 171 | + .then(() => { |
| 172 | + this.$store.dispatch('addContactToGroup', { contact, groupName: group.name }) |
| 173 | + }) |
| 174 | + .catch((error) => { |
| 175 | + console.error(error) |
| 176 | + }) |
| 177 | + }) |
| 178 | + }) |
| 179 | +
|
| 180 | + this.$emit('submit') |
| 181 | + }, |
| 182 | +
|
| 183 | + async moveToAddressbook() { |
| 184 | + if (!this.selectedAddressesBook) return |
| 185 | + const addressbook = this.$store.getters.getAddressbooks.find(ab => ab.id === this.selectedAddressesBook.value) |
| 186 | + if (!addressbook) { |
| 187 | + console.error('Selected addressbook not found', this.selectedAddressesBook) |
| 188 | + return |
| 189 | + } |
| 190 | +
|
| 191 | + const movePromises = this.contacts.map(async (contact) => { |
| 192 | + if (!contact.addressbook.canModifyCard || contact.addressbook.id === addressbook.id) { |
| 193 | + return null |
| 194 | + } |
| 195 | + try { |
| 196 | + await this.$store.dispatch('moveContactToAddressbook', { contact, addressbook }) |
| 197 | + return contact |
| 198 | + } catch (error) { |
| 199 | + console.error('Failed to move contact', contact, error) |
| 200 | + return null |
| 201 | + } |
| 202 | + }) |
| 203 | +
|
| 204 | + await Promise.all(movePromises) |
| 205 | + this.$emit('submit') |
| 206 | + }, |
| 207 | + }, |
| 208 | +} |
| 209 | +</script> |
| 210 | + |
| 211 | +<style lang="scss" scoped> |
| 212 | +
|
| 213 | +.batch { |
| 214 | + margin: calc(var(--default-grid-baseline) * 8); |
| 215 | +
|
| 216 | + &__title { |
| 217 | + margin-bottom: var(--default-grid-baseline); |
| 218 | + } |
| 219 | +
|
| 220 | + &__footer { |
| 221 | + width: 100%; |
| 222 | + display: flex; |
| 223 | + justify-content: flex-end; |
| 224 | + } |
| 225 | +
|
| 226 | + .contacts-list { |
| 227 | + display: grid; |
| 228 | + grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); |
| 229 | + gap: var(--default-grid-baseline); |
| 230 | + margin: calc(var(--default-grid-baseline) * 2) 0; |
| 231 | +
|
| 232 | + .disabled { |
| 233 | + opacity: 0.5; |
| 234 | + } |
| 235 | + } |
| 236 | +} |
| 237 | +</style> |
0 commit comments