Skip to content

Commit 6b47198

Browse files
committed
Move to vuex
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 4fe844c commit 6b47198

File tree

7 files changed

+178
-121
lines changed

7 files changed

+178
-121
lines changed

apps/files_sharing/js/sharetabview.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
var vm = new Resources.Vue({
8383
el: '#collaborationResources',
8484
render: h => h(Resources.View),
85+
store: Resources.Store(),
8586
data: {
8687
model: this.model.toJSON()
8788
},

apps/files_sharing/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"dependencies": {
1818
"nextcloud-axios": "^0.1.3",
1919
"nextcloud-vue": "^0.5.0",
20-
"vue": "^2.5.17"
20+
"vue": "^2.5.17",
21+
"vuex": "^3.1.0"
2122
},
2223
"devDependencies": {
2324
"@babel/core": "^7.1.0",

apps/files_sharing/src/collaborationresources.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@
2121
*/
2222

2323
import Vue from 'vue'
24+
import Vuex from 'vuex'
2425
import { PopoverMenu } from 'nextcloud-vue'
2526
import ClickOutside from 'vue-click-outside'
2627
import { VTooltip } from 'v-tooltip'
28+
import { Store } from './services/collections';
2729

2830
Vue.prototype.t = t;
2931

3032
Vue.component('PopoverMenu', PopoverMenu)
3133
Vue.directive('ClickOutside', ClickOutside)
3234
Vue.directive('Tooltip', VTooltip)
35+
Vue.use(Vuex);
3336

3437
import View from './CollaborationView'
3538

36-
export { Vue, View }
39+
export { Vue, View, Store }

apps/files_sharing/src/components/CollectionList.vue

Lines changed: 28 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
<script>
9494
import { Multiselect, Avatar } from 'nextcloud-vue';
9595
import CollectionListItem from '../components/CollectionListItem';
96-
import axios from 'nextcloud-axios';
9796
9897
const METHOD_CREATE_COLLECTION = 0;
9998
const METHOD_ADD_TO_COLLECTION = 1;
@@ -117,24 +116,19 @@
117116
codes: undefined,
118117
value: null,
119118
model: {},
120-
collections: null,
121119
searchCollections: []
122120
};
123121
},
124122
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+
})
136127
},
137128
computed: {
129+
collections() {
130+
return this.$store.getters.collectionsByResource(this.type, this.$root.model.id)
131+
},
138132
placeholder() {
139133
return t('files_sharing', 'Add to a collection');
140134
},
@@ -151,44 +145,43 @@
151145
})
152146
}
153147
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+
}
159155
}
160156
return options;
161157
}
162158
},
163-
created: function() {
164-
},
165159
methods: {
166160
select(selectedOption, id) {
167161
if (selectedOption.method === METHOD_CREATE_COLLECTION) {
168162
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+
})
173170
}).catch((e) => {
174-
console.error('No resource selected');
171+
console.error('No resource selected', e);
175172
});
176173
}
177174
178175
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+
})
180179
}
181180
},
182181
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+
})
192185
},
193186
showSelect() {
194187
this.selectIsOpen = true
@@ -199,45 +192,6 @@
199192
},
200193
isVueComponent(object) {
201194
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-
});
241195
}
242196
}
243197
}

apps/files_sharing/src/components/CollectionListItem.vue

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
-->
2222

2323
<template>
24-
<li class="collection-list">
24+
<li class="collection-list" v-click-outside="hideDetails">
2525
<avatar :displayName="collection.name" :allowPlaceholder="true"></avatar>
2626
<span class="username" title="" @click="showDetails" v-if="this.newName === ''">{{ collection.name }}</span>
2727
<form v-else @submit.prevent="renameCollection">
28-
<input type="text"v-model="newName" autocomplete="off" autocapitalize="off">
28+
<input type="text" v-model="newName" autocomplete="off" autocapitalize="off">
2929
<input type="submit" value="" class="icon-confirm">
3030
</form>
3131
<transition name="fade">
@@ -44,7 +44,7 @@
4444
</div>
4545
</span>
4646
<transition name="fade">
47-
<ul class="resource-list-details" v-if="detailsOpen" v-click-outside="hideDetails">
47+
<ul class="resource-list-details" v-if="detailsOpen">
4848
<li v-for="resource in collection.resources">
4949
<a :href="resource.link"><span :class="getIcon(resource)"></span><span class="resource-name">{{ resource.name || '' }}</span></a>
5050
<span class="icon-delete" @click="removeResource(collection, resource)"></span>
@@ -88,13 +88,9 @@
8888
text: t('files_sharing', 'Details'),
8989
},
9090
{
91-
action: () => { this.openRename() },
91+
action: () => this.openRename(),
9292
icon: 'icon-rename',
9393
text: t('files_sharing', 'Rename collection'),
94-
},{
95-
action: () => { },
96-
icon: 'icon-delete',
97-
text: t('files_sharing', 'Remove collection'),
9894
}
9995
]
10096
},
@@ -119,17 +115,18 @@
119115
this.detailsOpen = false
120116
},
121117
removeResource(collection, resource) {
122-
service.removeResource(collection.id, resource.type, resource.id).then((response) => {
123-
this.collection.resources = this.collection.resources.filter(item => !(item.id === resource.id && item.type === resource.type))
118+
this.$store.dispatch('removeResource', {
119+
collectionId: collection.id, resourceType: resource.type, resourceId: resource.id
124120
})
125121
},
126122
openRename() {
127123
this.newName = this.collection.name;
128124
},
129125
renameCollection() {
130-
service.renameCollection(this.collection.id, this.newName).then((response) => {
131-
console.log('Renamed collection', response.data.ocs.data)
132-
this.collection = response.data.ocs.data
126+
this.$store.dispatch('renameCollection', {
127+
collectionId: this.collection.id,
128+
name: this.newName
129+
}).then((collection) => {
133130
this.newName = '';
134131
});
135132
}
@@ -160,13 +157,12 @@
160157
}
161158
.collection-list {
162159
flex-wrap: wrap;
160+
height: auto;
163161
164-
form {
165-
display: flex;
166-
}
167-
.username {
162+
form, .username {
168163
flex-basis: 10%;
169164
flex-grow: 1;
165+
display: flex;
170166
}
171167
.resource-list-details {
172168
flex-basis: 100%;

0 commit comments

Comments
 (0)