From 57580ea9db92dff25c66143e5e817f054e82ed8d Mon Sep 17 00:00:00 2001 From: Sanjay B Date: Thu, 28 Oct 2021 22:01:08 +0530 Subject: [PATCH] store sort in url --- app/static/js/films.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/static/js/films.js b/app/static/js/films.js index 42185dd..3449f18 100644 --- a/app/static/js/films.js +++ b/app/static/js/films.js @@ -1,9 +1,8 @@ -document.getElementById('sort-select').addEventListener('change', function () { - const sortValue = this.value +function selectChanged() { + const sortValue = document.getElementById('sort-select').value const $films = [...document.querySelectorAll('.film')] - console.log('$films', $films) + location.hash = sortValue $films.sort((a, b) => { - console.log('a', a) const aVal = a.dataset[sortValue] const bVal = b.dataset[sortValue] return aVal < bVal ? -1 : 1 @@ -12,4 +11,12 @@ document.getElementById('sort-select').addEventListener('change', function () { $films.forEach($film => { document.getElementById('films').appendChild($film) }) -}) \ No newline at end of file +} + +document.getElementById('sort-select').addEventListener('change', selectChanged) + +const locationHash = location.hash.replace('#', '') +if (locationHash !== '') { + document.getElementById('sort-select').value = locationHash + selectChanged() +} \ No newline at end of file