Comments (4)
I am having the exact same issue with SinglePost.js page. Help me out if you find any solution.
from classsed-graphql-mern-apollo.
`import React, { useContext, useState, useRef } from 'react';
import gql from 'graphql-tag';
import { useQuery, useMutation } from '@apollo/react-hooks';
import moment from 'moment';
import {
Button,
Card,
Form,
Grid,
Image,
Icon,
Label
} from 'semantic-ui-react';
import { AuthContext } from '../context/auth';
import LikeButton from '../components/LikeButton';
import DeleteButton from '../components/DeleteButton';
import MyPopup from '../util/MyPopup';
function SinglePost(props) {
const postId = props.match.params.postId;
const { user } = useContext(AuthContext);
const commentInputRef = useRef(null);
const [comment, setComment] = useState('');
const {
data: { getPost } = {}
} = useQuery(FETCH_POST_QUERY, {
variables: {
postId
}
});
const [submitComment] = useMutation(SUBMIT_COMMENT_MUTATION, {
update() {
setComment('');
commentInputRef.current.blur();
},
variables: {
postId,
body: comment
}
});
function deletePostCallback() {
props.history.push('/');
}
let postMarkup;
if (!getPost) {
postMarkup =
Loading post..
;} else {
const {
id,
body,
createdAt,
username,
comments,
likes,
likeCount,
commentCount
} = getPost;
postMarkup = (
<Grid>
<Grid.Row>
<Grid.Column width={2}>
<Image
src="https://react.semantic-ui.com/images/avatar/large/molly.png"
size="small"
float="right"
/>
</Grid.Column>
<Grid.Column width={10}>
<Card fluid>
<Card.Content>
<Card.Header>{username}</Card.Header>
<Card.Meta>{moment(createdAt).fromNow()}</Card.Meta>
<Card.Description>{body}</Card.Description>
</Card.Content>
<hr />
<Card.Content extra>
<LikeButton user={user} post={{ id, likeCount, likes }} />
<MyPopup content="Comment on post">
<Button
as="div"
labelPosition="right"
onClick={() => console.log('Comment on post')}
>
<Button basic color="blue">
<Icon name="comments" />
</Button>
<Label basic color="blue" pointing="left">
{commentCount}
</Label>
</Button>
</MyPopup>
{user && user.username === username && (
<DeleteButton postId={id} callback={deletePostCallback} />
)}
</Card.Content>
</Card>
{user && (
<Card fluid>
<Card.Content>
<p>Post a comment</p>
<Form>
<div className="ui action input fluid">
<input
type="text"
placeholder="Comment.."
name="comment"
value={comment}
onChange={(event) => setComment(event.target.value)}
ref={commentInputRef}
/>
<button
type="submit"
className="ui button teal"
disabled={comment.trim() === ''}
onClick={submitComment}
>
Submit
</button>
</div>
</Form>
</Card.Content>
</Card>
)}
{comments.map((comment) => (
<Card fluid key={comment.id}>
<Card.Content>
{user && user.username === comment.username && (
<DeleteButton postId={id} commentId={comment.id} />
)}
<Card.Header>{comment.username}</Card.Header>
<Card.Meta>{moment(comment.createdAt).fromNow()}</Card.Meta>
<Card.Description>{comment.body}</Card.Description>
</Card.Content>
</Card>
))}
</Grid.Column>
</Grid.Row>
</Grid>
);
}
return postMarkup;
}
const SUBMIT_COMMENT_MUTATION = gqlmutation($postId: String!, $body: String!) { createComment(postId: $postId, body: $body) { id comments { id body createdAt username } commentCount } }
;
const FETCH_POST_QUERY = gqlquery($postId: ID!) { getPost(postId: $postId) { id body createdAt username likeCount likes { username } commentCount comments { id username createdAt body } } }
;
export default SinglePost;`
from classsed-graphql-mern-apollo.
copy paste the whole code in your SinglePost.js file. It should work. Mine is working perfectly.
from classsed-graphql-mern-apollo.
copy paste the whole code in your SinglePost.js file. It should work. Mine is working perfectly.
I will give your code a quick review and get back to you
from classsed-graphql-mern-apollo.
Related Issues (20)
- Unhandled Rejection (Error): Cannot read property 'req' of undefined
- Uncaught (in promise) TypeError: Cannot read property 'extensions' of undefined
- Server deployment error
- TypeError: Cannot read property 'getPosts' of undefined HOT 3
- "Cannot read property 'unshift' of undefined" HOT 1
- args not defined. HOT 1
- Args not defined HOT 1
- args not defined for react class 6 adding posts HOT 1
- Tab highlight issue HOT 1
- Making what you instructed in the video but still empty getPosts HOT 2
- Getting error on register form "Cannot read property 'username' of undefined. HOT 2
- deleteComment Mutation Bug
- Unable to get Single Post in graphql server
- TypeError: Cannot convert undefined or null to object HOT 3
- Error in Import semantic-ui-css
- Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
- useMutation response changes after reloading HOT 1
- Solving: Cast to ObjectId failed for value "" at path "_id" Error
- Invalid/Expired token
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from classsed-graphql-mern-apollo.