Skip to content

Commit

Permalink
Added boilerplate page for managing urls
Browse files Browse the repository at this point in the history
  • Loading branch information
masoncfrancis committed Jun 11, 2024
1 parent 781faf1 commit ac53e79
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>URL Manager</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1 class="mt-5">URL Manager</h1>

<div class="mt-4">
<h2>Add URL</h2>
<input type="text" id="add-url" class="form-control" placeholder="Enter long URL">
<button id="add-button" class="btn btn-primary mt-2">Add URL</button>
</div>

<div class="mt-4">
<h2>Remove URL</h2>
<input type="text" id="remove-url" class="form-control" placeholder="Enter short or long URL">
<button id="remove-button" class="btn btn-danger mt-2">Remove URL</button>
</div>

<div class="mt-4">
<h2>All URLs</h2>
<div id="url-list"></div>
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
const API_BASE = 'http://localhost:4567/api';

document.getElementById('add-button').addEventListener('click', async () => {
const longUrl = document.getElementById('add-url').value;
const response = await fetch(`${API_BASE}/add`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ LongURL: longUrl })
});
const data = await response.json();
alert(data.message);
document.getElementById('add-url').value = '';
loadUrls();
});

document.getElementById('remove-button').addEventListener('click', async () => {
const url = document.getElementById('remove-url').value;
let endpoint = `${API_BASE}/remove/short/${url}`;
if (url.startsWith('http')) {
endpoint = `${API_BASE}/remove/dest`;
}
const response = await fetch(endpoint, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ LongURL: url })
});
const data = await response.json();
alert(data.message);
document.getElementById('remove-url').value = '';
loadUrls();
});

async function loadUrls() {
const response = await fetch(`${API_BASE}/list`);
const urls = await response.json();
const listDiv = document.getElementById('url-list');
listDiv.innerHTML = '';
urls.forEach(url => {
const div = document.createElement('div');
div.textContent = `Short: ${url.short_url}, Long: ${url.long_url}`;
listDiv.appendChild(div);
});
}

loadUrls();
</script>
</body>
</html>

0 comments on commit ac53e79

Please sign in to comment.