MDL-66074 mod_forum: Make initial user id optional
authorAndrew Nicols <andrew@nicols.co.uk>
Sun, 6 Oct 2019 03:56:55 +0000 (11:56 +0800)
committerMathew May <mathewm@hotmail.co.nz>
Wed, 30 Oct 2019 02:23:41 +0000 (10:23 +0800)
mod/forum/amd/build/local/grades/grader.min.js
mod/forum/amd/build/local/grades/grader.min.js.map
mod/forum/amd/build/local/grades/local/grader/user_picker.min.js
mod/forum/amd/build/local/grades/local/grader/user_picker.min.js.map
mod/forum/amd/src/local/grades/grader.js
mod/forum/amd/src/local/grades/local/grader/user_picker.js

index d4e333c..8947f9f 100644 (file)
Binary files a/mod/forum/amd/build/local/grades/grader.min.js and b/mod/forum/amd/build/local/grades/grader.min.js differ
index 640f8e8..78982ce 100644 (file)
Binary files a/mod/forum/amd/build/local/grades/grader.min.js.map and b/mod/forum/amd/build/local/grades/grader.min.js.map differ
index 4fb0c82..b413235 100644 (file)
Binary files a/mod/forum/amd/build/local/grades/local/grader/user_picker.min.js and b/mod/forum/amd/build/local/grades/local/grader/user_picker.min.js differ
index a2afa80..331fe30 100644 (file)
Binary files a/mod/forum/amd/build/local/grades/local/grader/user_picker.min.js.map and b/mod/forum/amd/build/local/grades/local/grader/user_picker.min.js.map differ
index 637820d..0087b4f 100644 (file)
@@ -118,7 +118,7 @@ const getSaveUserGradeFunction = (root, setGradeForUser) => {
  * @param {Function} setGradeForUser A function to set the grade for a specific user
  */
 export const launch = async(getListOfUsers, getContentForUser, getGradeForUser, setGradeForUser, {
-    initialUserId = 0, moduleName
+    initialUserId = null, moduleName
 } = {}) => {
 
     const [
@@ -140,9 +140,11 @@ export const launch = async(getListOfUsers, getContentForUser, getGradeForUser,
     // Fetch the userpicker for display.
     const userPicker = await getUserPicker(
         userList,
-        initialUserId,
         updateUserContent,
-        saveGradeFunction
+        saveGradeFunction,
+        {
+            initialUserId,
+        },
     );
 
     // Register all event listeners.
index 9cbf1ee..7945f13 100644 (file)
@@ -33,27 +33,37 @@ class UserPicker {
      * Constructor for the User Picker.
      *
      * @param {Array} userList List of users
-     * @param {Number} initialUserId The ID of the initial user to display
      * @param {Function} showUserCallback The callback used to display the user
      * @param {Function} preChangeUserCallback The callback to use before changing user
-     * @param {Bool} render Whether to render on instantiation
      */
-    constructor(userList, initialUserId, showUserCallback, preChangeUserCallback, render = true) {
+    constructor(userList, showUserCallback, preChangeUserCallback) {
         this.userList = userList;
         this.showUserCallback = showUserCallback;
         this.preChangeUserCallback = preChangeUserCallback;
-
-        // Determine the current index.
-        this.currentUserIndex = userList.findIndex(user => {
-            return user.id === parseInt(initialUserId);
-        });
+        this.currentUserIndex = 0;
 
         // Ensure that render is bound correctly.
         this.render = this.render.bind(this);
+        this.setUserId = this.setUserId.bind(this);
+    }
+
+    /**
+     * Set the current userid without rendering the change.
+     * To show the user, call showUser too.
+     *
+     * @param {Number} userId
+     */
+    setUserId(userId) {
+        // Determine the current index based on the user ID.
+        const userIndex = this.userList.findIndex(user => {
+            return user.id === parseInt(userId);
+        });
 
-        if (render) {
-            this.render();
+        if (userIndex === -1) {
+            throw Error(`User with id ${userId} not found`);
         }
+
+        this.currentUserIndex = userIndex;
     }
 
     /**
@@ -164,13 +174,19 @@ class UserPicker {
  * Create a new user picker.
  *
  * @param {Array} users The list of users
- * @param {Number} currentUserID The userid of the current user
  * @param {Function} showUserCallback The function to call to show a specific user
  * @param {Function} preChangeUserCallback The fucntion to call to save the grade for the current user
+ * @param {Number} [currentUserID] The userid of the current user
  * @returns {UserPicker}
  */
-export default async(users, currentUserID, showUserCallback, preChangeUserCallback) => {
-    const userPicker = new UserPicker(users, currentUserID, showUserCallback, preChangeUserCallback, false);
+export default async(users, showUserCallback, preChangeUserCallback, {
+        initialUserId = null,
+    } = {}
+) => {
+    const userPicker = new UserPicker(users, showUserCallback, preChangeUserCallback);
+    if (initialUserId) {
+        userPicker.setUserId(initialUserId);
+    }
     await userPicker.render();
 
     return userPicker;