Skip to content

Commit ec5a56b

Browse files
committed
Task_3_Done_Hook
1 parent 742738f commit ec5a56b

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/Components/Post.jsx

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState } from 'react';
2-
import { Query } from '@apollo/react-components';
2+
import { useQuery } from '@apollo/react-hooks';
33
import {
44
Button, Container, Card, Row, Jumbotron, Spinner, Form,
55
} from 'react-bootstrap';
@@ -13,6 +13,17 @@ const Post = ({ postId }) => {
1313
const [commentContent, setCommentContent] = useState('');
1414
const [commentCreateIsLoading] = useState(false);
1515

16+
const {
17+
loading: postIsLoading,
18+
data: postsData,
19+
} = useQuery(queries.post.GET_POSTS_QUERY, {
20+
variables: {
21+
filters: [{ field: 'id', operation: 'eq', value: postId }],
22+
limit: 1,
23+
},
24+
});
25+
const post = get(postsData, 'posts.edges[0].node', {});
26+
1627
// TODO: TASK 4. query comments
1728
const commentIsLoading = false;
1829
const comments = [];
@@ -26,25 +37,20 @@ const Post = ({ postId }) => {
2637

2738
return (
2839
<Container className="post">
29-
<Query query={queries.post.GET_POSTS_QUERY} variables={{ filters: [{ field: 'id', operation: 'eq', value: postId }], limit: 1 }}>
30-
{({ loading: postIsLoading, data: postsData }) => {
31-
const post = get(postsData, 'posts.edges[0].node', {});
32-
return (postIsLoading
33-
? <div className="post-loader"><Spinner animation="border" /></div>
34-
: (
35-
<Jumbotron>
36-
<h1>{post.title}</h1>
37-
<footer className="blockquote-footer">
38-
{get(post, 'author.username')}
39-
<div>{DateTime.fromISO(post.timestamp).toFormat('HH:mm - dd/LL/yyyy')}</div>
40-
</footer>
41-
<br />
42-
<p>{post.description}</p>
43-
<p>{post.content}</p>
44-
</Jumbotron>
45-
));
46-
}}
47-
</Query>
40+
{postIsLoading
41+
? <div className="post-loader"><Spinner animation="border" /></div>
42+
: (
43+
<Jumbotron>
44+
<h1>{post.title}</h1>
45+
<footer className="blockquote-footer">
46+
{get(post, 'author.username')}
47+
<div>{DateTime.fromISO(post.timestamp).toFormat('HH:mm - dd/LL/yyyy')}</div>
48+
</footer>
49+
<br />
50+
<p>{post.description}</p>
51+
<p>{post.content}</p>
52+
</Jumbotron>
53+
)}
4854
<div className="post-commentForm">
4955
<h4>Write a comment</h4>
5056
<Form onSubmit={handleCreateComment}>

0 commit comments

Comments
 (0)