Skip to content

Commit

Permalink
feat(frontend): request update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Urban committed Oct 30, 2023
1 parent 8db114a commit d241a09
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions frontend/src/Requests.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<script>
import { onMount } from 'svelte';
import { onMount } from 'svelte';
let requests = [];
onMount(async () => {
await fetchRequests();
});
async function fetchRequests() {
try {
const response = await fetch('/api/requests');
if (response.ok) {
Expand All @@ -14,37 +18,58 @@
} catch (error) {
console.error('Error fetching requests:', error);
}
});
function acceptRequest(id) {
// Handle accept logic here
}
function denyRequest(id) {
// Handle deny logic here
async function updateRequestState(userEmail, siteURL, newState) {
try {
const response = await fetch('/api/requests/update', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ userEmail, siteURL, newState })
});
if (response.ok) {
await fetchRequests(); // Refetch the requests to update the UI
} else {
console.error('Failed to update request state:', response.statusText);
}
} catch (error) {
console.error('Error updating request state:', error);
}
}
function acceptRequest(request) {
updateRequestState(request.user, request.site, 'authorized');
}
function denyRequest(request) {
updateRequestState(request.user, request.site, 'declined');
}
</script>

<div class="container mt-5">
<h2>Access Requests</h2>
<table class="table">
<thead>
<tr>
<th>User</th>
<th>Site</th>
<th>Actions</th>
</tr>
<tr>
<th>User</th>
<th>Site</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{#each requests as request, index (index)}
<tr>
<td>{request.user}</td>
<td>{request.site}</td>
<td>
<button class="btn btn-success btn-sm" on:click={() => acceptRequest(request.id)}>Accept</button>
<button class="btn btn-danger btn-sm" on:click={() => denyRequest(request.id)}>Deny</button>
</td>
</tr>
{/each}
{#each requests as request (request.id)}
<tr>
<td>{request.user}</td>
<td>{request.site}>
<td>
<button class="btn btn-success btn-sm" on:click={() => acceptRequest(request)}>Accept</button>
<button class="btn btn-danger btn-sm" on:click={() => denyRequest(request)}>Deny</button>
</td>
</tr>
{/each}
</tbody>
</table>
</div>
</div>

0 comments on commit d241a09

Please sign in to comment.