|
@@ -1,88 +1,72 @@
|
|
|
import { Query, Mutation } from 'react-apollo'
|
|
|
-import Link from 'next/link'
|
|
|
-import { Formik, Form, Field, ErrorMessage } from 'formik'
|
|
|
-import { email } from '../lib/regex'
|
|
|
import { adopt } from 'react-adopt'
|
|
|
+import Link from 'next/link'
|
|
|
|
|
|
-import { USER_LOGIN, CURRENT_USER } from '../lib/graphql'
|
|
|
-
|
|
|
-const UserLoginForm = props => <p>Login Form</p>
|
|
|
+import { CURRENT_USER, USER_LOGOUT } from '../lib/graphql'
|
|
|
+import LoginForm from './login'
|
|
|
|
|
|
-const LoginAdoption = adopt({
|
|
|
- mutation: ({ render, formik }) => (
|
|
|
- <Mutation
|
|
|
- mutation={USER_LOGIN}
|
|
|
- refetchQueries={[{ query: CURRENT_USER }]}
|
|
|
- >{render}
|
|
|
- </Mutation>
|
|
|
- ),
|
|
|
- formik: ({ render, mutation }) => (
|
|
|
- <Formik
|
|
|
- initialValues={{ email: '', password: '' }}
|
|
|
- validate={values => {
|
|
|
- const errors = {}
|
|
|
- if (!values.email) errors.email = 'Required'
|
|
|
- else if (!email.test(values.email)) errors.email = 'Invalid email address'
|
|
|
- return errors
|
|
|
- }}
|
|
|
- onSubmit={(values, { setSubmitting }) => {
|
|
|
- mutation({ variables: values })
|
|
|
- }}
|
|
|
- >
|
|
|
- {render}
|
|
|
- </Formik>
|
|
|
- )
|
|
|
+const UserAdoption = adopt({
|
|
|
+ user: ({ render }) => <Query query={CURRENT_USER}>{render}</Query>,
|
|
|
+ logout: ({ render }) => <Mutation mutation={USER_LOGOUT} refetchQueries={[{ query: CURRENT_USER }]}>{render}</Mutation>
|
|
|
})
|
|
|
|
|
|
-const LoginForm = props => (
|
|
|
- <LoginAdoption>
|
|
|
- {({ formik, mutation }) => {
|
|
|
- return (
|
|
|
- <Form>
|
|
|
- <Field type='email' name='email' />
|
|
|
- <ErrorMessage name='email' component='span' />
|
|
|
- <Field type='password' name='password' />
|
|
|
- <ErrorMessage name='password' component='span' />
|
|
|
- <button type='submit' disabled={formik.isSubmitting}>Login</button>
|
|
|
- </Form>
|
|
|
- )
|
|
|
- }}
|
|
|
- </LoginAdoption>
|
|
|
-)
|
|
|
+class UserNav extends React.Component {
|
|
|
+ state = {
|
|
|
+ menu: false
|
|
|
+ }
|
|
|
|
|
|
-const UserNav = props => (
|
|
|
- <Query query={CURRENT_USER}>
|
|
|
- {
|
|
|
- ({ data, error, loading }) => {
|
|
|
- if (error) {
|
|
|
- return <LoginForm />
|
|
|
- }
|
|
|
- if (loading) return (<p>Loading...</p>)
|
|
|
- console.log(data)
|
|
|
- return (
|
|
|
- <Link href={{ pathname: 'user', query: { id: 12 } }}>
|
|
|
- <a>test {props.query}</a>
|
|
|
- </Link>
|
|
|
- )
|
|
|
- }
|
|
|
- }
|
|
|
- </Query>
|
|
|
-)
|
|
|
+ toggleMenu = ev => {
|
|
|
+ ev.preventDefault()
|
|
|
+ this.setState({ menu: !this.state.menu })
|
|
|
+ }
|
|
|
|
|
|
-const SignupForm = props => (
|
|
|
- <Formik
|
|
|
- initialValues={{ email: '', name: '', password: '', passwordAgain: '' }}
|
|
|
- onSubmit={values => {
|
|
|
- console.log('submitted', values)
|
|
|
- }}
|
|
|
- >
|
|
|
- {
|
|
|
- ({ isSubmitting }) => (
|
|
|
- <Form />
|
|
|
- )
|
|
|
+ logout = async (ev, logout) => {
|
|
|
+ ev.preventDefault()
|
|
|
+ try {
|
|
|
+ const id = await logout()
|
|
|
+ } catch (error) {
|
|
|
+ console.log(error)
|
|
|
}
|
|
|
- </Formik>
|
|
|
-)
|
|
|
+ }
|
|
|
+
|
|
|
+ render() {
|
|
|
+ return (
|
|
|
+ <UserAdoption>{({ user, logout }) => {
|
|
|
+ console.log(user, logout)
|
|
|
+ if (user.error) return <LoginForm />
|
|
|
+ if (user.loading) return <p>Loading...</p>
|
|
|
+ const { name, email, id } = user.data.me
|
|
|
+ return (
|
|
|
+ <>
|
|
|
+ <a href='' onClick={this.toggleMenu}>{name}</a>
|
|
|
+ {
|
|
|
+ this.state.menu ? (
|
|
|
+ <section className='usermenu'>
|
|
|
+ <h2>Welcome, {name}</h2>
|
|
|
+ <Link href={{ path: 'user' }}><a>Edit user data</a></Link>
|
|
|
+ <a href='' onClick={ev => {
|
|
|
+ ev.preventDefault()
|
|
|
+ this.logout(ev, logout)
|
|
|
+ }}>Logout</a>
|
|
|
+ </section>
|
|
|
+ ) : null
|
|
|
+ }
|
|
|
+
|
|
|
+ <style jsx>
|
|
|
+ {`
|
|
|
+section.usermenu {
|
|
|
+ position: absolute;
|
|
|
+ background: rgba(127,0,0,0.5);
|
|
|
+}
|
|
|
+ `}
|
|
|
+ </style>
|
|
|
+ </>
|
|
|
+ )
|
|
|
+ }}
|
|
|
+ </UserAdoption>
|
|
|
+ )
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
const User = props => <a />
|
|
|
|