Skip to content

Commit ef82d0c

Browse files
authored
Merge pull request #30 from devondragon/issue-27-Build-Admin-Lock-Account-Flow-in-Demo-App
Issue 27 build admin lock account flow in demo app
2 parents a019533 + 5b28b4e commit ef82d0c

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
document.addEventListener('DOMContentLoaded', () => {
2+
const form = document.getElementById('adminActionForm');
3+
const usernameInput = document.getElementById('username');
4+
const actionSelect = document.getElementById('actionType');
5+
const globalMessage = document.getElementById('globalMessage');
6+
7+
form.addEventListener('submit', async (e) => {
8+
e.preventDefault();
9+
10+
const email = usernameInput.value.trim();
11+
const action = actionSelect.value;
12+
13+
if (!email || !action) {
14+
showMessage('Please fill all fields', 'danger');
15+
return;
16+
}
17+
18+
const endpoint = action === 'LOCK' ? '/admin/lockAccount' : '/admin/unlockAccount';
19+
20+
try {
21+
const response = await fetch(endpoint, {
22+
method: 'POST',
23+
headers: {
24+
"Content-Type": "application/json",
25+
[document.querySelector("meta[name='_csrf_header']").content]:
26+
document.querySelector("meta[name='_csrf']").content,
27+
},
28+
body: JSON.stringify({ email }),
29+
});
30+
31+
const result = await response.json();
32+
33+
if (response.ok) {
34+
showMessage(result.messages[0] || 'Action successful!', 'success');
35+
} else {
36+
showMessage(result.messages[0] || 'Something went wrong.', 'danger');
37+
}
38+
} catch (error) {
39+
console.error(error);
40+
showMessage('Error occurred while performing action', 'danger');
41+
}
42+
});
43+
44+
function showMessage(message, type) {
45+
globalMessage.className = `alert alert-${type} text-center`;
46+
globalMessage.textContent = message;
47+
globalMessage.classList.remove('d-none');
48+
}
49+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:th="http://www.thymeleaf.org" layout:decorate="~{layout}">
3+
4+
<head>
5+
<title>Admin Actions</title>
6+
</head>
7+
8+
<body>
9+
<div layout:fragment="content">
10+
<section id="main_content" class="my-5">
11+
<div class="container">
12+
<div class="text-center mb-4">
13+
<h1>Admin Actions</h1>
14+
</div>
15+
16+
<div id="globalMessage" class="alert alert-info text-center d-none"></div>
17+
18+
<div class="card shadow-sm">
19+
<div class="card-body">
20+
<form id="adminActionForm">
21+
<div class="row mb-3 align-items-center">
22+
<label for="username" class="col-sm-3 col-form-label">Username / Email</label>
23+
<div class="col-sm-7">
24+
<input type="text" id="username" name="username" class="form-control" required>
25+
<div id="usernameError" class="form-text text-danger d-none"></div>
26+
</div>
27+
</div>
28+
29+
<!-- Action Type -->
30+
<div class="row mb-4 align-items-center">
31+
<label for="actionType" class="col-sm-3 col-form-label">Action</label>
32+
<div class="col-sm-7">
33+
<select id="actionType" name="actionType" class="form-select" required>
34+
<option value="">Select Action</option>
35+
<option value="LOCK">Lock Account</option>
36+
<option value="UNLOCK">Unlock Account</option>
37+
</select>
38+
<div id="actionTypeError" class="form-text text-danger d-none"></div>
39+
</div>
40+
</div>
41+
42+
<!-- Submit Button -->
43+
<div class="text-center">
44+
<button type="submit" class="btn btn-danger">Submit</button>
45+
</div>
46+
</form>
47+
</div>
48+
</div>
49+
</div>
50+
</section>
51+
52+
<script type="module" th:src="@{/js/admin/admin-action.js}"></script>
53+
</div>
54+
</body>
55+
56+
</html>

src/main/resources/templates/fragments/header.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="accountDropdown">
4646
<li><a class="dropdown-item" th:href="${@environment.getProperty('user.security.updateUserURI')}">Update Account</a></li>
4747
<li><a class="dropdown-item" th:href="@{/user/update-password.html}">Change Password</a></li>
48+
<li sec:authorize="hasAuthority('ADMIN_PRIVILEGE')">
49+
<a class="dropdown-item" th:href="@{/admin/actions.html}">Admin Actions</a>
50+
</li>
4851
<li>
4952
<form th:action="@{/user/logout}" method="POST" class="d-inline">
5053
<button class="dropdown-item" type="submit">Logout</button>

0 commit comments

Comments
 (0)