0.0.5 ā€¢ Published 3 years ago

@commont/react v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@commont/react

npm

Getting set up

To use Commont, you need to create a new account via our signup page. You can sign up using an email and password or by using GitHub or Google. Once you have an account, you can access the Commont dashboard. Initially, you'll see one default project that you can configure as you need.

šŸ‘€ Read the docs for more information.

Installing @commont/react

yarn add @commont/react commont # npm install @commont/react

The package exports a useComments hook that you can use to fetch the comments for your project.

Using useComments hook

useComments fetches comments from the backend on mount and whenever take or skip change.

Parameters

useComments takes an object with the following parameters:

  • projectId ā€” Your project ID.
  • topic ā€” Comments will be fetched for a particular topic, e.g. my-post-about-cats.
  • take ā€” Number of comments to fetch.
  • skip ā€” Number of comments to skip (offset).

Example usage in a React component

import { useComments, CommentStatus } from '@commont/react';

const Post = ({ projectId }) => {
  const { comments, count, loading, refetch, error } = useComments({
    projectId,
    topic: 'post-id'
    take: 10, skip: 0
  });

  return (
    <section>
      <h3>{count} comments</h3>
      {loading ? (
        <p>Loading...</p>
      ) : (
        <div>
          {comments.map(({ author, content, createdAt, status }) => (
            <article key={createdAt} className="bg-gray-100 rounded my-6 p-4">
              <div className="font-bold mb-2">
                {author} 惻 {new Date(createdAt).toLocaleDateString()}
              </div>
              <p className="text-gray-700">{content}</p>
            </article>
          ))}
        </div>
      )}
    </section>
  )
}

Examples

  • Demo with Theme UI
  • Demo with Tailwind
  • Demo with Theme UI ā€” an example with pagination

API Reference

UseCommentsComment

interface UseCommentsComment {
  topic: string;
  author: string;
  content: string;
  createdAt: string;
  status?: UseCommentsStatus;
  details?: Record<string, any>;
}

UseCommentsStatus

When a user adds a new comment, it will be in one of four states:

  • sending ā€” add comment request is still pending.
  • added ā€” the comment was successfully added and is visible for other people.
  • delivered-awaiting-approval ā€” the comment was successfully added, but it's not yet visible for other people.
  • failed ā€” adding a comment was unsuccessful.
type UseCommentsStatus =
  | 'sending'
  | 'added'
  | 'delivered-awaiting-approval'
  | 'failed';

UseCommentsParameters

interface UseCommentsParameters {
  projectId: string;
  topic: string;
  take?: number;
  skip?: number;
}

UseCommentsResult

interface UseComentsResult {
  comments: UseCommentsComment[];
  addComment: ({
    content,
    author,
  }: Pick<UseCommentsComment, 'content' | 'author' | 'details'>) => void;
  refetch: () => void;
  count: number;
  loading: boolean;
  error: string | null;
}
0.0.5

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.4

3 years ago

0.0.1

3 years ago