Skip to content

Commit

Permalink
feat: adds custom error pages
Browse files Browse the repository at this point in the history
  • Loading branch information
ghoshnirmalya committed Jul 31, 2020
1 parent 950244e commit 112ee99
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 64 deletions.
77 changes: 23 additions & 54 deletions frontend/components/navbar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ChangeEvent } from "react";
import React from "react";
import { NextComponentType } from "next";
import Link from "next/link";
import { signIn, signOut, useSession } from "next-auth/client";
Expand All @@ -7,14 +7,9 @@ import {
Stack,
Link as _Link,
Button,
useColorMode,
Menu,
MenuButton,
Icon,
MenuList,
MenuGroup,
MenuItem,
Switch,
IconButton,
useColorMode,
} from "@chakra-ui/core";

const Navbar: NextComponentType = () => {
Expand All @@ -25,51 +20,9 @@ const Navbar: NextComponentType = () => {
const color = { light: "gray.800", dark: "gray.100" };

const handleToggleTheme = () => {
toggleColorMode();
};
console.log("hello");

const profileDropDown = () => {
if (!session) {
return false;
}

return (
<Box>
<Menu closeOnSelect={false}>
<MenuButton
as={Button}
color={color[colorMode]}
borderColor={borderColor[colorMode]}
>
Profile <Icon name="chevron-down" />
</MenuButton>
<MenuList
color={color[colorMode]}
borderColor={borderColor[colorMode]}
placement="bottom-end"
>
<MenuGroup title="Profile">
<MenuItem>
<Link href="/my-account">
<_Link>My Account</_Link>
</Link>
</MenuItem>
<MenuItem>
<Stack justify="center" align="center" spacing={4} isInline>
<Box>Dark Theme</Box>
<Box>
<Switch
isChecked={colorMode === "dark"}
onChange={handleToggleTheme}
/>
</Box>
</Stack>
</MenuItem>
</MenuGroup>
</MenuList>
</Menu>
</Box>
);
toggleColorMode();
};

const linksForAllUsers = [
Expand All @@ -86,6 +39,11 @@ const Navbar: NextComponentType = () => {
label: "Users",
href: "/users",
},
{
id: "myAccount",
label: "My Account",
href: "/my-account",
},
];

const signInButtonNode = () => {
Expand Down Expand Up @@ -132,6 +90,17 @@ const Navbar: NextComponentType = () => {
);
};

const themeToggleButtonNode = () => {
return (
<IconButton
aria-label="Toggle theme"
fontSize="20px"
icon={colorMode === "dark" ? "sun" : "moon"}
onClick={handleToggleTheme}
/>
);
};

return (
<Box bg={bgColor[colorMode]}>
<Box
Expand All @@ -149,7 +118,7 @@ const Navbar: NextComponentType = () => {
w="full"
>
<Box>
<Stack isInline spacing={4} align="center">
<Stack isInline spacing={4} align="center" fontWeight="semibold">
{linksForAllUsers.map((link) => {
return (
<Box key={link.id}>
Expand All @@ -173,7 +142,7 @@ const Navbar: NextComponentType = () => {
</Box>
<Box>
<Stack isInline spacing={4} align="center">
{profileDropDown()}
{themeToggleButtonNode()}
{signInButtonNode()}
{signOutButtonNode()}
</Stack>
Expand Down
48 changes: 48 additions & 0 deletions frontend/components/pages/error/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { FC } from "react";
import { Box, Heading, Stack, Text, Button, Flex } from "@chakra-ui/core";
import Link from "next/link";

interface IProps {
statusCode: number;
}

const IndexPageComponent: FC<IProps> = ({ statusCode }) => {
const heightOfNavbar: string = "74px";
const containerPadding: string = "1rem";

const signOutButtonNode = () => {
return (
<Box>
<Link href="/">
<Button variantColor="cyan">Return to the home page</Button>
</Link>
</Box>
);
};

return (
<Stack>
<Flex
minH={`calc(100vh - ${heightOfNavbar} - ${containerPadding}*2)`}
justifyContent="center"
alignItems="center"
>
<Stack spacing={4} maxW="xl" mx="auto">
<Heading textAlign="center">Nextjs Hasura Boilerplate</Heading>
<Text fontSize="xl" lineHeight="tall" textAlign="center">
{statusCode
? `An error ${statusCode} occurred on server`
: "An error occurred on client"}
</Text>
<Box>
<Stack isInline align="center" justifyContent="center">
{signOutButtonNode()}
</Stack>
</Box>
</Stack>
</Flex>
</Stack>
);
};

export default IndexPageComponent;
2 changes: 1 addition & 1 deletion frontend/components/pages/index/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const IndexPageComponent = () => {
>
<Stack spacing={4} maxW="xl" mx="auto">
<Heading textAlign="center">Nextjs Hasura Boilerplate</Heading>
<Text fontSize="xl" lineHeight="tall">
<Text fontSize="xl" lineHeight="tall" textAlign="center">
Boilerplate for building applications using Hasura and Next.js. This
demo application has been built using Chakra UI, NextAuth.js and
urql.
Expand Down
4 changes: 4 additions & 0 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
// https://nextjs.org/docs/api-reference/next.config.js/react-strict-mode
reactStrictMode: true,
};
17 changes: 17 additions & 0 deletions frontend/pages/404.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";
import Head from "next/head";
import Page from "components/pages/error";
import { NextPage } from "next";

const Custom404Page: NextPage = () => {
return (
<>
<Head>
<title>Error Page</title>
</Head>
<Page statusCode={404} />
</>
);
};

export default Custom404Page;
27 changes: 27 additions & 0 deletions frontend/pages/_error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from "react";
import Head from "next/head";
import Page from "components/pages/error";
import { NextPage } from "next";

interface iProps {
statusCode: number;
}

const Error: NextPage<iProps> = ({ statusCode }) => {
return (
<>
<Head>
<title>Error Page</title>
</Head>
<Page statusCode={statusCode} />
</>
);
};

Error.getInitialProps = ({ res, err }) => {
const statusCode = res ? res.statusCode : err ? err.statusCode : 404;

return { statusCode };
};

export default Error;
14 changes: 5 additions & 9 deletions frontend/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import NextAuth from "next-auth";
import Providers from "next-auth/providers";
import jwt from "jsonwebtoken";
import { NextApiRequest, NextApiResponse } from "next";

interface iToken {
id: string;
email: string;
name: string;
picture: string;
}
import ISession from "types/session";
import IUser from "types/user";
import iToken from "types/token";

const options = {
providers: [
Expand Down Expand Up @@ -72,12 +68,12 @@ const options = {
},
debug: true,
callbacks: {
session: async (session, user) => {
session: async (session: ISession, user: IUser) => {
session.id = user.id;

return Promise.resolve(session);
},
jwt: async (token, user) => {
jwt: async (token: iToken, user: IUser) => {
const isSignIn = user ? true : false;

if (isSignIn) {
Expand Down
1 change: 1 addition & 0 deletions frontend/types/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export default interface ISession {
email: string;
image: string;
};
id: number;
expires: string;
}
6 changes: 6 additions & 0 deletions frontend/types/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default interface iToken {
id: number;
email: string;
name: string;
picture: string;
}

1 comment on commit 112ee99

@vercel
Copy link

@vercel vercel bot commented on 112ee99 Jul 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.