mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-21 19:29:51 -04:00
Add nice UI for posting comments
This commit is contained in:
parent
21cae9b016
commit
68574f0de7
|
@ -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) => {
|
|
||||||
|
const commentTextareaRef = useRef<HTMLTextAreaElement>(null);
|
||||||
|
const [commentStatus, setCommentStatus] = useState<
|
||||||
|
null | 'pending' | 'errored'
|
||||||
|
>(null);
|
||||||
|
|
||||||
|
const onComment = useCallback<React.MouseEventHandler<HTMLButtonElement>>(
|
||||||
|
(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
fetch(`${process.env.REACT_APP_API_ENDPOINT}/pool/comments`)
|
if (!commentTextareaRef.current) {
|
||||||
.then((response) => response.json())
|
// Wait for ref to be ready
|
||||||
.then((data) => {
|
return;
|
||||||
console.log(data);
|
}
|
||||||
|
|
||||||
|
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} />
|
||||||
|
|
Loading…
Reference in New Issue
Block a user