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."));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -17,9 +17,16 @@
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
let search = "";
|
let search = "";
|
||||||
if (window.location.hash.startsWith("#")) {
|
if (window.location.hash.startsWith("#")) {
|
||||||
|
if (window.location.hash === "#add_user")
|
||||||
|
$("#add_user_modal").modal('show');
|
||||||
|
else
|
||||||
search = decodeURI(window.location.hash.substring(1));
|
search = decodeURI(window.location.hash.substring(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$('#add_user_modal').on('shown.bs.modal', function() {
|
||||||
|
$(this).find('input:first').focus();
|
||||||
|
});
|
||||||
|
|
||||||
$('#users').DataTable({
|
$('#users').DataTable({
|
||||||
// Setting the DOM for dataTables. Using default with adjustments for margin. See here for syntax details https://datatables.net/reference/option/dom
|
// 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>>" +
|
dom: "<'row'<'col-sm-12 col-md-6 my-1'l><'col-sm-12 col-md-6 my-1'f>>" +
|
||||||
|
@ -38,11 +45,9 @@
|
||||||
<div class="text-center">
|
<div class="text-center">
|
||||||
<h1 class="display-5">
|
<h1 class="display-5">
|
||||||
Users
|
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
|
Add
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
|
||||||
</h1>
|
</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -54,15 +59,15 @@
|
||||||
<h5 class="modal-title" id="add_user_modal_label">Add User</h5>
|
<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>
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||||
</div>
|
</div>
|
||||||
<div id="add_user" class="modal-body">
|
<div class="modal-body">
|
||||||
<form method="POST">
|
<form method="POST" id="add_user">
|
||||||
<label for="nickname" class="form-label">Nickname</label>
|
<label for="nickname" class="form-label">Nickname</label>
|
||||||
<input type="text" maxlength="10" class="form-control" id="nickname" name="nickname" placeholder="nick" required autofocus>
|
<input type="text" maxlength="10" class="form-control" id="nickname" name="nickname" placeholder="nick" required autofocus>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button type="button" class="btn btn-lg btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
<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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using AfRApay.Web.Backend.Database;
|
using AfRApay.Web.Backend.Database;
|
||||||
|
using AfRApay.Web.Backend.Database.Tables;
|
||||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
|
||||||
namespace AfRApay.Web.Pages;
|
namespace AfRApay.Web.Pages;
|
||||||
|
@ -27,9 +28,25 @@ public class IndexModel : PageModel {
|
||||||
|
|
||||||
Response.Redirect($"/#{user.Nickname}");
|
Response.Redirect($"/#{user.Nickname}");
|
||||||
}
|
}
|
||||||
else {
|
else if (Request.Form["action"] == "add_user") {
|
||||||
Response.Redirect("/");
|
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;
|
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("/");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue