fix and stuff

This commit is contained in:
Kevin Kaldoja 2025-04-24 18:21:28 +03:00
parent 9866d8951b
commit f03cc3e203
3 changed files with 98 additions and 118 deletions

View File

@ -12,23 +12,41 @@ const PASSWORD = process.env.ADMIN_PASSWORD;
app.set("view engine", "ejs"); app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public")); app.use(express.static("public"));
app.use((req, res, next) => {
console.log(`[${new Date().toLocaleString()}] ${req.method} ${req.url}`);
next();
});
// Load packages from JSON
function loadPackages() { function loadPackages() {
if (!fs.existsSync(DATA_PATH)) return [];
return JSON.parse(fs.readFileSync(DATA_PATH)); return JSON.parse(fs.readFileSync(DATA_PATH));
} }
// Save packages to JSON
function savePackages(data) { function savePackages(data) {
fs.writeFileSync(DATA_PATH, JSON.stringify(data, null, 2)); fs.writeFileSync(DATA_PATH, JSON.stringify(data, null, 2));
} }
// Homepage - tracking form // Format local date/time to "YYYY-MM-DD HH:mm"
function getLocalTimeString() {
const now = new Date();
return now.getFullYear() + "-" +
String(now.getMonth() + 1).padStart(2, '0') + "-" +
String(now.getDate()).padStart(2, '0') + " " +
String(now.getHours()).padStart(2, '0') + ":" +
String(now.getMinutes()).padStart(2, '0');
}
// Home page
app.get("/", (req, res) => { app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html")); res.sendFile(path.join(__dirname, "public", "index.html"));
}); });
// Handle tracking request (POST) // Track package
app.post("/track", (req, res) => { app.post("/track", (req, res) => {
const id = req.body.id?.trim(); // Get package ID from the form submission const id = req.body.id?.trim();
if (!id) return res.send("Invalid tracking ID"); if (!id) return res.send("Invalid tracking ID");
const packages = loadPackages(); const packages = loadPackages();
@ -36,26 +54,23 @@ app.post("/track", (req, res) => {
if (!pack) return res.send(`<p>ID not found</p><a href="/">Back</a>`); if (!pack) return res.send(`<p>ID not found</p><a href="/">Back</a>`);
// Prepare data to be passed to the view
const latest = pack.updates[pack.updates.length - 1]; const latest = pack.updates[pack.updates.length - 1];
const history = [...pack.updates].reverse(); const history = [...pack.updates].reverse();
// Estimate delivery time (3 days from last update) res.render("track", {
const estimatedDeliveryDate = new Date(); id: pack.id,
estimatedDeliveryDate.setDate(estimatedDeliveryDate.getDate() + 3); // Add 3 days latest,
history,
const estimatedDeliveryTime = `Estimated Delivery Date: ${estimatedDeliveryDate.toLocaleDateString()}`; eta: pack.eta || "Not set"
});
// Render track.ejs with the package data
res.render("track", { package: pack, latest, history, estimatedDeliveryTime });
}); });
// Admin login form // Admin panel
app.get("/admin", (req, res) => { app.get("/admin", (req, res) => {
res.render("admin", { packages: null, error: null }); res.render("admin", { packages: null, error: null });
}); });
// Admin password check // Admin login
app.post("/admin", (req, res) => { app.post("/admin", (req, res) => {
const { password } = req.body; const { password } = req.body;
if (password !== PASSWORD) { if (password !== PASSWORD) {
@ -65,16 +80,15 @@ app.post("/admin", (req, res) => {
res.render("admin", { packages, error: null }); res.render("admin", { packages, error: null });
}); });
// Handle updates // Handle update
app.post("/update", (req, res) => { app.post("/update", (req, res) => {
const { id, progress, status, password } = req.body; const { id, progress, status, eta, password } = req.body;
if (password !== PASSWORD) return res.send("Invalid password"); if (password !== PASSWORD) return res.send("Invalid password");
const now = new Date().toISOString().replace("T", " ").slice(0, 16);
const update = { const update = {
progress: parseInt(progress), progress: parseInt(progress),
status, status,
time: now time: getLocalTimeString()
}; };
const packages = loadPackages(); const packages = loadPackages();
@ -82,8 +96,14 @@ app.post("/update", (req, res) => {
if (pack) { if (pack) {
pack.updates.push(update); pack.updates.push(update);
if (eta) pack.eta = eta;
} else { } else {
packages.push({ id, updates: [update] }); const newPackage = {
id,
updates: [update],
eta: eta || ""
};
packages.push(newPackage);
} }
savePackages(packages); savePackages(packages);

View File

@ -110,6 +110,7 @@
<input name="id" placeholder="Tracking ID" required /> <input name="id" placeholder="Tracking ID" required />
<input name="progress" type="number" placeholder="Progress %" required /> <input name="progress" type="number" placeholder="Progress %" required />
<input name="status" placeholder="Status" required /> <input name="status" placeholder="Status" required />
<input name="eta" type="date" placeholder="ETA (optional)" />
<button type="submit">Update</button> <button type="submit">Update</button>
</form> </form>
@ -120,10 +121,11 @@
<strong><%= p.id %></strong><br> <strong><%= p.id %></strong><br>
<% if (p.updates && p.updates.length > 0) { %> <% if (p.updates && p.updates.length > 0) { %>
<%= p.updates[p.updates.length - 1].status %> — <%= p.updates[p.updates.length - 1].status %> —
<%= p.updates[p.updates.length - 1].progress %>% <%= p.updates[p.updates.length - 1].progress %>%<br>
<% } else { %> <% } else { %>
<em>No updates yet</em> <em>No updates yet</em><br>
<% } %> <% } %>
ETA: <%= p.eta || 'Not set' %>
</div> </div>
<% }) %> <% }) %>
</div> </div>

View File

@ -1,9 +1,9 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Track Your Package</title> <title>Tracking Info</title>
<style> <style>
body { body {
font-family: 'Arial', sans-serif; font-family: 'Arial', sans-serif;
@ -14,135 +14,93 @@
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
height: 100vh; min-height: 100vh;
} }
.container { .container {
background-color: #2c2c2c; background-color: #2c2c2c;
border-radius: 15px; border-radius: 15px;
padding: 40px 60px; padding: 40px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3); box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
width: 100%;
max-width: 600px; max-width: 600px;
width: 90%;
}
h1, h2, h3 {
color: #fff;
text-align: center; text-align: center;
} }
h1 { .progress-bar {
font-size: 36px;
color: #f1f1f1;
margin-bottom: 30px;
}
.status {
background-color: #333;
padding: 20px;
margin: 20px 0;
border-radius: 8px;
}
.status h3 {
margin: 0;
color: #4caf50;
}
.status p {
margin: 5px 0;
color: #ddd;
}
.status .progress-bar {
width: 100%;
height: 10px;
background-color: #444; background-color: #444;
border-radius: 5px; border-radius: 10px;
overflow: hidden;
height: 10px; /* Thinner bar */
margin: 20px 0;
} }
.progress { .progress {
height: 100%; height: 100%;
background-color: #4caf50; background-color: #4caf50;
border-radius: 5px; width: <%= latest.progress %>%;
transition: width 0.3s;
} }
.history { ul {
text-align: left; list-style: none;
padding: 20px; padding: 0;
margin-top: 20px; }
background-color: #444;
li {
background-color: #333;
padding: 10px;
border-radius: 8px; border-radius: 8px;
margin: 5px 0;
} }
.history li { .eta-box {
margin-bottom: 10px; margin-top: 30px;
padding: 15px;
border-radius: 10px;
background-color: #333;
text-align: center;
color: #4caf50;
font-weight: bold;
} }
.message { a {
font-size: 18px;
color: #ddd;
margin-top: 20px;
}
.message a {
color: #4caf50; color: #4caf50;
text-decoration: none; text-decoration: none;
} display: block;
text-align: center;
button { margin-top: 20px;
padding: 12px;
width: 100%;
background-color: #4caf50;
color: #fff;
border: none;
border-radius: 10px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
} }
</style> </style>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<h1>Metsamarja Vedu</h1> <h1>Metsamarja Vedu</h1>
<h2>Tracking ID: <%= id %></h2>
<h3><%= latest.status %> — <%= latest.progress %>%</h3>
<% if (package) { %> <div class="progress-bar">
<div class="status"> <div class="progress"></div>
<h3>Package ID: <%= package.id %></h3> </div>
<p>Status: <%= latest.status %></p>
<p>Progress: <%= latest.progress %>%</p>
<!-- Progress Bar --> <h3>Tracking History</h3>
<div class="progress-bar"> <ul>
<div class="progress" style="width: <%= latest.progress + '%' %>"></div> <% history.forEach(u => { %>
</div> <li>[<%= u.time %>] <%= u.status %> — <%= u.progress %>%</li>
</div> <% }) %>
</ul>
<!-- Estimated Delivery Time --> <% if (eta) { %>
<div class="status"> <div class="eta-box">
<h3>Estimated Delivery Time</h3> Estimated Delivery: <%= eta %>
<p><%= estimatedDeliveryTime %></p>
</div>
<!-- Tracking History -->
<div class="history">
<h4>Tracking History:</h4>
<ul>
<% history.forEach(u => { %>
<li><strong><%= u.time %>:</strong> <%= u.status %> <%= u.progress %>%</li>
<% }) %>
</ul>
</div>
<div class="message">
<p>Want to track another package? <a href="/">Go back to Home</a></p>
</div>
<% } else { %>
<div class="message">
<p>Package not found. <a href="/">Try again</a></p>
</div> </div>
<% } %> <% } %>
<a href="/">Back to Home</a>
</div> </div>
</body> </body>
</html> </html>