summaryrefslogtreecommitdiffstats
path: root/frontend/components/Signin.js
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-03-28 15:22:34 -0400
committerWes Bos <wesbos@gmail.com>2018-03-28 15:22:34 -0400
commit65c524251d49ade9d44a62337f3c1c1fed6eedd0 (patch)
treed504ba9cea8d89bbde4b2e809fddb048bd660548 /frontend/components/Signin.js
parenta1fd7eb33bb08610aaf381a64b7f963cd98bfdaa (diff)
Permissions
Diffstat (limited to 'frontend/components/Signin.js')
-rw-r--r--frontend/components/Signin.js84
1 files changed, 44 insertions, 40 deletions
diff --git a/frontend/components/Signin.js b/frontend/components/Signin.js
index 9f9eb96..846fd0c 100644
--- a/frontend/components/Signin.js
+++ b/frontend/components/Signin.js
@@ -1,5 +1,5 @@
import React, { Component } from 'react';
-import { Mutation } from 'react-apollo';
+import { Mutation, Query } from 'react-apollo';
import { SIGNIN_MUTATION, CURRENT_USER_QUERY } from '../queries';
import Error from './ErrorMessage';
import Form from './styles/Form';
@@ -10,15 +10,19 @@ class Signin extends Component {
password: 'abc123',
};
- loginUser = async (e, signin) => {
+ loginUser = async (e, signin, refetchUser) => {
e.preventDefault();
const res = await signin();
localStorage.setItem('token', res.data.signin.token);
+ await refetchUser();
// TODO refetch current user query
};
- update = (proxy, result) => {
- console.log(proxy, result);
+ update = (proxy, payload) => {
+ console.log(proxy);
+ const data = proxy.readQuery({ query: CURRENT_USER_QUERY });
+ data.me = payload.data.signin.user;
+ proxy.writeQuery({ query: CURRENT_USER_QUERY, data });
};
saveToState = e => {
@@ -27,46 +31,46 @@ class Signin extends Component {
};
render() {
+ // TODO / ASK - do I really have to wrap this in a query just to access the refetch function
return (
- <Mutation
- update={this.update}
- mutation={SIGNIN_MUTATION}
- variables={this.state}
- refetchQueries={[{ query: CURRENT_USER_QUERY }]}
- >
- {(signin, { data, loading, error }) => (
- <Form onSubmit={e => this.loginUser(e, signin)}>
- <Error error={error} />
- <fieldset disabled={loading} aria-busy={loading}>
- <label htmlFor="email">
- Email
- <input
- value={this.state.email}
- onChange={this.saveToState}
- name="email"
- type="text"
- placeholder="email"
- />
- </label>
+ <Query query={CURRENT_USER_QUERY}>
+ {({ refetch }) => (
+ <Mutation mutation={SIGNIN_MUTATION} variables={this.state}>
+ {(signin, { data, loading, error }) => (
+ <Form onSubmit={e => this.loginUser(e, signin, refetch)}>
+ <Error error={error} />
+ <fieldset disabled={loading} aria-busy={loading}>
+ <label htmlFor="email">
+ Email
+ <input
+ value={this.state.email}
+ onChange={this.saveToState}
+ name="email"
+ type="text"
+ placeholder="email"
+ />
+ </label>
- <label htmlFor="password">
- Password
- <input
- type="password"
- name="password"
- id="password"
- className="password"
- placeholder="password"
- value={this.state.password}
- onChange={this.saveToState}
- />
- </label>
+ <label htmlFor="password">
+ Password
+ <input
+ type="password"
+ name="password"
+ id="password"
+ className="password"
+ placeholder="password"
+ value={this.state.password}
+ onChange={this.saveToState}
+ />
+ </label>
- <button type="submit">Sign In!</button>
- </fieldset>
- </Form>
+ <button type="submit">Sign In!</button>
+ </fieldset>
+ </Form>
+ )}
+ </Mutation>
)}
- </Mutation>
+ </Query>
);
}
}