Merge pull request #66 from alexwholland/alex/-top_ten_results

Limit result history to 10
This commit is contained in:
Bhavanvir Rai
2023-05-07 16:39:08 -07:00
committed by GitHub
3 changed files with 27 additions and 1 deletions

View File

@@ -0,0 +1,24 @@
// Get array of all dates
var dates = [];
for (var i = 0; i < localStorage.length; i++) {
var shortened_url = localStorage.key(i);
var data = JSON.parse(localStorage.getItem(shortened_url));
dates.push(data.date);
}
// Sort the dates in descending order
dates.sort(function(a, b) {
return new Date(b) - new Date(a);
});
// Select the two most recent dates
var mostRecentDates = dates.slice(0, 10);
// Remove any items that are not in the most recent two dates
for (var i = localStorage.length - 1; i >= 0; i--) {
var shortened_url = localStorage.key(i);
var data = JSON.parse(localStorage.getItem(shortened_url));
if (mostRecentDates.indexOf(data.date) === -1) {
localStorage.removeItem(shortened_url);
}
}

View File

@@ -31,6 +31,7 @@
</div>
<script src="{% static 'clearLocalStorage.js' %}"></script>
<script src="{% static 'removeOldResult.js' %}"></script>
<script src="{% static 'rowResult.js' %}"></script>
<script src="{% static 'filterSearchResults.js' %}"></script>
<script src="{% static 'displayEmptyMessage.js' %}"></script>

View File

@@ -8,7 +8,8 @@
image: "{{ image }}",
title: "{{ title }}",
rating: "{{ price_rating }}",
price: "{{ price }}"
price: "{{ price }}",
date: new Date()
};
var shortened_url = "{{ shortened_url }}";
localStorage.setItem(shortened_url, JSON.stringify(item));