From bf2c7110f113045344c3b2f30274c5effacaa00c Mon Sep 17 00:00:00 2001 From: fauzanmhr Date: Fri, 23 Aug 2024 14:14:48 +0700 Subject: [PATCH] fix when go back from detail page (after search) the data not follow last search --- index.js | 14 ++++++-------- views/all-anime.ejs | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 65ccd66..4308729 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,8 @@ import express from 'express'; import 'dotenv/config'; import path from 'path'; import { fileURLToPath } from 'url'; -import * as https from "node:https"; -import http from "http"; +import * as https from 'node:https'; +import http from 'http'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -41,9 +41,7 @@ const fetchAllAnimeData = async () => { })(); // Periodically update all anime data (every 60 minutes) -setInterval(async () => { - await fetchAllAnimeData(); -}, 60 * 60 * 1000); +setInterval(fetchAllAnimeData, 60 * 60 * 1000); // Home route to list content with pagination app.get('/', async (req, res) => { @@ -62,13 +60,13 @@ app.get('/search-ajax', (req, res) => { const { q: query, page = 1, limit = 20 } = req.query; if (!query) return res.json({ results: [] }); - let filteredResults = allAnimeData.filter(anime => anime.judul.toLowerCase().includes(query.toLowerCase())); + const filteredResults = allAnimeData.filter(anime => anime.judul.toLowerCase().includes(query.toLowerCase())); const startIndex = (page - 1) * limit; const endIndex = page * limit; const results = filteredResults.slice(startIndex, endIndex); res.json({ - results: results, + results, currentPage: parseInt(page, 10), totalPages: Math.ceil(filteredResults.length / limit) }); @@ -83,7 +81,7 @@ app.get('/all-anime-ajax', (req, res) => { const results = allAnimeData.slice(startIndex, endIndex); res.json({ - results: results, + results, currentPage: parseInt(page, 10), totalPages: Math.ceil(allAnimeData.length / limit) }); diff --git a/views/all-anime.ejs b/views/all-anime.ejs index 8a3d9b7..06473e8 100644 --- a/views/all-anime.ejs +++ b/views/all-anime.ejs @@ -39,6 +39,12 @@ const resultsContainer = document.getElementById('searchResults'); const paginationContainer = document.getElementById('pagination'); + // Update the URL with the search query + const url = new URL(window.location); + url.searchParams.set('q', query); + url.searchParams.set('page', page); + history.pushState({}, '', url); + if (query.trim() === '') { fetch(`/all-anime-ajax?page=${page}&limit=${limit}`) .then(response => response.json()) @@ -149,8 +155,14 @@ container.appendChild(nextLi); } - // Initial fetch - fetchData(); + document.addEventListener('DOMContentLoaded', () => { + const searchInput = document.getElementById('searchInput'); + const urlParams = new URLSearchParams(window.location.search); + const initialQuery = urlParams.get('q') || ''; + searchInput.value = initialQuery; + const initialPage = urlParams.get('page') || 1; + fetchData(initialQuery, initialPage); + }); \ No newline at end of file