forked from iaincollins/nextjs-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.js
267 lines (251 loc) · 10.4 KB
/
layout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import React from 'react'
import Router from 'next/router'
import Head from 'next/head'
import Link from 'next/link'
import { Container, Row, Col, Nav, NavItem, Button, Form, NavLink, Collapse,
Navbar, NavbarToggler, NavbarBrand, Modal, ModalHeader, ModalBody,
ModalFooter, ListGroup, ListGroupItem } from 'reactstrap'
import Signin from './signin'
import { NextAuth } from 'next-auth/client'
import Cookies from 'universal-cookie'
import Package from '../package'
import Styles from '../css/index.scss'
export default class extends React.Component {
static propTypes() {
return {
session: React.PropTypes.object.isRequired,
providers: React.PropTypes.object.isRequired,
children: React.PropTypes.object.isRequired,
fluid: React.PropTypes.boolean,
navmenu: React.PropTypes.boolean,
signinBtn: React.PropTypes.boolean
}
}
constructor(props) {
super(props)
this.state = {
navOpen: false,
modal: false,
providers: null
}
this.toggleModal = this.toggleModal.bind(this)
}
async toggleModal(e) {
if (e) e.preventDefault()
// Save current URL so user is redirected back here after signing in
if (this.state.modal !== true) {
const cookies = new Cookies()
cookies.set('redirect_url', window.location.pathname, { path: '/' })
}
this.setState({
providers: this.state.providers || await NextAuth.providers(),
modal: !this.state.modal
})
}
render() {
return (
<React.Fragment>
<Head>
<meta charSet="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>{this.props.title || 'Next.js Starter Project'}</title>
<style dangerouslySetInnerHTML={{__html: Styles}}/>
<script src="https://cdn.polyfill.io/v2/polyfill.min.js"/>
</Head>
<Navbar light className="navbar navbar-expand-md pt-3 pb-3">
<Link prefetch href="/">
<NavbarBrand href="/">
<span className="icon ion-md-home mr-1"></span> {Package.name}
</NavbarBrand>
</Link>
<input className="nojs-navbar-check" id="nojs-navbar-check" type="checkbox" aria-label="Menu"/>
<label tabIndex="1" htmlFor="nojs-navbar-check" className="nojs-navbar-label mt-2" />
<div className="nojs-navbar">
<Nav navbar>
<div tabIndex="1" className="dropdown nojs-dropdown">
<div className="nav-item">
<span className="dropdown-toggle nav-link">Examples</span>
</div>
<div className="dropdown-menu">
<Link prefetch href="/examples/authentication">
<a href="/examples/authentication" className="dropdown-item">Auth</a>
</Link>
<Link prefetch href="/examples/async">
<a href="/examples/async" className="dropdown-item">Async Data</a>
</Link>
<Link prefetch href="/examples/layout">
<a href="/examples/layout" className="dropdown-item">Layout</a>
</Link>
<Link prefetch href="/examples/routing">
<a href="/examples/routing" className="dropdown-item">Routing</a>
</Link>
<Link prefetch href="/examples/styling">
<a href="/examples/styling" className="dropdown-item">Styling</a>
</Link>
</div>
</div>
</Nav>
<UserMenu session={this.props.session} toggleModal={this.toggleModal} signinBtn={this.props.signinBtn}/>
</div>
</Navbar>
<MainBody navmenu={this.props.navmenu} fluid={this.props.fluid} container={this.props.container}>
{this.props.children}
</MainBody>
<Container fluid={this.props.fluid}>
<hr className="mt-3"/>
<p className="text-muted small">
<Link href="https://github.com/iaincollins/nextjs-starter"><a className="text-muted font-weight-bold"><span className="icon ion-logo-github"/> {Package.name} {Package.version}</a></Link>
<span> built with </span>
<Link href="https://github.com/zeit/next.js"><a className="text-muted font-weight-bold">Next.js {Package.dependencies.next.replace('^', '')}</a></Link>
<span> & </span>
<Link href="https://github.com/facebook/react"><a className="text-muted font-weight-bold">React {Package.dependencies.react.replace('^', '')}</a></Link>
.
<span className="ml-2">© {new Date().getYear() + 1900}.</span>
</p>
</Container>
<SigninModal modal={this.state.modal} toggleModal={this.toggleModal} session={this.props.session} providers={this.state.providers}/>
</React.Fragment>
)
}
}
export class MainBody extends React.Component {
render() {
if (this.props.container === false) {
return (
<React.Fragment>
{this.props.children}
</React.Fragment>
)
} else if (this.props.navmenu === false) {
return (
<Container fluid={this.props.fluid} style={{marginTop: '1em'}}>
{this.props.children}
</Container>
)
} else {
return (
<Container fluid={this.props.fluid} style={{marginTop: '1em'}}>
<Row>
<Col xs="12" md="9" lg="10">
{this.props.children}
</Col>
<Col xs="12" md="3" lg="2" style={{paddingTop: '1em'}}>
<h5 className="text-muted text-uppercase">Examples</h5>
<ListGroup>
<ListGroupItem>
<Link prefetch href="/examples/authentication"><a href="/examples/authentication" className="d-block">Auth</a></Link>
</ListGroupItem>
<ListGroupItem>
<Link prefetch href="/examples/async"><a href="/examples/async" className="d-block">Async</a></Link>
</ListGroupItem>
<ListGroupItem>
<Link prefetch href="/examples/layout"><a href="/examples/layout" className="d-block">Layout</a></Link>
</ListGroupItem>
<ListGroupItem>
<Link prefetch href="/examples/routing"><a href="/examples/routing" className="d-block">Routing</a></Link>
</ListGroupItem>
<ListGroupItem>
<Link prefetch href="/examples/styling"><a href="/examples/styling" className="d-block">Styling</a></Link>
</ListGroupItem>
</ListGroup>
</Col>
</Row>
</Container>
)
}
}
}
export class UserMenu extends React.Component {
constructor(props) {
super(props)
this.handleSignoutSubmit = this.handleSignoutSubmit.bind(this)
}
async handleSignoutSubmit(event) {
event.preventDefault()
// Save current URL so user is redirected back here after signing out
const cookies = new Cookies()
cookies.set('redirect_url', window.location.pathname, { path: '/' })
await NextAuth.signout()
Router.push('/')
}
render() {
if (this.props.session && this.props.session.user) {
// If signed in display user dropdown menu
const session = this.props.session
return (
<Nav className="ml-auto" navbar>
{/*<!-- Uses .nojs-dropdown CSS to for a dropdown that works without client side JavaScript ->*/}
<div tabIndex="2" className="dropdown nojs-dropdown">
<div className="nav-item">
<span className="dropdown-toggle nav-link d-none d-md-block">
<span className="icon ion-md-contact" style={{fontSize: '2em', position: 'absolute', top: -5, left: -25}}></span>
</span>
<span className="dropdown-toggle nav-link d-block d-md-none">
<span className="icon ion-md-contact mr-2"></span>
{session.user.name || session.user.email}
</span>
</div>
<div className="dropdown-menu">
<Link prefetch href="/account">
<a href="/account" className="dropdown-item"><span className="icon ion-md-person mr-1"></span> Your Account</a>
</Link>
<AdminMenuItem {...this.props}/>
<div className="dropdown-divider d-none d-md-block"/>
<div className="dropdown-item p-0">
<Form id="signout" method="post" action="/auth/signout" onSubmit={this.handleSignoutSubmit}>
<input name="_csrf" type="hidden" value={this.props.session.csrfToken}/>
<Button type="submit" block className="pl-4 rounded-0 text-left dropdown-item"><span className="icon ion-md-log-out mr-1"></span> Sign out</Button>
</Form>
</div>
</div>
</div>
</Nav>
)
} if (this.props.signinBtn === false) {
// If not signed in, don't display sign in button if disabled
return null
} else {
// If not signed in, display sign in button
return (
<Nav className="ml-auto" navbar>
<NavItem>
{/**
* @TODO Add support for passing current URL path as redirect URL
* so that users without JavaScript are also redirected to the page
* they were on before they signed in.
**/}
<a href="/auth?redirect=/" className="btn btn-outline-primary" onClick={this.props.toggleModal}><span className="icon ion-md-log-in mr-1"></span> Sign up / Sign in</a>
</NavItem>
</Nav>
)
}
}
}
export class AdminMenuItem extends React.Component {
render() {
if (this.props.session.user && this.props.session.user.admin === true) {
return (
<React.Fragment>
<Link prefetch href="/admin">
<a href="/admin" className="dropdown-item"><span className="icon ion-md-settings mr-1"></span> Admin</a>
</Link>
</React.Fragment>
)
} else {
return(<div/>)
}
}
}
export class SigninModal extends React.Component {
render() {
if (this.props.providers === null) return null
return (
<Modal isOpen={this.props.modal} toggle={this.props.toggleModal} style={{maxWidth: 700}}>
<ModalHeader>Sign up / Sign in</ModalHeader>
<ModalBody style={{padding: '1em 2em'}}>
<Signin session={this.props.session} providers={this.props.providers}/>
</ModalBody>
</Modal>
)
}
}