import React from "react"; import {H1} from "../utils/fonts"; import {FlexColumn, Grid} from "../utils/containers"; import Search from "../components/elements/Search"; import MiniChallenge from "../components/elements/MiniChallenge"; import Pager from "../components/elements/Pager"; import challengesResp from "../prototypeData/challenges"; import {ELEMENTS_PER_PAGE} from "../utils/globals"; import FiltersMenu from "../components/elements/FiltersMenu"; const Challenges = () => { const [pageNr, setPageNr] = React.useState(1); const [challenges, setChallenges] = React.useState(challengesResp); const [filtersMenu, setFiltersMenu] = React.useState(false); const calcPages = () => { return Math.ceil(challenges.length / ELEMENTS_PER_PAGE); } const searchQueryHandler = (event) => { let searchQuery = event.target.value; let challengesToRender = []; setPageNr(1); if (searchQuery === '') setChallenges(challengesResp) else { for (let challenge of challengesResp) { let str = `${challenge.title} ${challenge.describe} ${challenge.type} ${challenge.metric} ${challenge.bestScore} ${challenge.timeLeft} ${challenge.baseline} ${challenge.prize}`; if (str.toLowerCase().includes(searchQuery.toLowerCase())) challengesToRender.push(challenge); } setChallenges(challengesToRender); } } const nextPage = () => { if (pageNr !== calcPages(challenges)) { let newPage = pageNr + 1; setPageNr(newPage); } } const previousPage = () => { if (pageNr !== 1) { let newPage = pageNr - 1; setPageNr(newPage); } } const renderChallenges = () => { const n = (pageNr - 1) * ELEMENTS_PER_PAGE; return ( challenges.slice(n, n + ELEMENTS_PER_PAGE).map((challenge, index) => { return ( ); }) ) } const toggleFiltersMenu = () => { let newFiltersMenu = !filtersMenu; setFiltersMenu(newFiltersMenu); } return ( <>

Challenges

{renderChallenges()}
); } export default Challenges;