This repository has been archived by the owner on Aug 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created stores for config pages and page
- Loading branch information
Showing
9 changed files
with
72 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,29 @@ | ||
import { Routes, Route, BrowserRouter } from "react-router-dom"; | ||
import { Routes, Route, BrowserRouter, Navigate } from "react-router-dom"; | ||
import { ConfigProvider } from "@stores/config"; | ||
import { PageProvider } from "@stores/page"; | ||
import ErrorBoundary from "./ErrorBoundary"; | ||
import Background from "@components/Background"; | ||
import Header from "@components/Header"; | ||
import Body from "@components/Body"; | ||
import Footer from "@components/Footer"; | ||
import Body from "@components/Body"; | ||
import pages from "@pages"; | ||
|
||
export default function App() { | ||
return <BrowserRouter basename="/"> | ||
<ErrorBoundary fallback="/"> | ||
<Background /> | ||
<Header /> | ||
<Body> | ||
<Routes> | ||
<Route path="/" element={<pages.Home />} /> | ||
</Routes> | ||
</Body> | ||
<Footer /> | ||
</ErrorBoundary > | ||
</BrowserRouter> | ||
return <ConfigProvider> | ||
<PageProvider> | ||
<BrowserRouter basename="/"> | ||
<ErrorBoundary fallback="/"> | ||
<Background /> | ||
<Header /> | ||
<Body> | ||
<Routes> | ||
<Route path="*" element={<Navigate to="/" />} /> | ||
<Route path="/" element={<pages.Page />} /> | ||
</Routes> | ||
</Body> | ||
<Footer /> | ||
</ErrorBoundary > | ||
</BrowserRouter> | ||
</PageProvider> | ||
</ConfigProvider> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,23 @@ | ||
import { useState } from "react"; | ||
import { useContext } from "react"; | ||
import { PageContext } from "@stores/page"; | ||
import { pages } from "@stores/pages"; | ||
import styles from "@styles"; | ||
|
||
const pages = 4; | ||
|
||
export default function Footer() { | ||
const [page, setPage] = useState(1); | ||
|
||
const pageHandler = (direction) => { | ||
if (typeof direction !== "string") throw new Error("direction must be typeof string"); | ||
const { page, setPage } = useContext(PageContext); | ||
|
||
switch (direction) { | ||
case "backwards": | ||
if (page - 1 < 1) throw new Error("page cannot be less than 1"); | ||
else setPage(page - 1); | ||
break; | ||
case "forwards": | ||
if (page + 1 > pages) throw new Error(`page cannot be greater than ${pages}`); | ||
else setPage(page + 1); | ||
break; | ||
default: | ||
throw new Error("direction must be either \"backwards\" or \"forwards\""); | ||
} | ||
} | ||
const forwards = () => setPage(page + 1); | ||
const backwards = () => setPage(page - 1); | ||
|
||
return <div className={styles.all.footer}> | ||
<div aria-disabled={page === 1} className={styles.all.footerLeft} onClick={pageHandler.bind(null, "backwards")}>Back</div> | ||
<div aria-disabled={page < 2} className={styles.all.footerLeft} onClick={() => { | ||
if (page > 1) backwards(); | ||
}}>Back</div> | ||
|
||
<div className={styles.all.footerCenter}>{page} / {pages}</div> | ||
|
||
<div aria-disabled={page === pages} className={styles.all.footerRight} onClick={pageHandler.bind(null, "forwards")}>Next</div> | ||
<div aria-disabled={page === pages} className={styles.all.footerRight} onClick={() => { | ||
if (page < pages) forwards(); | ||
}}>Next</div> | ||
</div> | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { useContext } from "react" | ||
import { PageContext } from "@stores/page" | ||
|
||
export default function Page() { | ||
const { page } = useContext(PageContext); | ||
|
||
return <h1>Page {page}</h1> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import Error from "./Error"; | ||
import Home from "./Home"; | ||
import Page from "./Page"; | ||
|
||
export default { | ||
Error, | ||
Home | ||
Page | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createContext, useEffect, useState } from "react"; | ||
|
||
export const ConfigContext = createContext(); | ||
|
||
export function ConfigProvider({ children }) { | ||
const [config, setConfig] = useState(JSON.parse(localStorage.getItem('config')) || {}); | ||
|
||
useEffect(() => localStorage.setItem('config', JSON.stringify(config)), [config]); | ||
|
||
return <ConfigContext.Provider value={{ config, setConfig }}> | ||
{children} | ||
</ConfigContext.Provider> | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createContext, useState, useEffect } from "react"; | ||
|
||
export const PageContext = createContext(); | ||
|
||
export function PageProvider({ children }) { | ||
const [page, setPage] = useState(parseInt(localStorage.getItem("page")) || 1); | ||
|
||
useEffect(() => localStorage.setItem("page", page), [page]); | ||
|
||
return <PageContext.Provider value={{ page, setPage }}> | ||
{children} | ||
</PageContext.Provider> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const pages = 6; | ||
|
||
export { pages }; |