summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Nassabain <marco.nassabain@hotmail.com>2021-03-14 21:30:19 +0100
committerSean Molenaar <SMillerDev@users.noreply.github.com>2021-04-08 23:17:31 +0200
commit3a02101e668d6bc1a05d6c6b35708e97d22a104f (patch)
treeb82e3076e3d7b7b23134c7f501d917833da7c941
parentf7820fffd06c85e765cba8350bf79e54da7982c0 (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>
-rw-r--r--js/controller/ShareController.js63
-rw-r--r--templates/part.content.php2
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 }}
<span class="right" style="margin-top: 0.9em; margin-right: 1em"
- ng-class="{'icon-loading-small': App.loading.isLoading(user.value.shareWith), 'icon-checkmark': !App.loading.isLoading(user.value.shareWith) && Share.usersSharedArticles[item.id].includes(user.value.shareWith)}">
+ ng-class="{'icon-loading-small': Share.isLoading(user.value.shareWith), 'icon-checkmark': Share.isSuccessful(item.id, user.value.shareWith)}">
</span>
</a>