151 lines
6.6 KiB
Plaintext
151 lines
6.6 KiB
Plaintext
@using CarCareTracker.Helper
|
|
@{
|
|
ViewData["Title"] = "Admin";
|
|
}
|
|
@inject IConfiguration config;
|
|
@inject ITranslationHelper translator
|
|
@{
|
|
bool emailServerIsSetup = true;
|
|
var mailConfig = config.GetSection("MailConfig").Get<MailConfig>();
|
|
var userLanguage = config[nameof(UserConfig.UserLanguage)] ?? "en_US";
|
|
if (mailConfig is null || string.IsNullOrWhiteSpace(mailConfig.EmailServer))
|
|
{
|
|
emailServerIsSetup = false;
|
|
}
|
|
}
|
|
@model AdminViewModel
|
|
<div class="container">
|
|
<div class="row d-flex align-items-center justify-content-between justify-content-md-start">
|
|
<div class="col-2 col-md-1">
|
|
<a href="/Home" class="btn btn-secondary btn-md mt-1 mb-1"><i class="bi bi-arrow-left-square"></i></a>
|
|
</div>
|
|
<div class="col-6 col-md-7 text-end text-md-start">
|
|
<span class="display-6">@translator.Translate(userLanguage, "Admin Panel")</span>
|
|
</div>
|
|
</div>
|
|
<hr />
|
|
<div class="row">
|
|
<div class="col-md-5 col-12">
|
|
<span class="lead">@translator.Translate(userLanguage, "Tokens")</span>
|
|
<hr />
|
|
<div class="row">
|
|
<div class="col-6">
|
|
<button onclick="generateNewToken()" class="btn btn-primary btn-md mt-1 mb-1"><i class="bi bi-pencil-square me-2"></i>@translator.Translate(userLanguage, "Generate User Token")</button>
|
|
</div>
|
|
<div class="col-6 d-flex align-items-center">
|
|
<div class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" role="switch" id="enableAutoNotify" @(emailServerIsSetup ? "checked" : "disabled")>
|
|
<label class="form-check-label" for="enableAutoNotify">@translator.Translate(userLanguage, "Auto Notify(via Email)")</label>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<table class="table table-hover">
|
|
<thead class="sticky-top">
|
|
<tr class="d-flex">
|
|
<th scope="col" class="col-4">@translator.Translate(userLanguage, "Token")</th>
|
|
<th scope="col" class="col-6">@translator.Translate(userLanguage, "Issued To")</th>
|
|
<th scope="col" class="col-2">@translator.Translate(userLanguage, "Delete")</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@foreach (Token token in Model.Tokens)
|
|
{
|
|
<tr class="d-flex">
|
|
<td class="col-4" style="cursor:pointer;" onclick="copyToClipboard(this)">@token.Body</td>
|
|
<td class="col-6 text-truncate">@token.EmailAddress</td>
|
|
<td class="col-2">
|
|
<button type="button" class="btn btn-danger" onclick="deleteToken(@token.Id, this)"><i class="bi bi-trash"></i></button>
|
|
</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="col-12 col-md-7">
|
|
<span class="lead">@translator.Translate(userLanguage, "Users")</span>
|
|
<hr />
|
|
<table class="table table-hover">
|
|
<thead class="sticky-top">
|
|
<tr class="d-flex">
|
|
<th scope="col" class="col-4">@translator.Translate(userLanguage, "Username")</th>
|
|
<th scope="col" class="col-4">@translator.Translate(userLanguage, "Email")</th>
|
|
<th scope="col" class="col-2">@translator.Translate(userLanguage, "Is Admin")</th>
|
|
<th scope="col" class="col-2">@translator.Translate(userLanguage, "Delete")</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@foreach (UserData userData in Model.Users)
|
|
{
|
|
<tr class="d-flex" style="cursor:pointer;">
|
|
<td class="col-4">@userData.UserName</td>
|
|
<td class="col-4">@userData.EmailAddress</td>
|
|
<td class="col-2"><input class="form-check-input" type="checkbox" value="" onchange="updateUserAdmin(@userData.Id, this)" @(userData.IsAdmin ? "checked" : "")/></td>
|
|
<td class="col-2"><button type="button" class="btn btn-danger" onclick="deleteUser(@userData.Id, this)"><i class="bi bi-trash"></i></button></td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script>
|
|
function updateUserAdmin(userId, sender){
|
|
var isChecked = $(sender).is(":checked");
|
|
$.post('/Admin/UpdateUserAdminStatus', { userId: userId, isAdmin: isChecked }, function (data) {
|
|
if (data){
|
|
reloadPage();
|
|
} else {
|
|
errorToast(genericErrorMessage());
|
|
}
|
|
});
|
|
}
|
|
function reloadPage() {
|
|
window.location.reload();
|
|
}
|
|
function deleteToken(tokenId) {
|
|
$.post(`/Admin/DeleteToken?tokenId=${tokenId}`, function (data) {
|
|
if (data) {
|
|
reloadPage();
|
|
} else {
|
|
errorToast(genericErrorMessage());
|
|
}
|
|
});
|
|
}
|
|
function deleteUser(userId) {
|
|
$.post(`/Admin/DeleteUser?userId=${userId}`, function (data) {
|
|
if (data) {
|
|
reloadPage();
|
|
} else {
|
|
errorToast(genericErrorMessage());
|
|
}
|
|
})
|
|
}
|
|
function generateNewToken() {
|
|
Swal.fire({
|
|
title: 'Generate Token',
|
|
html: `
|
|
<input type="text" id="inputEmail" class="swal2-input" placeholder="Email Address">
|
|
`,
|
|
confirmButtonText: 'Generate',
|
|
focusConfirm: false,
|
|
preConfirm: () => {
|
|
const emailAddress = $("#inputEmail").val();
|
|
if (!emailAddress) {
|
|
Swal.showValidationMessage(`Please enter an email address`)
|
|
}
|
|
return { emailAddress }
|
|
},
|
|
}).then(function (result) {
|
|
if (result.isConfirmed) {
|
|
var autoNotify = $("#enableAutoNotify").is(":checked");
|
|
$.get('/Admin/GenerateNewToken', { emailAddress: result.value.emailAddress, autoNotify: autoNotify }, function (data) {
|
|
if (data.success) {
|
|
reloadPage();
|
|
} else {
|
|
errorToast(data.message)
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
</script> |