Test
This commit is contained in:
parent
a986654332
commit
aa13c1ffbb
|
@ -19,7 +19,19 @@ router.get('/', async (req, res) => {
|
|||
});
|
||||
|
||||
router.get('/add', (req, res) => { // Render form to add new ACL entry
|
||||
res.render('acl-add', { user: req.session.user });
|
||||
const dbData = db.query('PRAGMA table_info(ACL)').then(columns => {
|
||||
const doorList = {};
|
||||
columns.forEach(col => {
|
||||
if (col.name !== 'Name' && col.name !== 'CardNumber' && col.name !== 'PIN' && col.name !== 'StartDate' && col.name !== 'EndDate') {
|
||||
doorList[col.name] = false;
|
||||
}
|
||||
});
|
||||
return doorList;
|
||||
}).catch(err => {
|
||||
log.error(`Database error fetching ACL columns: ${err}`);
|
||||
return {};
|
||||
});
|
||||
res.render('acl-add', { user: req.session.user, doorList });
|
||||
});
|
||||
|
||||
router.put('/', async (req, res) => { // Attempt to create new ACL entry. Fail if cardNumber already exists
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Edit ACL Entry</title>
|
||||
<title>Add ACL Entry</title>
|
||||
<style>
|
||||
form#acl-edit-form {
|
||||
max-width: 500px;
|
||||
|
@ -70,7 +70,7 @@
|
|||
</head>
|
||||
|
||||
<body>
|
||||
<form id="acl-edit-form" method="put" action="/acl">
|
||||
<form id="acl-edit-form" method="patch" action="/acl">
|
||||
<div>
|
||||
<label for="Name">Name:</label>
|
||||
<input type="text" id="Name" name="Name" value="" required>
|
||||
|
@ -85,55 +85,30 @@
|
|||
</div>
|
||||
<div>
|
||||
<label for="StartDate">Start Date:</label>
|
||||
<input type="date" id="StartDate" name="StartDate" value="">
|
||||
<input type="date" id="StartDate" name="StartDate"
|
||||
value="<%= new Date().toISOString().slice(0,10) %>" required>
|
||||
</div>
|
||||
<div>
|
||||
<label for="EndDate">End Date:</label>
|
||||
<input type="date" id="EndDate" name="EndDate" value="">
|
||||
<input type="date" id="EndDate" name="EndDate"
|
||||
value="<%= new Date(Date.now() + 99*365.25*24*60*60*1000).toISOString().slice(0,10) %>" required>
|
||||
</div>
|
||||
<fieldset>
|
||||
<legend>Doors</legend>
|
||||
<% Object.keys(doorsList).forEach(function(door) { %>
|
||||
<legend>Doors</legend>
|
||||
<% Object.keys(doorsList).forEach(function(door) { %>
|
||||
<% Object.keys(doorList).forEach(function(door) { %>
|
||||
<div>
|
||||
<label>
|
||||
<input type="checkbox" name="doors[<%= door %>]" value="1">
|
||||
<%= door %>
|
||||
<input type="checkbox" name="doors[<%= door %>]" value="1" <% if (doorsList[door] &&
|
||||
doorsList[door]==1) { %> checked <% } %> >
|
||||
<%= door %>
|
||||
<% doorsList[door] %>
|
||||
</label>
|
||||
</div>
|
||||
<% }) %>
|
||||
<% }) %>
|
||||
</fieldset>
|
||||
<button type="submit">Create</button>
|
||||
<button type="submit">Save</button>
|
||||
</form>
|
||||
document.getElementById('acl-edit-form').addEventListener('submit', function (e) {
|
||||
e.preventDefault();
|
||||
const form = e.target;
|
||||
const formData = new FormData(form);
|
||||
const data = {
|
||||
Name: formData.get('Name'),
|
||||
CardNumber: formData.get('CardNumber'),
|
||||
PIN: formData.get('PIN'),
|
||||
StartDate: formData.get('StartDate'),
|
||||
EndDate: formData.get('EndDate'),
|
||||
doors: {}
|
||||
};
|
||||
<% Object.keys(doorsList).forEach(function (door) { %>
|
||||
data.doors['<%= door %>'] = formData.get('doors[<%= door %>]') ? 1 : 0;
|
||||
<% }) %>
|
||||
fetch(form.action, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
}).then(res => {
|
||||
console.log(res.status, res.statusText);
|
||||
if (res.ok) window.location.href = '/acl';
|
||||
else alert('Failed to create ACL entry');
|
||||
}).catch(err => {
|
||||
alert('Error: ' + err.message);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<script>
|
||||
document.getElementById('acl-edit-form').addEventListener('submit', function (e) {
|
||||
e.preventDefault();
|
||||
|
@ -147,20 +122,22 @@
|
|||
EndDate: formData.get('EndDate'),
|
||||
doors: {}
|
||||
};
|
||||
<% Object.keys(doorsList).forEach(function (door) { %>
|
||||
<% Object.keys(doorsList).forEach(function (door) { %>
|
||||
data.doors['<%= door %>'] = formData.get('doors[<%= door %>]') ? 1 : 0;
|
||||
<% }) %>
|
||||
fetch(form.action, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
}).then(res => {
|
||||
console.log(res.status, res.statusText);
|
||||
if (res.ok) window.location.href = '/acl';
|
||||
else alert('Failed to create ACL entry');
|
||||
}).catch(err => {
|
||||
alert('Error: ' + err.message);
|
||||
});
|
||||
<% }) %>
|
||||
fetch(form.action, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
}).then(res => {
|
||||
console.log(res.status, res.statusText);
|
||||
if (res.ok) window.location.href = '/acl';
|
||||
else alert('Failed to update ACL entry');
|
||||
}).catch(err => {
|
||||
alert('Error: ' + err.message);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in a new issue