-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from bettersg/SPA-state-logic
SPA Page Logic using useState
- Loading branch information
Showing
8 changed files
with
132 additions
and
51 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
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,56 +1,27 @@ | ||
import { Container, VStack } from "@chakra-ui/react"; | ||
import { StickyFooter } from "components/footer/StickyFooter"; | ||
import { Banner, UserInput } from "components/home"; | ||
import styles from "components/home/hideScrollbar.module.css"; | ||
import { NAVBAR_HEIGHT } from "components/nav/NavHeader"; | ||
import { useSheetyData } from "hooks/useRecyclableItemList"; | ||
import { useWindowDimensions } from "hooks/useWindowDimensions"; | ||
import { BasePage } from "layouts/BasePage"; | ||
import type { NextPage } from "next"; | ||
import { useEffect, useRef, useState } from "react"; | ||
import { useState } from "react"; | ||
import { HomePage, HomePickupPage, InstructionsPage, MapPage } from "spa-pages"; | ||
import { Pages } from "spa-pages/pageEnums"; | ||
|
||
const Home: NextPage = () => { | ||
const [stickyHeight, setStickyHeight] = useState<number>(0); | ||
const [readyToSubmit, setReadyToSubmit] = useState(false); | ||
const { height } = useWindowDimensions(); | ||
const { isLoaded } = useSheetyData(); | ||
const [page, setPage] = useState<Pages>(Pages.HOME); | ||
|
||
const stickyRef = useRef<HTMLDivElement>(null); | ||
const scrollableContainerRef = useRef<HTMLObjectElement>(null); | ||
|
||
useEffect(() => { | ||
setStickyHeight(stickyRef.current?.clientHeight || 0); | ||
}, [stickyRef.current?.clientHeight, height]); | ||
|
||
return ( | ||
<BasePage title="Home" description="Singapore's first recycling planner"> | ||
<VStack p={0} m={0} height={`${height - NAVBAR_HEIGHT}px`}> | ||
<Container | ||
className={styles.hideScrollbar} | ||
maxW={{ | ||
base: "full", | ||
sm: "container.md", | ||
}} | ||
p={0} | ||
pb={5} | ||
overflow="auto" | ||
height={height - stickyHeight - NAVBAR_HEIGHT} | ||
ref={scrollableContainerRef} | ||
> | ||
<VStack align="initial" mx={25} spacing={30}> | ||
<Banner /> | ||
{isLoaded && ( | ||
<UserInput | ||
scrollableContainerRef={scrollableContainerRef} | ||
setReadyToSubmit={setReadyToSubmit} | ||
/> | ||
)} | ||
</VStack> | ||
</Container> | ||
<StickyFooter ref={stickyRef} disabled={!readyToSubmit} /> | ||
</VStack> | ||
</BasePage> | ||
); | ||
let PageComponent: JSX.Element; | ||
switch (page) { | ||
case Pages.HOME: | ||
PageComponent = <HomePage setPage={setPage} />; | ||
break; | ||
case Pages.HOMEPICKUP: | ||
PageComponent = <HomePickupPage />; | ||
break; | ||
case Pages.INSTRUCTIONS: | ||
PageComponent = <InstructionsPage />; | ||
break; | ||
case Pages.MAP: | ||
PageComponent = <MapPage />; | ||
break; | ||
} | ||
return PageComponent; | ||
}; | ||
|
||
export default Home; |
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,58 @@ | ||
import { Container, VStack } from "@chakra-ui/react"; | ||
import { StickyFooter } from "components/footer/StickyFooter"; | ||
import { Banner, UserInput } from "components/home"; | ||
import styles from "components/home/hideScrollbar.module.css"; | ||
import { NAVBAR_HEIGHT } from "components/nav/NavHeader"; | ||
import { useSheetyData } from "hooks/useRecyclableItemList"; | ||
import { useWindowDimensions } from "hooks/useWindowDimensions"; | ||
import { BasePage } from "layouts/BasePage"; | ||
import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react"; | ||
import { Pages } from "spa-pages/pageEnums"; | ||
|
||
type Props = { | ||
setPage: Dispatch<SetStateAction<Pages>>; | ||
}; | ||
export const HomePage = ({ setPage }: Props) => { | ||
const [stickyHeight, setStickyHeight] = useState<number>(0); | ||
const [readyToSubmit, setReadyToSubmit] = useState(false); | ||
|
||
const { height } = useWindowDimensions(); | ||
const { isLoaded } = useSheetyData(); | ||
|
||
const stickyRef = useRef<HTMLDivElement>(null); | ||
const scrollableContainerRef = useRef<HTMLObjectElement>(null); | ||
|
||
useEffect(() => { | ||
setStickyHeight(stickyRef.current?.clientHeight || 0); | ||
}, [stickyRef.current?.clientHeight, height]); | ||
|
||
return ( | ||
<BasePage title="Home" description="Singapore's first recycling planner"> | ||
<VStack p={0} m={0} height={`${height - NAVBAR_HEIGHT}px`}> | ||
<Container | ||
className={styles.hideScrollbar} | ||
maxW={{ | ||
base: "full", | ||
sm: "container.md", | ||
}} | ||
p={0} | ||
pb={5} | ||
overflow="auto" | ||
height={height - stickyHeight - NAVBAR_HEIGHT} | ||
ref={scrollableContainerRef} | ||
> | ||
<VStack align="initial" mx={25} spacing={30}> | ||
<Banner /> | ||
{isLoaded && ( | ||
<UserInput | ||
scrollableContainerRef={scrollableContainerRef} | ||
setReadyToSubmit={setReadyToSubmit} | ||
/> | ||
)} | ||
</VStack> | ||
</Container> | ||
<StickyFooter ref={stickyRef} disabled={!readyToSubmit} setPage={setPage} /> | ||
</VStack> | ||
</BasePage> | ||
); | ||
}; |
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,12 @@ | ||
import { Flex, Text } from "@chakra-ui/react"; | ||
import { BasePage } from "layouts/BasePage"; | ||
|
||
export const HomePickupPage = () => { | ||
return ( | ||
<BasePage title="Home Pickup" description="Singapore's first recycling planner"> | ||
<Flex direction="column" align="center" my={23}> | ||
<Text>Home Pickup Page</Text> | ||
</Flex> | ||
</BasePage> | ||
); | ||
}; |
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,12 @@ | ||
import { Flex, Text } from "@chakra-ui/react"; | ||
import { BasePage } from "layouts/BasePage"; | ||
|
||
export const InstructionsPage = () => { | ||
return ( | ||
<BasePage title="Instructions" description="Singapore's first recycling planner"> | ||
<Flex direction="column" align="center" my={23}> | ||
<Text>Instructions</Text> | ||
</Flex> | ||
</BasePage> | ||
); | ||
}; |
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,12 @@ | ||
import { Flex, Text } from "@chakra-ui/react"; | ||
import { BasePage } from "layouts/BasePage"; | ||
|
||
export const MapPage = () => { | ||
return ( | ||
<BasePage title="Map" description="Singapore's first recycling planner"> | ||
<Flex direction="column" align="center" my={23}> | ||
<Text>Map Page</Text> | ||
</Flex> | ||
</BasePage> | ||
); | ||
}; |
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,4 @@ | ||
export * from "./components/HomePage"; | ||
export * from "./components/HomePickupPage"; | ||
export * from "./components/InstructionsPage"; | ||
export * from "./components/MapPage"; |
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,6 @@ | ||
export enum Pages { | ||
HOME, | ||
HOMEPICKUP, | ||
INSTRUCTIONS, | ||
MAP, | ||
} |