Skip to content

Commit 600726b

Browse files
authored
Merge pull request #41 from sea-kg/feature/add_user_teams_history_modal
Add team history modal
2 parents abaeae1 + 9415a2a commit 600726b

19 files changed

+226
-633
lines changed
File renamed without changes.

html/assets/js/api.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ window.ctf01d_tp_api.service_info = function(service_id) {
9090
});
9191
}
9292

93+
window.ctf01d_tp_api.teams_list = function () {
94+
return $.ajax({
95+
url: '/api/v1/teams',
96+
method: 'GET',
97+
});
98+
}
99+
93100
window.ctf01d_tp_api.users_list = function (user_data) {
94101
return $.ajax({
95102
url: '/api/v1/users',

html/assets/js/index.js

Lines changed: 117 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,27 @@ function gameCreate() {
121121
function showLoginForm() {
122122
$('#sign_error_info').html('')
123123
$('#sign_error_info').css({"display": "none"});
124-
$('#signin_username').focus();
125-
$('#signin_username').unbind();
126-
$('#signin_username').keypress(function (e) {
124+
$('#sign_in_username').focus();
125+
$('#sign_in_username').unbind();
126+
$('#sign_in_username').keypress(function (e) {
127127
if (e.which == 13) {
128-
$('#signin_password').focus();
128+
$('#sign_in_password').focus();
129129
return false; // <---- Add this line
130130
}
131131
});
132-
$('#signin_password').unbind();
133-
$('#signin_password').keypress(function (e) {
132+
$('#sign_in_password').unbind();
133+
$('#sign_in_password').keypress(function (e) {
134134
if (e.which == 13) {
135135
doSignIn();
136136
return false; // <---- Add this line
137137
}
138138
});
139-
$('#modal_signin').modal('show');
139+
$('#modal_sign_in').modal('show');
140140
}
141141

142142
function doSignIn() {
143-
var username = $('#signin_username').val();
144-
var password = $('#signin_password').val();
143+
var username = $('#sign_in_username').val();
144+
var password = $('#sign_in_password').val();
145145
$('#sign_error_info').html('')
146146
$('#sign_error_info').css({"display": "none"});
147147
window.ctf01d_tp_api.auth_signin({
@@ -154,7 +154,7 @@ function doSignIn() {
154154
}).done(function(res) {
155155
console.log(res);
156156
showSuccessNotification('Login successful!');
157-
$('#modal_signin').modal('hide');
157+
$('#modal_sign_in').modal('hide');
158158
setTimeout(function () {
159159
window.location.reload();
160160
}, 1000);
@@ -359,25 +359,102 @@ function renderServicesPage() {
359359
})
360360
}
361361

362+
function showCreateUser() {
363+
$('#users_page_error').css({ "display": "none" });
364+
$('#users_page_error').html("");
365+
366+
$('#user_update_user_id').val(0);
367+
$('#user_create_name').val("");
368+
$('#user_create_display_name').val("");
369+
$('#user_create_avatar_url').val("");
370+
$('#user_create_password').val("");
371+
$('#user_create_status').val("");
372+
$('#user_create_role').val("guest");
373+
$('#user_create_team').empty(); // Clear teams
374+
375+
loadTeams(); // Load teams into the select element
376+
377+
$('#btn_user_create_or_update').html("Create");
378+
$('#title_user_create_or_update').html("New User");
379+
$('#modal_edit_or_create_user').modal('show');
380+
}
381+
382+
function loadTeams() {
383+
window.ctf01d_tp_api.teams_list().done(function (teams) {
384+
var teamSelect = $('#user_create_team');
385+
teams.forEach(function (team) {
386+
teamSelect.append(`<option value="${team.id}">${team.name}</option>`);
387+
});
388+
}).fail(function () {
389+
alert('Error loading teams');
390+
});
391+
}
392+
393+
function userCreateOrUpdate() {
394+
$('#users_page_error').css({ "display": "none" });
395+
$('#users_page_error').html("");
396+
397+
var user_id = $('#user_update_user_id').val();
398+
399+
var userName = $('#user_create_name').val();
400+
var userDisplayName = $('#user_create_display_name').val();
401+
var userAvatarUrl = $('#user_create_avatar_url').val();
402+
var userPassword = $('#user_create_password').val();
403+
var userStatus = $('#user_create_status').val();
404+
var userRole = $('#user_create_role').val();
405+
var userTeams = $('#user_create_team').val().map(function (id) {
406+
return id;
407+
});
408+
409+
var userData = {
410+
user_name: userName,
411+
display_name: userDisplayName,
412+
avatar_url: userAvatarUrl,
413+
password: userPassword,
414+
status: userStatus,
415+
role: userRole,
416+
team_ids: userTeams
417+
};
418+
419+
if (user_id == 0) {
420+
window.ctf01d_tp_api.user_create(userData).fail(function (res) {
421+
$('#users_page_error').css({ "display": "block" });
422+
$('#users_page_error').html("Error creating user");
423+
console.error(res);
424+
}).done(function (res) {
425+
$('#modal_edit_or_create_user').modal('hide');
426+
showSuccessNotification('User created successfully!');
427+
renderUsersPage();
428+
});
429+
} else {
430+
window.ctf01d_tp_api.user_update(user_id, userData).fail(function (res) {
431+
$('#users_page_error').css({ "display": "block" });
432+
$('#users_page_error').html("Error updating user");
433+
console.error(res);
434+
}).done(function (res) {
435+
$('#modal_edit_or_create_user').modal('hide');
436+
showSuccessNotification('User updated successfully!');
437+
renderUsersPage();
438+
});
439+
}
440+
}
441+
362442
function renderUsersPage() {
363-
$('.spa-web-page').css({ "display": "" })
364-
$('#users_page').css({ "display": "block" })
443+
$('.spa-web-page').css({ "display": "" });
444+
$('#users_page').css({ "display": "block" });
365445
$('#users_page_error').css({ "display": "none" });
366446
$('#users_page_error').html("");
367447
if (window.location.pathname != "/users/") {
368448
window.location.href = "/users/";
369449
}
370450
window.ctf01d_tp_api.users_list().fail(function (res) {
371-
$('#users_page_error').css({
372-
"display": "block"
373-
});
451+
$('#users_page_error').css({ "display": "block" });
374452
$('#users_page_error').html("Error loading users");
375453
console.error("users_list", res);
376454
}).done(function (res) {
377455
var usersHtml = ""
378456
for (var i in res) {
379457
var user_info = res[i];
380-
console.log("user_info", user_info);
381458
usersHtml += '<div class="card services-card" style="width: 20rem;">';
382459
usersHtml += ' <img class="users-card-avatar" src="' + user_info.avatar_url + '" alt="Image of user">';
383460
usersHtml += ' <div class="card-body">';
@@ -387,14 +464,14 @@ function renderUsersPage() {
387464
usersHtml += ' <p class="card-text"> state ' + escapeHtml(user_info.status) + '</p>';
388465
usersHtml += ' </div>';
389466
usersHtml += ' <div class="card-body">';
390-
usersHtml += ' <button class="btn btn-secondary" onclick="showProfileUser(\'' + user_info.id + '\');">Profile</button>';
467+
usersHtml += ' <button class="btn btn-secondary" onclick="showMyTeams(\'' + user_info.id + '\');">Profile</button>';
391468
usersHtml += ' <button class="btn btn-primary" onclick="showUpdateUser(\'' + user_info.id + '\');">Update</button>';
392469
usersHtml += ' <button class="btn btn-danger" onclick="deleteUser(\'' + user_info.id + '\');">Delete</button>';
393470
usersHtml += ' </div>';
394471
usersHtml += '</div>';
395472
}
396473
$('#users_page_list').html(usersHtml);
397-
})
474+
});
398475
}
399476

400477
function renderPage(pathname) {
@@ -423,6 +500,28 @@ function renderPage(pathname) {
423500
}
424501
}
425502

503+
function showMyTeams(userId) {
504+
window.ctf01d_tp_api.user_profile(userId).fail(function (res) {
505+
$('#my_teams_content').html('<div class="alert alert-danger">Fail to fetch profile</div>');
506+
$('#modal_my_teams').modal('show');
507+
console.error("profile", res);
508+
}).done(function (data) {
509+
var teamHistoryHtml = '<div class="team-history">';
510+
for (var i in data.team_history) {
511+
var team = data.team_history[i];
512+
teamHistoryHtml += '<div class="team">';
513+
teamHistoryHtml += '<div class="team-name">' + team.name + '</div>';
514+
teamHistoryHtml += '<div class="team-dates">Joined at: ' + new Date(team.join).toLocaleString() +
515+
(team.left ? ', Left at: ' + new Date(team.left).toLocaleString() : ', ... ') + '</div>';
516+
teamHistoryHtml += '</div>';
517+
}
518+
teamHistoryHtml += '</div>';
519+
var currentTeamHtml = '<div class="current-team"><strong>Current Team: </strong>' + data.team_name + '</div>';
520+
$('#my_teams_content').html(currentTeamHtml + teamHistoryHtml);
521+
$('#modal_my_teams').modal('show');
522+
});
523+
}
524+
426525

427526
$(document).ready(function () {
428527
console.log("Ready")
File renamed without changes.

0 commit comments

Comments
 (0)