From 593334456a83e3de0a9b7bfbca0c03130112db97 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Fri, 1 Apr 2022 09:49:22 -0400 Subject: [PATCH] Add a local query history to Spinner. --- spinner/lib/src/main/assets/query.hbs | 48 +++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/spinner/lib/src/main/assets/query.hbs b/spinner/lib/src/main/assets/query.hbs index 1d9d104bd9..75eb95e5e7 100644 --- a/spinner/lib/src/main/assets/query.hbs +++ b/spinner/lib/src/main/assets/query.hbs @@ -8,13 +8,17 @@
- + or - + or
+ +

Query History

+
+

Data

{{#if queryResult}} @@ -57,7 +61,47 @@ } } + function onQuerySubmitted() { + const query = document.querySelector('.query-input').value; + + let history = getQueryHistory(); + history.unshift(query); + history = history.slice(0, 10); + + localStorage.setItem('query-history', JSON.stringify(history)); + } + + function renderQueryHistory() { + const container = document.getElementById('history-container'); + + let history = getQueryHistory(); + + if (history.length > 0) { + for (let item of history) { + container.innerHTML += ` + + + ${item} + + ` + } + } else { + container.innerHTML = 'None' + } + } + + function onHistoryItemClicked(item) { + document.querySelector('.query-input').value = item; + } + + function getQueryHistory() { + const historyRaw = localStorage.getItem('query-history') || "[]"; + return JSON.parse(historyRaw); + } + document.querySelector('.query-input').addEventListener("keypress", submitOnEnter); + renderQueryHistory(); +