Make AddUser modal work properly
This commit is contained in:
parent
0a5dd05bd2
commit
e5e8b5e215
|
@ -1,19 +0,0 @@
|
|||
@page
|
||||
@model AddUserModel
|
||||
@{
|
||||
ViewData["Title"] = "Add User";
|
||||
}
|
||||
|
||||
<div class="text-center">
|
||||
<h1 class="display-5">
|
||||
Add User
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
<form method="POST">
|
||||
<div class="mb-3">
|
||||
<label for="nickname" class="form-label">Nickname</label>
|
||||
<input type="text" maxlength="10" class="form-control" id="nickname" name="nickname" placeholder="nick" required autofocus>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary" name="action" value="save">Save</button>
|
||||
</form>
|
|
@ -1,29 +0,0 @@
|
|||
using System.Net;
|
||||
using AfRApay.Web.Backend.Database;
|
||||
using AfRApay.Web.Backend.Database.Tables;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
|
||||
namespace AfRApay.Web.Pages;
|
||||
|
||||
public class AddUserModel : PageModel {
|
||||
public void OnGet() { }
|
||||
|
||||
public async void OnPost() {
|
||||
await using var db = new DatabaseContext();
|
||||
if (Request.Form.ContainsKey("nickname") && !string.IsNullOrWhiteSpace(Request.Form["nickname"])) {
|
||||
var nick = Request.Form["nickname"];
|
||||
if (db.Users.Any(p => p.Nickname == nick.ToString())) {
|
||||
Response.Redirect("/ErrorRedirect?redir=/AddUser&message=" + WebUtility.UrlEncode("User with nick already exists."));
|
||||
return;
|
||||
}
|
||||
|
||||
var user = new User { Nickname = nick.ToString(), Balance = 0 };
|
||||
db.Add(user);
|
||||
await db.SaveChangesAsync();
|
||||
Response.Redirect($"/#{user.Nickname}");
|
||||
return;
|
||||
}
|
||||
|
||||
Response.Redirect("/ErrorRedirect?redir=/AddUser&message=" + WebUtility.UrlEncode("Nickname must not be empty."));
|
||||
}
|
||||
}
|
|
@ -14,35 +14,40 @@
|
|||
<script src="~/lib/datatables/jquery.dataTables.min.js"></script>
|
||||
<script src="~/lib/datatables/dataTables.bootstrap5.min.js"></script>
|
||||
<script>
|
||||
$(document).ready( function () {
|
||||
$(document).ready(function () {
|
||||
let search = "";
|
||||
if (window.location.hash.startsWith("#")) {
|
||||
search = decodeURI(window.location.hash.substring(1));
|
||||
if (window.location.hash === "#add_user")
|
||||
$("#add_user_modal").modal('show');
|
||||
else
|
||||
search = decodeURI(window.location.hash.substring(1));
|
||||
}
|
||||
|
||||
$('#add_user_modal').on('shown.bs.modal', function() {
|
||||
$(this).find('input:first').focus();
|
||||
});
|
||||
|
||||
$('#users').DataTable({
|
||||
// Setting the DOM for dataTables. Using default with adjustments for margin. See here for syntax details https://datatables.net/reference/option/dom
|
||||
dom: "<'row'<'col-sm-12 col-md-6 my-1'l><'col-sm-12 col-md-6 my-1'f>>" +
|
||||
"<'row'<'col-sm-12'tr>>" +
|
||||
"<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
|
||||
lengthMenu: [10, 25, 50, 100, -1],
|
||||
pageLength: 25,
|
||||
search: {
|
||||
"<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
|
||||
lengthMenu: [10, 25, 50, 100, -1],
|
||||
pageLength: 25,
|
||||
search: {
|
||||
search: search,
|
||||
}
|
||||
});
|
||||
} );
|
||||
});
|
||||
});
|
||||
</script>
|
||||
}
|
||||
|
||||
<div class="text-center">
|
||||
<h1 class="display-5">
|
||||
Users
|
||||
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#add_user_modal">
|
||||
<button type="button" class="btn btn-lg btn-primary" data-bs-toggle="modal" data-bs-target="#add_user_modal">
|
||||
Add
|
||||
</button>
|
||||
|
||||
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
|
@ -54,15 +59,15 @@
|
|||
<h5 class="modal-title" id="add_user_modal_label">Add User</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div id="add_user" class="modal-body">
|
||||
<form method="POST">
|
||||
<div class="modal-body">
|
||||
<form method="POST" id="add_user">
|
||||
<label for="nickname" class="form-label">Nickname</label>
|
||||
<input type="text" maxlength="10" class="form-control" id="nickname" name="nickname" placeholder="nick" required autofocus>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-lg btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
||||
<button type="submit" form="add_user" class="btn btn-lg btn-primary" name="action" value="save">Save</button>
|
||||
<button type="submit" form="add_user" class="btn btn-lg btn-primary" name="action" value="add_user">Save</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using System.Net;
|
||||
using AfRApay.Web.Backend.Database;
|
||||
using AfRApay.Web.Backend.Database.Tables;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
|
||||
namespace AfRApay.Web.Pages;
|
||||
|
@ -27,9 +28,25 @@ public class IndexModel : PageModel {
|
|||
|
||||
Response.Redirect($"/#{user.Nickname}");
|
||||
}
|
||||
else if (Request.Form["action"] == "add_user") {
|
||||
if (Request.Form.ContainsKey("nickname") && !string.IsNullOrWhiteSpace(Request.Form["nickname"])) {
|
||||
var nick = Request.Form["nickname"];
|
||||
if (db.Users.Any(p => p.Nickname == nick.ToString())) {
|
||||
Response.Redirect("/ErrorRedirect?redir=/%23add_user&message=" + WebUtility.UrlEncode("User with nick already exists."));
|
||||
return;
|
||||
}
|
||||
|
||||
var user = new User { Nickname = nick.ToString(), Balance = 0 };
|
||||
db.Add(user);
|
||||
await db.SaveChangesAsync();
|
||||
Response.Redirect($"/#{user.Nickname}");
|
||||
return;
|
||||
}
|
||||
|
||||
Response.Redirect("/ErrorRedirect?redir=/%23add_user&message=" + WebUtility.UrlEncode("Nickname must not be empty."));
|
||||
}
|
||||
else {
|
||||
Response.Redirect("/");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue