You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.0 KiB
JavaScript

import React, {useState} from "react"
5 years ago
import Head from 'next/head'
import PropTypes from "prop-types"
5 years ago
import axios from 'axios'
import {UserContextProvider} from '~/hooks/useUser'
import {CartContextProvider} from '~/hooks/useCart'
import Header from '~/components/header'
import Footer from '~/components/footer'
5 years ago
import AdminNav from '~/components/admin/nav'
5 years ago
import ErrorBoundary from '~/components/errorBoundary'
5 years ago
import "~/styles/layout.css"
import 'react-infinite-calendar/styles.css'
Layout.getInitialProps = async ({Component, ctx}) => {
// Configure axios instance
ctx.axios = axios.create({
headers: (ctx.req && ctx.req.headers && ctx.req.headers.cookie)
? {cookie: ctx.req.headers.cookie}
: undefined
})
ctx.axios.interceptors.response.use(res => res,
err => {
if(!err.isAxiosError || !err.response || !err.response.data || !err.response.data.error)
throw err
// Unwrap API error, rethrow that
throw {
...err.response.data.error,
url: err.request.path
}
}
)
const promises = {
user: ctx.axios.get(`/api/auth`),
cart: ctx.axios.get(`/api/cart`)
}
const {data: user} = await promises.user
const {data: cart} = await promises.cart
let pageProps = {};
if(Component.getInitialProps)
pageProps = await Component.getInitialProps({ctx, user})
return {pageProps, user, cart}
5 years ago
}
function Layout({ Component, pageProps, user, cart }){
const cartState = useState(cart)
const userState = useState(user)
return (
5 years ago
<>
<Head>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
</Head>
<CartContextProvider value={cartState}>
<UserContextProvider value={userState}>
5 years ago
<Header/>
5 years ago
<ErrorBoundary>
<AdminNav>
<Component {...pageProps} />
</AdminNav>
</ErrorBoundary>
5 years ago
<Footer/>
</UserContextProvider>
</CartContextProvider>
</>
)
}
Layout.propTypes = {
pageProps: PropTypes.object
}
export default Layout