From 6a6c5bf0f76fc3d1bdac70f0614bdc6e8ca92ae5 Mon Sep 17 00:00:00 2001 From: Roland Szoke Date: Thu, 19 Sep 2019 13:31:20 +0200 Subject: [PATCH] Task_2_Done --- src/Components/Singin.jsx | 21 +++++++++++++++++---- src/query/fragments.js | 22 +++++++++++++--------- src/query/user.js | 30 +++++++++++++++++++----------- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/Components/Singin.jsx b/src/Components/Singin.jsx index 9e7ade3..1808778 100644 --- a/src/Components/Singin.jsx +++ b/src/Components/Singin.jsx @@ -1,19 +1,32 @@ import React, { useState, useContext } from 'react'; import { Redirect, Link } from 'react-router-dom'; +import { useApolloClient } from '@apollo/react-hooks'; import { Button, Form, Container } from 'react-bootstrap'; import UserContext from '../Context/UserContext'; +import queries from '../query'; const Signin = () => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); - const [isLoading] = useState(false); - const { user } = useContext(UserContext); + const [isLoading, setIsLoading] = useState(false); + const { user, setUser } = useContext(UserContext); + const apolloClient = useApolloClient(); - // TODO: TASK 2. handleSingin const handleSingin = (event) => { event.preventDefault(); - console.log('handleSingin'); + if (email && password && !isLoading) { + setIsLoading(true); + apolloClient.query({ + query: queries.user.SIGNIN_QUERY, + variables: { email, password }, + }).then(({ data: { signin } }) => { + setUser(signin); + }).catch(() => { + alert('Wrong email/password'); + setIsLoading(false); + }); + } }; if (user) return ; diff --git a/src/query/fragments.js b/src/query/fragments.js index 4d29d12..b1a93d9 100644 --- a/src/query/fragments.js +++ b/src/query/fragments.js @@ -1,6 +1,13 @@ import gql from 'graphql-tag'; -// TODO: TASK 2. EXTRA User Fragment +const user = gql` + fragment User on User { + id + name + username + email + } +`; const comment = gql` fragment Comment on Comment { @@ -8,12 +15,10 @@ const comment = gql` content timestamp author { - id - name - username - email + ...User } } + ${user} `; const post = gql` @@ -24,10 +29,7 @@ const post = gql` content timestamp author { - id - name - username - email + ...User } comments { pageInfo { @@ -42,10 +44,12 @@ const post = gql` } } } + ${user} ${comment} `; export default { + user, post, comment, }; diff --git a/src/query/user.js b/src/query/user.js index 7a9bcd0..11e2321 100644 --- a/src/query/user.js +++ b/src/query/user.js @@ -1,5 +1,5 @@ import gql from 'graphql-tag'; -// TODO: TASK 2. EXTRA User Fragment for every query/mutation +import fragments from './fragments'; const GET_USERS_QUERY = gql` query getUsers( @@ -21,18 +21,28 @@ const GET_USERS_QUERY = gql` } edges { node { - id - name - username - email + ...User } } } } + ${fragments.user} `; -// TODO: TASK 2. SIGNIN_QUERY -const SIGNIN_QUERY = ''; +const SIGNIN_QUERY = gql` + query signin( + $email: String! + $password: String! + ) { + signin ( + email: $email + password: $password + ) { + ...User + } + } + ${fragments.user} +`; const CREATE_USER_MUTATION = gql` mutation createUser( @@ -48,13 +58,11 @@ const CREATE_USER_MUTATION = gql` email: $email password: $password }) { - id - name - username - email + ...User } } } + ${fragments.user} `; export default {