Skip to content

Commit

Permalink
feat: add a context to manage pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
KimJoonSeo committed Dec 11, 2023
1 parent 53b9e36 commit 2ef24fa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
11 changes: 3 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
DatePicker,
DatePickerProps,
Layout,
Pagination,
Row,
Select,
} from 'antd'
Expand All @@ -21,6 +20,7 @@ import ligue1 from './resources/images/ligue1.png'
import europa from './resources/images/europa.png'
import conference from './resources/images/conference.png'
import {PaginationContext, PaginationProvider} from "./contexts";
import Footer from "./components/Footer";

const LeagueOption: React.FC<{ image: string; name: string }> = ({
image,
Expand All @@ -39,7 +39,7 @@ const LeagueOption: React.FC<{ image: string; name: string }> = ({
const App: React.FC = () => {
const [date, setDate] = useState<dayjs.Dayjs>(dayjs())
const [league, setLeague] = useState<string>('eng.1')
const {state, actions} = useContext(PaginationContext);
const { state, actions} = useContext(PaginationContext)
const leagueOptions = [
{label: 'Nation', options: [
{
Expand Down Expand Up @@ -85,7 +85,6 @@ const App: React.FC = () => {
setLeague(value)
}
return (
<PaginationProvider>
<Layout>
<Layout.Content style={{ height: 580 }}>
<Row>
Expand Down Expand Up @@ -114,12 +113,8 @@ const App: React.FC = () => {
/>
</Row>
</Layout.Content>
<Layout.Footer style={{textAlign: 'center', padding: 0}}>
<Pagination defaultCurrent={state.currentPage} total={state.totalCount}
pageSize={12} onChange={page => {actions.setCurrentPage(page)}}/>
</Layout.Footer>
<Footer />
</Layout>
</PaginationProvider>
)
}

Expand Down
13 changes: 7 additions & 6 deletions src/components/DashBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@ interface Props {

export const DashBoard: React.FC<Props> = ({ date, league }) => {
const { isLoading, error, data } = useScoreBoardData(date, league)
const {state, actions} = useContext(PaginationContext)
const { state, actions} = useContext(PaginationContext)

useEffect(() => {
actions.setCurrentPage(1)
actions.setTotalCount(data?.events.length)
if (error) {
actions.setCurrentPage(1)
actions.setTotalCount(1)
showMessage('error', 'An error has occurred while fetching data.')
}
if (data?.events.length === 0) {
actions.setCurrentPage(1)
actions.setTotalCount(1)
showMessage('info', 'No game today!')
} else {
} else if (typeof data?.events !== 'undefined') {
actions.setCurrentPage(1)
actions.setTotalCount(data?.events.length)
console.log('test')
}
}, [data, error])

Expand All @@ -42,9 +41,11 @@ export const DashBoard: React.FC<Props> = ({ date, league }) => {
return (
<>
{data?.events.map((event, i) => (
i >= (state.currentPage-1)*12 && i < state.currentPage*12 ?
<Col span={6} key={i}>
<ScoreCard status={event.status} competitions={event.competitions} />
</Col>
: null
))}
</>
)
Expand Down
15 changes: 15 additions & 0 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Layout, Pagination} from "antd";
import React, {useContext} from "react";
import {PaginationContext} from "../contexts";

const Footer: React.FC = () => {
const { state, actions} = useContext(PaginationContext)
return (
<Layout.Footer style={{textAlign: 'center', padding: 0}}>
<Pagination defaultCurrent={state.currentPage} total={state.totalCount}
pageSize={12} onChange={page => {actions.setCurrentPage(page)}}/>
</Layout.Footer>
)
}

export default Footer
5 changes: 4 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
QueryClientConfig,
QueryClientProvider,
} from '@tanstack/react-query'
import {PaginationProvider} from "./contexts";

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)

Expand All @@ -27,7 +28,9 @@ const queryClient = new QueryClient(config)

root.render(
<QueryClientProvider client={queryClient}>
<App />
<PaginationProvider>
<App />
</PaginationProvider>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>,
)
Expand Down

0 comments on commit 2ef24fa

Please sign in to comment.