|
|
@@ -1,36 +1,50 @@
|
|
|
+import React, {useState, useContext} from 'react';
|
|
|
+import {useRouter} from 'found';
|
|
|
import {commitMutation, graphql} from 'react-relay';
|
|
|
-import React, {useState} from 'react';
|
|
|
import {Form, Row, Col, Button} from 'react-bootstrap';
|
|
|
|
|
|
-import {authEnvironment} from '../../graphqlEnvironment';
|
|
|
+import {UserContext} from '../../context/User';
|
|
|
+import graphqlEnvironment from '../../graphqlEnvironment';
|
|
|
+
|
|
|
+import {LoginMutationResponse as LoginResponse} from './__generated__/LoginMutation.graphql';
|
|
|
+import {User} from '../../context/User';
|
|
|
|
|
|
const mutation = graphql`
|
|
|
mutation LoginMutation($login: String!, $password: String!) {
|
|
|
userLogin(login: $login, password: $password) {
|
|
|
user {
|
|
|
+ iid
|
|
|
username
|
|
|
+ email
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
`;
|
|
|
|
|
|
-const onSubmit = (login, password): void => {
|
|
|
+const onSubmit = (login, password, onCompleted): void => {
|
|
|
const variables = {
|
|
|
login,
|
|
|
password,
|
|
|
};
|
|
|
|
|
|
- commitMutation(authEnvironment, {
|
|
|
+ commitMutation(graphqlEnvironment, {
|
|
|
mutation,
|
|
|
variables,
|
|
|
- onCompleted: e => console.log(e),
|
|
|
- onError: (err): void => console.error(err),
|
|
|
+ onCompleted: (e: LoginResponse) => onCompleted(e.userLogin.user),
|
|
|
+ onError: (err): void => console.error(err), //FIXME
|
|
|
});
|
|
|
};
|
|
|
|
|
|
const Login: React.FC = () => {
|
|
|
const [login, setLogin] = useState('');
|
|
|
const [password, setPassword] = useState('');
|
|
|
+ const {setUser} = useContext(UserContext);
|
|
|
+ const {router} = useRouter();
|
|
|
+
|
|
|
+ const onLogin = (user: User): void => {
|
|
|
+ setUser(user);
|
|
|
+ router.push('/');
|
|
|
+ };
|
|
|
|
|
|
return (
|
|
|
<Form className="my-4" onSubmit={e => e.preventDefault()}>
|
|
|
@@ -53,7 +67,10 @@ const Login: React.FC = () => {
|
|
|
</Form.Group>
|
|
|
<Form.Group as={Row}>
|
|
|
<Col sm={{span: 10, offset: 2}}>
|
|
|
- <Button onClick={() => onSubmit(login, password)} type="submit">Login</Button>
|
|
|
+ <Button className="mr-2" onClick={() => onSubmit(login, password, onLogin)} type="submit">Login</Button>
|
|
|
+ <Button className="mx-2" href="/auth/discord"><span className="fab fa-discord" /></Button>
|
|
|
+ <Button className="mx-2" href="/auth/google"><span className="fab fa-google" /></Button>
|
|
|
+ <Button className="ml-2" href="/auth/reddit"><span className="fab fa-reddit" /></Button>
|
|
|
</Col>
|
|
|
</Form.Group>
|
|
|
</Form>
|