Add nice UI for posting comments

This commit is contained in:
Michael Fatemi 2021-04-10 17:50:12 -04:00
parent 21cae9b016
commit 68574f0de7

View File

@ -1,10 +1,12 @@
import { useState, useEffect, FormEventHandler, useCallback } from 'react'; import { useState, useEffect, useCallback, useRef } from 'react';
import { useParams } from 'react-router'; import { useParams } from 'react-router';
import Button from '@material-ui/core/Button'; import Button from '@material-ui/core/Button';
import Card from '@material-ui/core/Card'; import Card from '@material-ui/core/Card';
import Textarea from '@material-ui/core/TextareaAutosize'; import Textarea from '@material-ui/core/TextareaAutosize';
import Typography from '@material-ui/core/Typography'; import Typography from '@material-ui/core/Typography';
import Comment from './Comment'; import Comment from './Comment';
import getSessionId from '../lib/getSessionId';
import { makeAPIPostCall } from '../api/utils';
export default function Pool({ registered = false }: { registered?: boolean }) { export default function Pool({ registered = false }: { registered?: boolean }) {
const id = useParams<{ id: string }>().id; const id = useParams<{ id: string }>().id;
@ -32,15 +34,41 @@ export default function Pool({ registered = false }: { registered?: boolean }) {
author_id: 'michael', author_id: 'michael',
type: 'offer', type: 'offer',
}); });
const onComment = useCallback<FormEventHandler<HTMLFormElement>>((e) => {
e.preventDefault();
fetch(`${process.env.REACT_APP_API_ENDPOINT}/pool/comments`) const commentTextareaRef = useRef<HTMLTextAreaElement>(null);
.then((response) => response.json()) const [commentStatus, setCommentStatus] = useState<
.then((data) => { null | 'pending' | 'errored'
console.log(data); >(null);
});
}, []); const onComment = useCallback<React.MouseEventHandler<HTMLButtonElement>>(
(e) => {
e.preventDefault();
if (!commentTextareaRef.current) {
// Wait for ref to be ready
return;
}
if (commentTextareaRef.current.value.length === 0) {
// Noop, no comment to send
return;
}
setCommentStatus('pending');
makeAPIPostCall('/comment', {
body: commentTextareaRef.current!.value,
})
.then(() => {
setCommentStatus(null);
commentTextareaRef.current!.value = '';
})
.catch(() => {
setCommentStatus('errored');
});
},
[]
);
useEffect(() => { useEffect(() => {
fetch(`${process.env.REACT_APP_API_ENDPOINT}/pool/${id}`) fetch(`${process.env.REACT_APP_API_ENDPOINT}/pool/${id}`)
@ -77,10 +105,22 @@ export default function Pool({ registered = false }: { registered?: boolean }) {
<hr /> <hr />
<Textarea <Textarea
cols={80} cols={80}
ref={commentTextareaRef}
placeholder="Post a comment..." placeholder="Post a comment..."
disabled={commentStatus === 'pending'}
style={{ margin: '0.5rem 0rem' }} style={{ margin: '0.5rem 0rem' }}
/> />
<Button variant="contained">Post Comment</Button> <Button
variant="contained"
onClick={onComment}
style={{ margin: '0.5rem 0rem' }}
disabled={commentStatus === 'pending'}
>
Post Comment
</Button>
<Typography variant="subtitle1">
{commentStatus === 'errored' && 'Error posting comment'}
</Typography>
<div style={{ display: 'flex', flexDirection: 'column' }}> <div style={{ display: 'flex', flexDirection: 'column' }}>
{pool.comments.map((comment) => ( {pool.comments.map((comment) => (
<Comment comment={comment} key={comment.id} /> <Comment comment={comment} key={comment.id} />