|
93 | 93 | <script> |
94 | 94 | import { Multiselect, Avatar } from 'nextcloud-vue'; |
95 | 95 | import CollectionListItem from '../components/CollectionListItem'; |
96 | | - import axios from 'nextcloud-axios'; |
97 | 96 |
|
98 | 97 | const METHOD_CREATE_COLLECTION = 0; |
99 | 98 | const METHOD_ADD_TO_COLLECTION = 1; |
|
117 | 116 | codes: undefined, |
118 | 117 | value: null, |
119 | 118 | model: {}, |
120 | | - collections: null, |
121 | 119 | searchCollections: [] |
122 | 120 | }; |
123 | 121 | }, |
124 | 122 | mounted() { |
125 | | - let resourceId = this.$root.model.id |
126 | | - /** TODO move to service */ |
127 | | - const resourceBase = OC.linkToOCS(`collaboration/resources/${this.type}`); |
128 | | - axios.get(`${resourceBase}${resourceId}?format=json`, { |
129 | | - headers: { |
130 | | - 'OCS-APIRequest': true, |
131 | | - 'Content-Type': 'application/json; charset=UTF-8' |
132 | | - } |
133 | | - }).then((response) => { |
134 | | - this.collections = response.data.ocs.data |
135 | | - }); |
| 123 | + this.$store.dispatch('fetchCollectionsByResource', { |
| 124 | + resourceType: this.type, |
| 125 | + resourceId: this.$root.model.id |
| 126 | + }) |
136 | 127 | }, |
137 | 128 | computed: { |
| 129 | + collections() { |
| 130 | + return this.$store.getters.collectionsByResource(this.type, this.$root.model.id) |
| 131 | + }, |
138 | 132 | placeholder() { |
139 | 133 | return t('files_sharing', 'Add to a collection'); |
140 | 134 | }, |
|
151 | 145 | }) |
152 | 146 | } |
153 | 147 | for(let index in this.searchCollections) { |
154 | | - options.push({ |
155 | | - method: METHOD_ADD_TO_COLLECTION, |
156 | | - title: this.searchCollections[index].name, |
157 | | - collectionId: this.searchCollections[index].id |
158 | | - }) |
| 148 | + if (this.collections.findIndex((collection) => collection.id === this.searchCollections[index].id) === -1) { |
| 149 | + options.push({ |
| 150 | + method: METHOD_ADD_TO_COLLECTION, |
| 151 | + title: this.searchCollections[index].name, |
| 152 | + collectionId: this.searchCollections[index].id |
| 153 | + }) |
| 154 | + } |
159 | 155 | } |
160 | 156 | return options; |
161 | 157 | } |
162 | 158 | }, |
163 | | - created: function() { |
164 | | - }, |
165 | 159 | methods: { |
166 | 160 | select(selectedOption, id) { |
167 | 161 | if (selectedOption.method === METHOD_CREATE_COLLECTION) { |
168 | 162 | selectedOption.action().then((id) => { |
169 | | - console.log('Create a new collection with') |
170 | | - console.log('This file ', this.$root.model.id) |
171 | | - console.log('Selected resource ', selectedOption.type, id) |
172 | | - this.createCollection(this.$root.model.id, selectedOption.type, id) |
| 163 | + this.$store.dispatch('createCollection', { |
| 164 | + baseResourceType: this.type, |
| 165 | + baseResourceId: this.$root.model.id, |
| 166 | + resourceType: selectedOption.type, |
| 167 | + resourceId: id, |
| 168 | + name: this.$root.model.name, |
| 169 | + }) |
173 | 170 | }).catch((e) => { |
174 | | - console.error('No resource selected'); |
| 171 | + console.error('No resource selected', e); |
175 | 172 | }); |
176 | 173 | } |
177 | 174 |
|
178 | 175 | if (selectedOption.method === METHOD_ADD_TO_COLLECTION) { |
179 | | - this.addResourceToCollection(selectedOption.collectionId, this.type, this.$root.model.id) |
| 176 | + this.$store.dispatch('addResourceToCollection', { |
| 177 | + collectionId: selectedOption.collectionId, resourceType: this.type, resourceId: this.$root.model.id |
| 178 | + }) |
180 | 179 | } |
181 | 180 | }, |
182 | 181 | search(query) { |
183 | | - const searchBase = OC.linkToOCS(`collaboration/resources/collections/search`); |
184 | | - axios.get(`${searchBase}%25${query}%25?format=json`, { |
185 | | - headers: { |
186 | | - 'OCS-APIRequest': true, |
187 | | - 'Content-Type': 'application/json; charset=UTF-8' |
188 | | - } |
189 | | - }).then((response) => { |
190 | | - this.searchCollections = response.data.ocs.data |
191 | | - }); |
| 182 | + this.$store.dispatch('search', query).then((collections) => { |
| 183 | + this.searchCollections = collections |
| 184 | + }) |
192 | 185 | }, |
193 | 186 | showSelect() { |
194 | 187 | this.selectIsOpen = true |
|
199 | 192 | }, |
200 | 193 | isVueComponent(object) { |
201 | 194 | return object._isVue |
202 | | - }, |
203 | | - createCollection(resourceIdBase, resourceType, resourceId) { |
204 | | - /** TODO move to service */ |
205 | | - const resourceBase = OC.linkToOCS(`collaboration/resources/files`, 2); |
206 | | - axios.post(`${resourceBase}${resourceIdBase}?format=json`, { |
207 | | - name: this.$root.model.name |
208 | | - }, { |
209 | | - headers: { |
210 | | - 'OCS-APIRequest': true, |
211 | | - 'Content-Type': 'application/json; charset=UTF-8' |
212 | | - } |
213 | | - }).then((response) => { |
214 | | - let newCollection = response.data.ocs.data |
215 | | - console.log('Add new collection', newCollection) |
216 | | - this.collections.push(newCollection) |
217 | | - this.addResourceToCollection(newCollection.id, resourceType.toString(), resourceId.toString()) |
218 | | - }); |
219 | | - }, |
220 | | - addResourceToCollection(collectionId, resourceType, resourceId) { |
221 | | - resourceId = '' + resourceId; |
222 | | - /** TODO move to service */ |
223 | | - const resourceBase = OC.linkToOCS(`collaboration/resources/collections`, 2); |
224 | | - return axios.post(`${resourceBase}${collectionId}?format=json`, { |
225 | | - resourceType, |
226 | | - resourceId |
227 | | - }, { |
228 | | - headers: { |
229 | | - 'OCS-APIRequest': true, |
230 | | - 'Content-Type': 'application/json; charset=UTF-8' |
231 | | - } |
232 | | - }).then((response) => { |
233 | | - console.log('Add new collection', response.data.ocs.data) |
234 | | - let collection = this.collections.find((_item) => _item.id === collectionId) |
235 | | - if (collection) { |
236 | | - this.collections.find((_item) => _item.id === collectionId).resources = response.data.ocs.data.resources |
237 | | - } else { |
238 | | - this.collections.push(response.data.ocs.data) |
239 | | - } |
240 | | - }); |
241 | 195 | } |
242 | 196 | } |
243 | 197 | } |
|
0 commit comments