Skip to content

Commit

Permalink
fix when go back from detail page (after search) the data not follow …
Browse files Browse the repository at this point in the history
…last search
  • Loading branch information
Fauzanmhr committed Aug 23, 2024
1 parent 2b10a33 commit bf2c711
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
14 changes: 6 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) => {
Expand All @@ -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)
});
Expand All @@ -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)
});
Expand Down
16 changes: 14 additions & 2 deletions views/all-anime.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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);
});
</script>
</body>
</html>

0 comments on commit bf2c711

Please sign in to comment.