-
Notifications
You must be signed in to change notification settings - Fork 0
/
Post.jsx
103 lines (88 loc) · 2.87 KB
/
Post.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { format, formatDistanceToNow } from 'date-fns';
import ptBR from 'date-fns/locale/pt-BR';
import { useState } from 'react';
import { Avatar } from './Avatar';
import { Comment } from './Comment';
import styles from './Post.module.css';
export function Post({ author, publishedAt, content }) {
const [comments, setComments] = useState([
'Muito bom! 👏'
]);
const [newCommentText, setNewCommentText] = useState('');
const publishedDateFormated = format(publishedAt, "d 'de' LLLL 'às' HH:mm'h'", {
locale: ptBR,
});
const publishedDateRelativeToNow = formatDistanceToNow(publishedAt, {
locale: ptBR,
addSuffix: true
});
function handleCreateNewComment() {
event.preventDefault();
setComments([...comments, newCommentText]);
setNewCommentText('');
};
function handleNewCommentChange() {
event.target.setCustomValidity('');
setNewCommentText(event.target.value);
};
function handleNewCommentInvalid() {
event.target.setCustomValidity('Esse campo é obrigatório!!!');
}
function deleteComment(commentToDelete) {
const commentsWithoutDeletedOne = comments.filter(comment => {
return comment !== commentToDelete;
})
setComments(commentsWithoutDeletedOne);
}
const isNewCommentEmpty = newCommentText.length === 0;
return (
<article className={styles.post}>
<header>
<div className={styles.author}>
<Avatar src={author.avatarUrl} />
<div className={styles.authorInfo}>
<strong>{author.name}</strong>
<span>{author.role}</span>
</div>
</div>
<time title={publishedDateFormated} dateTime={publishedAt.toISOString()}>
{publishedDateRelativeToNow}
</time>
</header>
<div className={styles.content}>
{content.map(line => {
if (line.type === 'paragraph') {
return <p key={line.content}>{line.content}</p>;
} else if (line.type === 'link') {
return <p key={line.content}><a href="#">{line.content}</a></p>
}
})}
</div>
<form onSubmit={handleCreateNewComment} className={styles.commentForm}>
<strong>Deixe seu feedback</strong>
<textarea
name = "comment"
placeholder="Deixe um comentário"
value={newCommentText}
onChange={handleNewCommentChange}
onInvalid={handleNewCommentInvalid}
required
/>
<footer>
<button type="submit" disabled={isNewCommentEmpty}>Publicar</button>
</footer>
</form>
<div className={styles.commentList}>
{comments.map(comment => {
return (
<Comment
key={content}
content={comment}
onDeleteComment={deleteComment}
/>
);
})}
</div>
</article>
);
}