From 88cba88a1c5dd9c6e301e936ee1ebfe1cadccfc4 Mon Sep 17 00:00:00 2001
From: Marcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>
Date: Wed, 11 May 2022 11:21:58 +0000
Subject: [PATCH] Fix `eslint` problems, refs #1038

Merge request studip/studip!604
---
 .../assets/javascripts/lib/activityfeed.js    |   2 +-
 resources/vue/mixins/courseware/export.js     |   1 +
 resources/vue/mixins/courseware/import.js     |  27 +++--
 .../vue/store/courseware/courseware.module.js | 107 +++++++++---------
 4 files changed, 70 insertions(+), 67 deletions(-)

diff --git a/resources/assets/javascripts/lib/activityfeed.js b/resources/assets/javascripts/lib/activityfeed.js
index 95145cb6512..a753e1d9b2d 100644
--- a/resources/assets/javascripts/lib/activityfeed.js
+++ b/resources/assets/javascripts/lib/activityfeed.js
@@ -7,7 +7,7 @@ const ActivityFeed = {
     filter: null,
 
     init: function() {
-        STUDIP.ActivityFeed.maxheight = parseInt($('#stream-container').css('max-height').replace(/[^-\d\.]/g, ''));
+        STUDIP.ActivityFeed.maxheight = parseInt($('#stream-container').css('max-height').replace(/[^-\d.]/g, ''));
 
         STUDIP.ActivityFeed.loadFeed(STUDIP.ActivityFeed.filter);
 
diff --git a/resources/vue/mixins/courseware/export.js b/resources/vue/mixins/courseware/export.js
index 1eab732a951..8c73de002c2 100755
--- a/resources/vue/mixins/courseware/export.js
+++ b/resources/vue/mixins/courseware/export.js
@@ -1,3 +1,4 @@
+/* eslint-disable no-await-in-loop */
 import { mapActions, mapGetters } from 'vuex';
 import JSZip from 'jszip';
 import FileSaver from 'file-saver';
diff --git a/resources/vue/mixins/courseware/import.js b/resources/vue/mixins/courseware/import.js
index 406a3b81299..100a0e5bca7 100755
--- a/resources/vue/mixins/courseware/import.js
+++ b/resources/vue/mixins/courseware/import.js
@@ -1,3 +1,4 @@
+/* eslint-disable no-await-in-loop */
 import { mapActions, mapGetters } from 'vuex';
 
 export default {
@@ -73,9 +74,7 @@ export default {
             let root = this.structuralElementById({ id: rootId });
             // add containers and blocks
             if (element.containers?.length > 0) {
-                for (let i = 0; i < element.containers.length; i++) {
-                    await this.importContainer(element.containers[i], root, files);
-                }
+                await Promise.all(element.containers.map((container) => this.importContainer(container, root, files)));
             }
             //compare payload
             let changedData = false;
@@ -145,7 +144,7 @@ export default {
                             currentId: parent_id,
                         });
                     } catch(error) {
-                        this.currentImportErrors.push(this.$gettext('Seite konnte nicht erstellt werden') + ': ' 
+                        this.currentImportErrors.push(this.$gettext('Seite konnte nicht erstellt werden') + ': '
                         + element.attributes.title);
 
                         continue;
@@ -196,11 +195,11 @@ export default {
                     attributes: container.attributes,
                     structuralElementId: structuralElement.id,
                 });
-                
+
             } catch(error) {
-                this.currentImportErrors.push(this.$gettext('Abschnitt konnte nicht erstellt werden') + ': ' 
+                this.currentImportErrors.push(this.$gettext('Abschnitt konnte nicht erstellt werden') + ': '
                 + structuralElement.attributes.title + '→'
-                + block_container.attributes.title);
+                + container.attributes.title);
 
                 return null;
             }
@@ -218,9 +217,9 @@ export default {
                         try {
                             await this.updateContainerPayload(new_container, structuralElement.id, container.blocks[k].id, new_block.id);
                         } catch(error) {
-                            this.currentImportErrors.push(this.$gettext('Abschnittdaten sind beschädigt. Möglicherweise werden nicht alle Blöcke dargestellt') + ': ' 
+                            this.currentImportErrors.push(this.$gettext('Abschnittdaten sind beschädigt. Möglicherweise werden nicht alle Blöcke dargestellt') + ': '
                             + structuralElement.attributes.title + '→'
-                            + block_container.attributes.title);
+                            + container.attributes.title);
                         }
                     }
                 }
@@ -235,7 +234,7 @@ export default {
                     blockType: block.attributes['block-type'],
                 });
             } catch(error) {
-                this.currentImportErrors.push(this.$gettext('Block konnte nicht erstellt werden') + ': ' 
+                this.currentImportErrors.push(this.$gettext('Block konnte nicht erstellt werden') + ': '
                     + element.attributes.title + '→'
                     + block_container.attributes.title + '→'
                     + block.attributes.title);
@@ -266,8 +265,8 @@ export default {
                     containerId: block_container.id,
                 });
             } catch(error) {
-                
-                this.currentImportErrors.push(this.$gettext('Blockdaten sind beschädigt. Es werden die Standardwerte eingesetzt') + ': ' 
+
+                this.currentImportErrors.push(this.$gettext('Blockdaten sind beschädigt. Es werden die Standardwerte eingesetzt') + ': '
                     + element.attributes.title + '→'
                     + block_container.attributes.title + '→'
                     + block.attributes.title);
@@ -282,9 +281,9 @@ export default {
 
             container.attributes.payload.sections.forEach((section, index) => {
                 let blockIndex = section.blocks.findIndex(blockID => blockID === oldBlockId);
-                
+
                 if(blockIndex > -1) {
-                    container.attributes.payload.sections[index].blocks[blockIndex] = newBlockId; 
+                    container.attributes.payload.sections[index].blocks[blockIndex] = newBlockId;
                 }
             });
 
diff --git a/resources/vue/store/courseware/courseware.module.js b/resources/vue/store/courseware/courseware.module.js
index bf07efcc320..bf443700f8c 100755
--- a/resources/vue/store/courseware/courseware.module.js
+++ b/resources/vue/store/courseware/courseware.module.js
@@ -264,16 +264,16 @@ export const actions = {
 
         const activities = rootGetters['users/all'];
 
-        for (const activity of activities) {
-            //load parents for breadcrumb
-            if (activity.type == 'activities') {
-                await this.dispatch('courseware-structural-elements/loadById', {
-                    id: activity.relationships.object.meta['object-id'],
-                });
-            }
-        }
+        const parentFetchers = activities
+              .filter(({ type }) => type === 'activities')
+              .map((activity) => this.dispatch(
+                  'courseware-structural-elements/loadById',
+                  {
+                      id: activity.relationships.object.meta['object-id'],
+                  },
+              ));
 
-        return activities;
+        return Promise.all(parentFetchers).then(() => activities);
     },
 
     async createFile(context, { file, filedata, folder }) {
@@ -400,17 +400,6 @@ export const actions = {
         return dispatch('courseware-structure/loadDescendants', { root: newElement });
     },
 
-    lockObject({ dispatch, getters }, { id, type }) {
-        return dispatch(`${type}/setRelated`, {
-            parent: { id, type },
-            relationship: 'edit-blocker',
-            data: {
-                type: 'users',
-                id: getters.userId,
-            },
-        });
-    },
-
     async createBlockInContainer({ dispatch }, { container, blockType }) {
         const block = {
             attributes: {
@@ -923,24 +912,32 @@ export const actions = {
         const limit = 100;
         let offset = 0;
 
-        do {
-            const optionsWithPages = {
-                ...options,
-                'page[offset]': offset,
-                'page[limit]': limit,
-            };
-            await dispatch(
+        await loadPage(offset, limit);
+        const total = rootGetters[`${type}/lastMeta`].page.total;
+
+        const pages = [];
+        for (let page = 1; page * limit < total; page++) {
+            pages.push(loadPage(page * limit, limit));
+        }
+
+        return Promise.all(pages);
+
+        function loadPage(offset, limit) {
+            return dispatch(
                 `${type}/loadRelated`,
                 {
                     parent,
                     relationship,
-                    options: optionsWithPages,
+                    options: {
+                        ...options,
+                        'page[offset]': offset,
+                        'page[limit]': limit,
+                    },
                     resetRelated: false,
                 },
                 { root: true }
-            );
-            offset += limit;
-        } while (rootGetters[`${type}/all`].length < rootGetters[`${type}/lastMeta`].page.total);
+            )
+        }
     },
 
     loadUsersBookmarks({ dispatch, rootGetters, state }, userId) {
@@ -982,30 +979,36 @@ export const actions = {
             relationship,
         });
 
-        let courses = [];
-        for (let membership of memberships) {
-            if (
-                (membership.attributes.permission === 'dozent' || membership.attributes.permission === 'tutor') &&
-                state.context.id !== membership.relationships.course.data.id
-            ) {
-                const course = rootGetters['courses/related']({ parent: membership, relationship: 'course' });
-                if (!withCourseware) {
-                    courses.push(course);
-                    continue;
-                }
-                const coursewareInstance = await dispatch('loadRemoteCoursewareStructure', {
+        const otherMemberships = memberships.filter(({ attributes, relationships }) => {
+            return ['dozent', 'tutor'].includes(attributes.permission) && state.context.id !== relationships.course.data.id;
+        });
+
+        if (!withCourseware) {
+            return otherMemberships.map((membership) => {
+                return getCourse(membership);
+            });
+        }
+
+        const items = await Promise.all(
+            otherMemberships.map((membership) => {
+                const course = getCourse(membership);
+
+                return dispatch('loadRemoteCoursewareStructure', {
                     rangeId: course.id,
                     rangeType: course.type
-                });
-                if (coursewareInstance?.relationships?.root) {
-                    if (membership.attributes.permission === 'dozent' ||
-                        coursewareInstance.attributes['editing-permission-level'] === 'tutor') {
-                        courses.push(course);
-                    }
-                }
-            }
+                }).then((instance) => ({ instance, membership, course }));
+            })
+        )
+
+        return items
+            .filter(({ instance, membership }) => {
+                return instance?.relationships?.root && (membership.attributes.permission === 'dozent' || instance.attributes['editing-permission-level'] === 'tutor');
+            })
+            .map(({ course }) => course);
+
+        function getCourse(membership) {
+            return rootGetters['courses/related']({ parent: membership, relationship: 'course' });
         }
-        return courses;
     },
 
     async loadRemoteCoursewareStructure({ dispatch, rootGetters }, { rangeId, rangeType }) {
-- 
GitLab