shared page layout component

This commit is contained in:
Emi Matchu 2020-09-06 18:12:34 -07:00
parent 5a91dd2f2a
commit e33e5fb88f
4 changed files with 92 additions and 80 deletions

View file

@ -8,6 +8,7 @@ import loadable from "@loadable/component";
import { useAuth0 } from "@auth0/auth0-react"; import { useAuth0 } from "@auth0/auth0-react";
import buildApolloClient from "./apolloClient"; import buildApolloClient from "./apolloClient";
import PageLayout from "./PageLayout";
const ItemsPage = loadable(() => import("./ItemsPage")); const ItemsPage = loadable(() => import("./ItemsPage"));
const HomePage = loadable(() => import("./HomePage")); const HomePage = loadable(() => import("./HomePage"));
@ -49,10 +50,14 @@ function App() {
<WardrobePage /> <WardrobePage />
</Route> </Route>
<Route path="/user/:userId/items"> <Route path="/user/:userId/items">
<ItemsPage /> <PageLayout>
<ItemsPage />
</PageLayout>
</Route> </Route>
<Route path="/"> <Route path="/">
<HomePage /> <PageLayout>
<HomePage />
</PageLayout>
</Route> </Route>
</Switch> </Switch>
</ChakraProvider> </ChakraProvider>

View file

@ -5,21 +5,16 @@ import {
Box, Box,
Button, Button,
Flex, Flex,
IconButton,
Input, Input,
useColorMode,
useColorModeValue, useColorModeValue,
useTheme, useTheme,
useToast, useToast,
} from "@chakra-ui/core"; } from "@chakra-ui/core";
import { MoonIcon, SunIcon } from "@chakra-ui/icons"; import { useHistory, useLocation } from "react-router-dom";
import { Link, useHistory, useLocation } from "react-router-dom";
import { useLazyQuery } from "@apollo/client"; import { useLazyQuery } from "@apollo/client";
import { useAuth0 } from "@auth0/auth0-react";
import { Heading1, usePageTitle } from "./util"; import { Heading1, usePageTitle } from "./util";
import OutfitPreview from "./components/OutfitPreview"; import OutfitPreview from "./components/OutfitPreview";
import useCurrentUser from "./components/useCurrentUser";
import HomepageSplashImg from "../images/homepage-splash.png"; import HomepageSplashImg from "../images/homepage-splash.png";
import HomepageSplashImg2x from "../images/homepage-splash@2x.png"; import HomepageSplashImg2x from "../images/homepage-splash@2x.png";
@ -32,13 +27,7 @@ function HomePage() {
const [previewState, setPreviewState] = React.useState(null); const [previewState, setPreviewState] = React.useState(null);
return ( return (
<Flex direction="column" p="6" pt="3" align="center" textAlign="center"> <Flex direction="column" align="center" textAlign="center" marginTop="8">
<Box width="100%" display="flex" alignItems="center">
<ColorModeToggleButton />
<Box flex="1 0 0" />
<UserLoginLogout />
</Box>
<Box height="8" />
<Box <Box
width="200px" width="200px"
height="200px" height="200px"
@ -76,48 +65,6 @@ function HomePage() {
); );
} }
function UserLoginLogout() {
const { isLoading, isAuthenticated, loginWithRedirect, logout } = useAuth0();
const { id, username } = useCurrentUser();
if (isLoading) {
return null;
}
if (isAuthenticated) {
return (
<Box display="flex" alignItems="center">
{username && <Box fontSize="sm">Hi, {username}!</Box>}
{id && (
<Button
as={Link}
to={`/user/${id}/items`}
size="sm"
variant="outline"
marginLeft="2"
>
Items
</Button>
)}
<Button
size="sm"
variant="outline"
onClick={() => logout({ returnTo: window.location.origin })}
marginLeft="2"
>
Log out
</Button>
</Box>
);
} else {
return (
<Button size="sm" variant="outline" onClick={() => loginWithRedirect()}>
Log in
</Button>
);
}
}
function StartOutfitForm({ onChange }) { function StartOutfitForm({ onChange }) {
const history = useHistory(); const history = useHistory();
@ -289,21 +236,6 @@ function SubmitPetForm() {
); );
} }
function ColorModeToggleButton() {
const { colorMode, toggleColorMode } = useColorMode();
return (
<IconButton
aria-label={
colorMode === "light" ? "Switch to dark mode" : "Switch to light mode"
}
icon={colorMode === "light" ? <MoonIcon /> : <SunIcon />}
onClick={toggleColorMode}
variant="ghost"
/>
);
}
/** /**
* useSupportSetup helps our support staff get set up with special access. * useSupportSetup helps our support staff get set up with special access.
* If you provide ?supportSecret=... in the URL, we'll save it in a cookie and * If you provide ?supportSecret=... in the URL, we'll save it in a cookie and

View file

@ -32,23 +32,19 @@ function ItemsPage() {
if (loading) { if (loading) {
return ( return (
<Box padding="6" display="flex" justifyContent="center"> <Box display="flex" justifyContent="center">
<HangerSpinner boxSize="48px" /> <HangerSpinner boxSize="48px" />
</Box> </Box>
); );
} }
if (error) { if (error) {
return ( return <Box color="red.400">{error.message}</Box>;
<Box padding="6" color="red.400">
{error.message}
</Box>
);
} }
return ( return (
<Box padding="6" maxWidth="800px" margin="0 auto"> <Box maxWidth="800px" margin="0 auto">
<Heading1 marginBottom="6"> <Heading1 marginBottom="8">
{isCurrentUser ? "Items you own" : `Items ${data.user.username} owns`} {isCurrentUser ? "Items you own" : `Items ${data.user.username} owns`}
</Heading1> </Heading1>
<Wrap justify="center"> <Wrap justify="center">

79
src/app/PageLayout.js Normal file
View file

@ -0,0 +1,79 @@
import React from "react";
import { Box, Button, IconButton, useColorMode } from "@chakra-ui/core";
import { Link } from "react-router-dom";
import { useAuth0 } from "@auth0/auth0-react";
import { MoonIcon, SunIcon } from "@chakra-ui/icons";
import useCurrentUser from "./components/useCurrentUser";
function PageLayout({ children }) {
return (
<Box padding="6" paddingTop="3">
<Box width="100%" display="flex" alignItems="center" marginBottom="6">
<ColorModeToggleButton />
<Box flex="1 0 0" />
<UserLoginLogout />
</Box>
{children}
</Box>
);
}
function UserLoginLogout() {
const { isLoading, isAuthenticated, loginWithRedirect, logout } = useAuth0();
const { id, username } = useCurrentUser();
if (isLoading) {
return null;
}
if (isAuthenticated) {
return (
<Box display="flex" alignItems="center">
{username && <Box fontSize="sm">Hi, {username}!</Box>}
{id && (
<Button
as={Link}
to={`/user/${id}/items`}
size="sm"
variant="outline"
marginLeft="2"
>
Items
</Button>
)}
<Button
size="sm"
variant="outline"
onClick={() => logout({ returnTo: window.location.origin })}
marginLeft="2"
>
Log out
</Button>
</Box>
);
} else {
return (
<Button size="sm" variant="outline" onClick={() => loginWithRedirect()}>
Log in
</Button>
);
}
}
function ColorModeToggleButton() {
const { colorMode, toggleColorMode } = useColorMode();
return (
<IconButton
aria-label={
colorMode === "light" ? "Switch to dark mode" : "Switch to light mode"
}
icon={colorMode === "light" ? <MoonIcon /> : <SunIcon />}
onClick={toggleColorMode}
variant="ghost"
/>
);
}
export default PageLayout;