Git Product home page Git Product logo

Comments (4)

aamirnaved26 avatar aamirnaved26 commented on July 20, 2024 1

I am having the exact same issue with SinglePost.js page. Help me out if you find any solution.

from classsed-graphql-mern-apollo.

aamirnaved26 avatar aamirnaved26 commented on July 20, 2024

`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.

aamirnaved26 avatar aamirnaved26 commented on July 20, 2024

copy paste the whole code in your SinglePost.js file. It should work. Mine is working perfectly.

from classsed-graphql-mern-apollo.

bradleymubenga777 avatar bradleymubenga777 commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.