]> littlesong.place Git - swapshop.git/commitdiff
Only reload on change
authorChris Fulljames <christianfulljames@gmail.com>
Sat, 23 May 2026 15:03:21 +0000 (11:03 -0400)
committerChris Fulljames <christianfulljames@gmail.com>
Sat, 23 May 2026 15:03:21 +0000 (11:03 -0400)
index.php

index 23a0b49f5025b53404c60377d175b3a8e9bbbb11..63a24e9700f149a43c3bfec2018af90367496b9c 100755 (executable)
--- a/index.php
+++ b/index.php
@@ -1,8 +1,9 @@
 <?php
 const BASE_URL = "https://swapshop.littlesong.place";
 $GLOBALS['db'] = new PDO("sqlite:database.db");
-$event = $_GET['event'];
-$name = $_GET['name'];
+$event = $_GET['event'] ?? NULL;
+$name = $_GET['name'] ?? NULL;
+$count = $_GET['count'] ?? NULL;
 
 function event_url($e) {
     return "?event=$e";
@@ -61,6 +62,20 @@ if (isset($event) && isset($name)) {
 
 $peeps = get_peeps($event);
 $shareable_link = BASE_URL.event_url($event);
+
+// Background JSON check whether more users have joined
+if (isset($event) && isset($count)) {
+    // Check of more users have joined
+    header('Content-type: application/json');
+    if (count($peeps) != $count) {
+        echo '{"reload": true}';
+    }
+    else {
+        echo '{"reload": false}';
+    }
+    exit;
+}
+
 ?>
 <!DOCTYPE html>
 <html>
@@ -99,10 +114,20 @@ $shareable_link = BASE_URL.event_url($event);
     }
     </style>
     <meta name="viewport" content="width=device-width, initial-scale=1">
+    <?php if (isset($event)): ?>
+    <script>
+    async function reloadIfChanged() {
+        const response = await fetch("/?event=<?= $event ?>&count=<?= count($peeps) ?>");
+        const result = await response.json();
+        if (result.reload) location.reload();
+        else setTimeout(reloadIfChanged, 10000);
+    }
+    </script>
+    <?php endif ?>
 </head>
 
-<?php if (isset($name)): # Periodically reload after join ?>
-<body onload="setTimeout('location.reload()', 10000)">
+<?php if (isset($event)): # Periodically reload event page when changed ?>
+<body onload="setTimeout('reloadIfChanged()', 10000)">
 <?php else: ?>
 <body>
 <?php endif ?>