Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task_2_Done #7

Open
wants to merge 1 commit into
base: Task_1_Done
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/Components/Singin.jsx
Original file line number Diff line number Diff line change
@@ -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 <Redirect to="/" />;
Expand Down
22 changes: 13 additions & 9 deletions src/query/fragments.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
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 {
id
content
timestamp
author {
id
name
username
email
...User
}
}
${user}
`;

const post = gql`
Expand All @@ -24,10 +29,7 @@ const post = gql`
content
timestamp
author {
id
name
username
email
...User
}
comments {
pageInfo {
Expand All @@ -42,10 +44,12 @@ const post = gql`
}
}
}
${user}
${comment}
`;

export default {
user,
post,
comment,
};
30 changes: 19 additions & 11 deletions src/query/user.js
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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(
Expand All @@ -48,13 +58,11 @@ const CREATE_USER_MUTATION = gql`
email: $email
password: $password
}) {
id
name
username
email
...User
}
}
}
${fragments.user}
`;

export default {
Expand Down