Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Leaderboard Search a bit #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 22 additions & 30 deletions src/pages/leaderboard/Leaderboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const defaultTopCardsData = [
export default function Leaderboard() {
const [data, setData] = useState([]);
const [topCardsData, setTopCardsData] = useState(defaultTopCardsData);

const [originalData, setOriginalData] = useState([]);
const imageUrl = `${apiURL}/leaderboard/image/`;
const [inputValue, setInputValue] = useState("");
const [open, setOpen] = React.useState(false);
Expand Down Expand Up @@ -95,45 +95,37 @@ export default function Leaderboard() {
};

useEffect(() => {
const getLeaderboardData = async () => {
try {
const { data, error } = await supabase
.from("leaderboard")
.select()
.order("points", { ascending: false });
if (error) throw error;
setOriginalData(data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just modify the function defined in the outer scope instead of redfining it to add this

setData(data);
} catch (error) {
console.log("error", error);
}
};

getLeaderboardData();
// axios
// .get(`${apiURL}/leaderboard`)
// .then(function (response) {
// let raw_data = response.data
// raw_data.sort((a, b) => b.points - a.points);
// setData(raw_data);
// setTopCardsData(raw_data.slice(0, 3));
// })
// .catch(function (error) {
// console.log(error);
// });
}, []);

// tableData.sort((a, b) => b.points > a.points);


function handleSearch(e) {
console.log(e.target.value);
const searchTerm = e.target.value;
if (searchTerm == "") {
axios
.get(`${apiURL}/leaderboard`)
.then(function (response) {
let raw_data = response.data;
raw_data.sort((a, b) => b.points - a.points);
setData(raw_data);
})
.catch(function (error) {
console.log(error);
});
setInputValue(searchTerm);

if (searchTerm === "") {
setData(originalData);
} else {
setInputValue(searchTerm);

const results = data.filter((item) =>
const results = originalData.filter((item) =>
item.name.toLowerCase().includes(searchTerm.toLowerCase())
);
setData(results);
}
}


return (
<>
Expand Down