Skip to content

Commit

Permalink
add search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
cdietschrun committed Jan 1, 2024
1 parent 4c3a02a commit 07b4ef0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
9 changes: 9 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"chart.js": "^4.4.0",
"d3": "^7.8.5",
"date-fns": "^2.30.0",
"fuse.js": "^7.0.0",
"react": "^18.2.0",
"react-chartjs-2": "^5.2.0",
"react-dom": "^18.2.0",
Expand Down
37 changes: 34 additions & 3 deletions client/src/components/GameSessionList.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
import "../styles/GameSessionList.css";

import React, { useContext, useState } from "react";
import React, { useContext, useEffect, useState } from "react";
import GameSessionCard from "./GameSessionCard";
import { GoodplaysContext } from "../models/GoodplaysContextType";
import AddGameSessionModal from "./AddGameSessionModal";
import { fetchGameSessions } from "../AppUtils";
import { faSync } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import Fuse from "fuse.js";

const GameSessionList: React.FC = () => {
const { gameSessions, setGameSessions, goodplaysUser } =
useContext(GoodplaysContext);

const [search, setSearch] = useState("");
const [searchResults, setSearchResults] = useState(gameSessions);

const fuseOptions = {
keys: ['gameName'],
includeScore: true,
threshold: 0.3,
};
const fuse = new Fuse(gameSessions, fuseOptions);
useEffect(() => {
if (search === '') {
setSearchResults(gameSessions);
} else {
const result = fuse.search(search);
const sortedResults = result.map(({ item }) => item).sort((a, b) => {
// Assuming endTimestamp is a Date object
return new Date(b.endTimestamp).getTime() - new Date(a.endTimestamp).getTime();
});
setSearchResults(sortedResults);
}
}, [search, gameSessions]);

const gamesPerPage = 5;
const totalPages = Math.ceil(gameSessions.length / gamesPerPage);
const totalPages = Math.ceil(searchResults.length / gamesPerPage);

const paginate = (pageNumber: number) => {
const startIndex = (pageNumber - 1) * gamesPerPage;
const endIndex = startIndex + gamesPerPage;
return gameSessions.slice(startIndex, endIndex);
return searchResults.slice(startIndex, endIndex);
};

const [currentPage, setCurrentPage] = useState(1);
Expand Down Expand Up @@ -81,6 +104,14 @@ const GameSessionList: React.FC = () => {
</button>
<br />

<input
type="text"
value={search}
onChange={(e) => setSearch(e.target.value)}
placeholder="Search game sessions"
/>
<br />

{currentGameSessions.map((session, index) => (
<GameSessionCard key={index} session={session} />
))}
Expand Down

0 comments on commit 07b4ef0

Please sign in to comment.