GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#865)
by Ahmad
05:34
created

user_edit.js ➔ clearRole   C

Complexity

Conditions 11

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
dl 0
loc 24
rs 5.4
c 0
b 0
f 0
eloc 12

How to fix   Complexity   

Complexity

Complex classes like user_edit.js ➔ clearRole often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
// BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
2
//
3
// Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
4
//
5
// This program is free software; you can redistribute it and/or modify it under the
6
// terms of the GNU Lesser General Public License as published by the Free Software
7
// Foundation; either version 3.0 of the License, or (at your option) any later
8
// version.
9
//
10
// BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
11
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12
// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public License along
15
// with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
16
17
$(document).on('turbolinks:load', function(){
18
  var controller = $("body").data('controller');
19
  var action = $("body").data('action');
20
  if ((controller == "admins" && action == "edit_user") || (controller == "users" && action == "edit")) {
21
    // Reset the value of the form element that allows the user to clear their avatar
22
    $("#clear-avatar").val("false")
23
24
    //Preview the image in the profile to the new image when a file is uploaded
25
    $("#user_avatar").change(function() {
26
      var reader = new FileReader();
0 ignored issues
show
Bug introduced by
The variable FileReader seems to be never declared. If this is a global, consider adding a /** global: FileReader */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
27
28
      reader.onload = function(e) {
29
        var file = e.target.result
30
31
        // Format of file is 'data:image/*;...'
32
        let colonLocation = file.indexOf(":") + 1
33
        var incomingFileType = file.substr(colonLocation, file.indexOf(";")-colonLocation)
34
35
        // check to see that the incoming file type is accepted
36
        if (incomingFileType.startsWith("image/")) {
37
          $(".invalid-message").hide()
38
39
          // If its the first image they upload, remove the letter avatar and preview the image avatar
40
          if ($("#avatar-letter").length > 0) {
41
            $("#clear-avatar").val("false")
42
43
            document.getElementById("avatar-letter").outerHTML = "<img id='avatar-image' class='avatar avatar-xxl mr-5 mt-2'></img>"
44
          }
45
46
          $("#avatar-image").attr("src", file)
47
        } else {
48
          $(".invalid-message").show()
49
          $("#user_avatar").val("")
50
        }
51
      }
52
53
      reader.readAsDataURL(this.files[0]);
54
    })
55
56
    // Preview the default avatar if the user clicks the remove image button
57
    $("#remove-image").click(function() {
58
      $("#clear-avatar").val("true")
59
      document.getElementById("avatar-image").outerHTML = "<span id='avatar-letter' class='avatar avatar-xxl mr-5 mt-2 bg-primary'>" + $("#remove-image").data("first") + "</span>"
60
    })
61
62
    // Clear the role when the user clicks the x
63
    $(".clear-role").click(clearRole)
64
65
    // When the user selects an item in the dropdown add the role to the user
66
    $("#role-select-dropdown").change(function(data){
67
      var dropdown = $("#role-select-dropdown");
68
      var select_role_id = dropdown.val();
69
70
      if(select_role_id){
71
        // Disable the role in the dropdown
72
        var selected_role = dropdown.find('[value=\"' + select_role_id + '\"]');
73
        selected_role.prop("disabled", true)
74
75
        // Add the role tag
76
        var tag_container = $("#role-tag-container");
77
        tag_container.append("<span id=\"user-role-tag_" + select_role_id + "\" style=\"background-color:" + selected_role.data("colour") + ";\" class=\"tag user-role-tag\">" + 
78
          selected_role.text() + "<a data-role-id=\"" + select_role_id + "\" class=\"tag-addon clear-role\"><i data-role-id=\"" + select_role_id + "\" class=\"fas fa-times\"></i></a></span>");
79
80
        // Update the role ids input that gets submited on user update
81
        var role_ids = $("#user_role_ids").val()
82
        role_ids += " " + select_role_id
83
        $("#user_role_ids").val(role_ids)
84
        
85
        // Add the clear role function to the tag
86
        $("#user-role-tag_" + select_role_id).click(clearRole);
87
88
        // Reset the dropdown
89
        dropdown.val(null)
90
      }
91
    })
92
  }
93
})
94
95
// This function removes the specfied role from a user
96
function clearRole(data){
97
  // Get the role id
98
  var role_id = $(data.target).data("role-id");
99
  var role_tag = $("#user-role-tag_" + role_id);
100
101
  // Remove the role tag
102
  $(role_tag).remove()
103
  
104
  // Update the role ids input
105
  var role_ids = $("#user_role_ids").val()
106
  var parsed_ids = role_ids.split(' ')
107
  
108
  var index = parsed_ids.indexOf(role_id.toString());
109
  
110
  if (index > -1) {
111
    parsed_ids.splice(index, 1);
112
  }
113
  
114
  $("#user_role_ids").val(parsed_ids.join(' '))
115
  
116
  // Enable the role in the role select dropdown
117
  var selected_role = $("#role-select-dropdown").find('[value=\"' + role_id + '\"]');
118
  selected_role.prop("disabled", false)
119
}