Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Filter by Role to user search #1278

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ func (r usersRoutes) updateUser(c *gin.Context) {

func (r usersRoutes) prepareUserSearch(c *gin.Context) (users []model.User, err error) {
q := c.Query("q")
rQ := c.Query("r")
reg, _ := regexp.Compile("[^a-zA-Z0-9 ]+")
q = reg.ReplaceAllString(q, "")
if len(q) < 3 {
// Removed in order to make the search work with empty query but selected role
if len(q) < 3 && (rQ == "-1" || rQ == "") {
_ = c.Error(tools.RequestError{
Status: http.StatusBadRequest,
CustomMessage: "query too short (minimum length is 3)",
Expand Down
9 changes: 9 additions & 0 deletions web/template/admin/admin_tabs/users.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
<h2>Search</h2>
<input class="tl-input" placeholder="Search for users" type="text" x-model="userlist.searchInput"
@keyup="userlist.search()">
<div class="bg-transparent text-gray-800 dark:text-gray-200">
<label for="role">Role:</label>
<select id="role" class="bg-transparent dark:text-gray-300 text-gray-800" x-model="userlist.roles" @change="userlist.search()">
<option value="-1">All</option>
<option value="1">Admin</option>
<option value="2">Lecturer</option>
<option value="4">Student</option>
</select>
</div>
<div class="w-4 m-auto">
<i class="fas fa-circle-notch text-4 m-auto animate-spin" x-show="userlist.searchLoading"></i>
</div>
Expand Down
18 changes: 13 additions & 5 deletions web/ts/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@ export class AdminUserList {
showSearchResults: boolean;
searchLoading: boolean;
searchInput: string;
roles: number;

constructor(usersAsJson: object[]) {
this.list = usersAsJson;
this.rowsPerPage = 10;
this.showSearchResults = false;
this.currentIndex = 0;
this.searchInput = "";
this.roles = -1;
this.numberOfPages = Math.ceil(this.list.length / this.rowsPerPage);
this.updateVisibleRows();
}

async search() {
if (this.searchInput.length < 3) {
if (this.searchInput.length < 3 && this.roles == -1) {
this.showSearchResults = false;
this.updateVisibleRows();
return;
}
if (this.searchInput.length > 2) {
} else {
this.searchLoading = true;
fetch("/api/searchUser?q=" + this.searchInput)
fetch("/api/searchUser?q=" + this.searchInput + "&r=" + this.roles)
.then((response) => {
this.searchLoading = false;
if (!response.ok) {
Expand All @@ -42,7 +44,13 @@ export class AdminUserList {
return response.json();
})
.then((r) => {
this.currentPage = r; // show all results on page one.
if (this.roles != -1) {
this.currentPage = r.filter((obj) => {
return obj.role == this.roles;
}); // show all results on page one.
} else {
this.currentPage = r;
}
this.showSearchResults = true;
})
.catch((err) => {
Expand Down
Loading