|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + <title>TechStore - Product Search</title> |
| 7 | + </head> |
| 8 | + <body> |
| 9 | + <header> |
| 10 | + <h1>🛍️ TechStore - Find Your Perfect Product</h1> |
| 11 | + </header> |
| 12 | + |
| 13 | + <main> |
| 14 | + <form onsubmit="event.preventDefault(); searchProducts();"> |
| 15 | + <fieldset> |
| 16 | + <legend>Product Search</legend> |
| 17 | + <p> |
| 18 | + <label for="searchQuery">Search Products:</label><br /> |
| 19 | + <input |
| 20 | + type="text" |
| 21 | + id="searchQuery" |
| 22 | + placeholder="Search for phones, laptops, headphones..." |
| 23 | + size="50" |
| 24 | + required /> |
| 25 | + <button type="submit">🔍 Search</button> |
| 26 | + </p> |
| 27 | + </fieldset> |
| 28 | + </form> |
| 29 | + |
| 30 | + <fieldset> |
| 31 | + <legend>Live Notifications</legend> |
| 32 | + <p id="status">🟡 Connecting to live notifications...</p> |
| 33 | + </fieldset> |
| 34 | + |
| 35 | + <section id="searchResults"> |
| 36 | + <h2>Search Results</h2> |
| 37 | + <blockquote> |
| 38 | + <em>🔍 Enter a search term above to find products</em> |
| 39 | + </blockquote> |
| 40 | + </section> |
| 41 | + </main> |
| 42 | + |
| 43 | + <!-- HTML Dialog for notifications --> |
| 44 | + <dialog id="notificationDialog"> |
| 45 | + <fieldset> |
| 46 | + <legend>🔔 Live Search Activity</legend> |
| 47 | + <p id="notificationMessage"></p> |
| 48 | + <p> |
| 49 | + <button onclick="closeNotification()" autofocus>OK</button> |
| 50 | + </p> |
| 51 | + </fieldset> |
| 52 | + </dialog> |
| 53 | + |
| 54 | + <script> |
| 55 | + let ws = null; |
| 56 | + let sessionId = null; |
| 57 | + |
| 58 | + window.onload = function () { |
| 59 | + sessionId = "session_" + Date.now(); |
| 60 | + connectWebSocket(); |
| 61 | + }; |
| 62 | + |
| 63 | + function connectWebSocket() { |
| 64 | + const statusDiv = document.getElementById("status"); |
| 65 | + |
| 66 | + ws = new WebSocket("ws://localhost:8000/ws"); |
| 67 | + |
| 68 | + ws.onopen = function () { |
| 69 | + statusDiv.innerHTML = |
| 70 | + "🟢 Connected - You will see when others search for products"; |
| 71 | + console.log("Connected to WebSocket"); |
| 72 | + }; |
| 73 | + |
| 74 | + ws.onmessage = function (event) { |
| 75 | + try { |
| 76 | + const notification = JSON.parse(event.data); |
| 77 | + if (notification.type === "search") { |
| 78 | + showSearchNotification(notification); |
| 79 | + } |
| 80 | + } catch (error) { |
| 81 | + console.error("Error parsing notification:", error); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + ws.onclose = function () { |
| 86 | + statusDiv.innerHTML = "🔴 Disconnected from live notifications"; |
| 87 | + console.log("Disconnected from WebSocket"); |
| 88 | + }; |
| 89 | + |
| 90 | + ws.onerror = function (error) { |
| 91 | + statusDiv.innerHTML = "❌ Connection error"; |
| 92 | + console.error("WebSocket error:", error); |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + function showSearchNotification(notification) { |
| 97 | + // Skip notifications from the same session (same browser window) |
| 98 | + if (notification.session_id === sessionId) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + const dialog = document.getElementById("notificationDialog"); |
| 103 | + const messageElement = document.getElementById("notificationMessage"); |
| 104 | + |
| 105 | + messageElement.innerHTML = `<p><strong>Hot search alert!</strong> Other users are looking for <em>"${notification.query}"</em> right now.</p>`; |
| 106 | + |
| 107 | + dialog.showModal(); |
| 108 | + } |
| 109 | + |
| 110 | + function closeNotification() { |
| 111 | + const dialog = document.getElementById("notificationDialog"); |
| 112 | + dialog.close(); |
| 113 | + } |
| 114 | + |
| 115 | + async function searchProducts() { |
| 116 | + const query = document.getElementById("searchQuery").value.trim(); |
| 117 | + const resultsDiv = document.getElementById("searchResults"); |
| 118 | + |
| 119 | + if (!query) { |
| 120 | + resultsDiv.innerHTML = ` |
| 121 | + <h2>Search Results</h2> |
| 122 | + <blockquote> |
| 123 | + <strong>⚠️ Please enter a search term</strong> |
| 124 | + </blockquote> |
| 125 | + `; |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + resultsDiv.innerHTML = ` |
| 130 | + <h2>Search Results</h2> |
| 131 | + <blockquote> |
| 132 | + <em>🔍 Searching TechStore inventory...</em> |
| 133 | + </blockquote> |
| 134 | + `; |
| 135 | + |
| 136 | + try { |
| 137 | + const response = await fetch( |
| 138 | + `/search?q=${encodeURIComponent( |
| 139 | + query |
| 140 | + )}&session_id=${encodeURIComponent(sessionId)}` |
| 141 | + ); |
| 142 | + const data = await response.json(); |
| 143 | + |
| 144 | + if (response.ok) { |
| 145 | + displaySearchResults(data); |
| 146 | + } else { |
| 147 | + throw new Error(data.error || "Search failed"); |
| 148 | + } |
| 149 | + } catch (error) { |
| 150 | + resultsDiv.innerHTML = ` |
| 151 | + <h2>Search Results</h2> |
| 152 | + <fieldset> |
| 153 | + <legend>❌ Search Error</legend> |
| 154 | + <p><strong>Error:</strong> ${error.message}</p> |
| 155 | + </fieldset> |
| 156 | + `; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + function displaySearchResults(data) { |
| 161 | + const resultsDiv = document.getElementById("searchResults"); |
| 162 | + |
| 163 | + if (data.results.length === 0) { |
| 164 | + resultsDiv.innerHTML = ` |
| 165 | + <h2>Search Results</h2> |
| 166 | + <fieldset> |
| 167 | + <legend>❌ No products found</legend> |
| 168 | + <p>No products match "<strong>${data.query}</strong>"</p> |
| 169 | + <p><em>Try searching for: iPhone, laptop, headphones, watch, etc.</em></p> |
| 170 | + </fieldset> |
| 171 | + `; |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + let html = `<h2>✅ Found ${data.total} products for "${data.query}"</h2>`; |
| 176 | + |
| 177 | + data.results.forEach((product) => { |
| 178 | + html += ` |
| 179 | + <fieldset> |
| 180 | + <legend><strong>${ |
| 181 | + product.product_name |
| 182 | + }</strong></legend> |
| 183 | + <p><big>💰 $${product.price.toFixed(2)}</big></p> |
| 184 | + <p>${product.description}</p> |
| 185 | + </fieldset> |
| 186 | + `; |
| 187 | + }); |
| 188 | + |
| 189 | + resultsDiv.innerHTML = html; |
| 190 | + } |
| 191 | + </script> |
| 192 | + </body> |
| 193 | +</html> |
0 commit comments