From 3a02101e668d6bc1a05d6c6b35708e97d22a104f Mon Sep 17 00:00:00 2001 From: Marco Nassabain Date: Sun, 14 Mar 2021 21:30:19 +0100 Subject: =?UTF-8?q?=E2=99=BB=EF=B8=8F=20ShareController:=20refactor=20dict?= =?UTF-8?q?ionary=20+=20fncts=20-=20updated=20dictionary=20structure=20to?= =?UTF-8?q?=20contain=20share=20status=20-=20divided=20code=20into=20funct?= =?UTF-8?q?ions=20-=20adapted=20code=20to=20match=20new=20implementation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marco Nassabain --- js/controller/ShareController.js | 63 ++++++++++++++++++++++++++++++++++------ templates/part.content.php | 2 +- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/js/controller/ShareController.js b/js/controller/ShareController.js index 694843d79..673da7319 100644 --- a/js/controller/ShareController.js +++ b/js/controller/ShareController.js @@ -23,7 +23,7 @@ app.controller('ShareController', function (ShareResource, Loading) { /** * @param search Username search query - * + * * Retrieve users matching search query using OC */ this.searchUsers = function(search) { @@ -42,31 +42,76 @@ app.controller('ShareController', function (ShareResource, Loading) { }; /** Dictionary mapping articles to users they're shared with */ - this.usersSharedArticles = {}; + this.usersSharedArticles = []; + + this.itemIsSharedWithUser = function(itemId, userId) { + let item = this.usersSharedArticles.find(i => i.id === itemId); + if (!item) { + return false; + } + let user = item.users.find(u => u.id === userId); + if (!user || !user.status) { + return false; + } + return true; + }; + + this.addItemShareWithUser = function(itemId, userId, status) { + let item = this.usersSharedArticles.find(i => i.id === itemId); + if (!item) { + item = { + id: itemId, + users: [] + }; + this.usersSharedArticles.push(item); + } + let user = item.users.find(u => u.id === userId); + if (!user) { + user = { + id: userId, + status: status + }; + item.users.push(user); + } + user.status = status; + }; /** * @param itemId ID of the item to be shared * @param userId ID of the recipient - * + * * Call the /share route with the appropriate params to share an item. * Fills this.usersSharedArticles to avoid re-sharing the same article * with the same user multiple times. */ this.shareItem = function(itemId, userId) { - Loading.setLoading(userId, true); - if (this.usersSharedArticles[itemId] && this.usersSharedArticles[itemId].includes(userId)) { - Loading.setLoading(userId, false); + if (this.itemIsSharedWithUser(itemId, userId)) { return; } + Loading.setLoading(userId, true); - this.usersSharedArticles[itemId] = this.usersSharedArticles[itemId] ? this.usersSharedArticles[itemId] : []; - this.usersSharedArticles[itemId].push(userId); - ShareResource.shareItem(itemId, userId) .then((result) => { + this.addItemShareWithUser(itemId, userId, true); Loading.setLoading(userId, false); return result; }); }; + this.isLoading = function(userId) { + return Loading.isLoading(userId); + }; + + this.isSuccessful = function(itemId, userId) { + let item = this.usersSharedArticles.find(i => i.id === itemId); + if (!item) { + return false; + } + let user = item.users.find(u => u.id === userId); + if (!user) { + return false; + } + return user.status; + }; + }); diff --git a/templates/part.content.php b/templates/part.content.php index a239cf5de..bde264f2e 100644 --- a/templates/part.content.php +++ b/templates/part.content.php @@ -133,7 +133,7 @@ ng-click="Share.shareItem(item.id, user.value.shareWith)"> {{ user.label }} + ng-class="{'icon-loading-small': Share.isLoading(user.value.shareWith), 'icon-checkmark': Share.isSuccessful(item.id, user.value.shareWith)}"> -- cgit v1.2.3