diff options
author | Marco Nassabain <marco.nassabain@hotmail.com> | 2021-03-14 21:30:19 +0100 |
---|---|---|
committer | Sean Molenaar <SMillerDev@users.noreply.github.com> | 2021-04-08 23:17:31 +0200 |
commit | 3a02101e668d6bc1a05d6c6b35708e97d22a104f (patch) | |
tree | b82e3076e3d7b7b23134c7f501d917833da7c941 /js | |
parent | f7820fffd06c85e765cba8350bf79e54da7982c0 (diff) |
♻️ ShareController: refactor dictionary + fncts
- updated dictionary structure to contain share status
- divided code into functions
- adapted code to match new implementation
Signed-off-by: Marco Nassabain <marco.nassabain@hotmail.com>
Diffstat (limited to 'js')
-rw-r--r-- | js/controller/ShareController.js | 63 |
1 files changed, 54 insertions, 9 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; + }; + }); |