forked from b00tc4mp/isdi-bootcamp-202409
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement comments functions; refactor functions b00tc4mp#168
- Loading branch information
Javier Romera Claros
authored and
Javier Romera Claros
committed
Oct 25, 2024
1 parent
6714853
commit e170da4
Showing
36 changed files
with
587 additions
and
338 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
staff/javier-romera/unsocial/src/components/functional/AddComment.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.Form { | ||
gap: 10px; | ||
|
||
.Field { | ||
width: 90%; | ||
} | ||
|
||
textarea { | ||
width: 100%; | ||
} | ||
|
||
.send-button { | ||
width: 75px; | ||
margin: 0; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
staff/javier-romera/unsocial/src/components/functional/AddComment.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Button, Form, Field } from '../library' | ||
|
||
import logic from '../../logic' | ||
|
||
import './AddComment.css' | ||
|
||
export default ({ postId, onAdded }) => { | ||
const handleSubmit = event => { | ||
event.preventDefault() | ||
|
||
const form = event.target | ||
|
||
const { text: { value: text } } = form | ||
|
||
try { | ||
logic.addComment(postId, text) | ||
|
||
form.reset() | ||
|
||
onAdded() | ||
} catch (error) { | ||
alert(error.message) | ||
console.error(error) | ||
} | ||
} | ||
|
||
return <Form onSubmit={handleSubmit}> | ||
<Field> | ||
<textarea placeholder="Comment" id="text"></textarea> | ||
</Field> | ||
|
||
<Button classname="send-button" type="submit">Send</Button> | ||
</Form> | ||
} |
15 changes: 15 additions & 0 deletions
15
staff/javier-romera/unsocial/src/components/functional/Comment.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.Comment { | ||
p { | ||
padding-right: 0; | ||
} | ||
|
||
.username-delete { | ||
display: flex; | ||
justify-content: space-between; | ||
} | ||
|
||
.remove-comment { | ||
width: 26px; | ||
border: 0; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
staff/javier-romera/unsocial/src/components/functional/Comment.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Button } from '../library' | ||
|
||
import logic from '../../logic' | ||
|
||
import getElapsedTime from "../../utils/getElapsedTime" | ||
|
||
import './Comment.css' | ||
|
||
export default ({ postId, comment: { id, author, text, date }, onRemoved }) => { | ||
|
||
const handleRemove = () => { | ||
if (confirm('Delete comment?')) | ||
try { | ||
logic.removeComment(postId, id) | ||
|
||
onRemoved() | ||
} catch (error) { | ||
alert(error.message) | ||
console.error(error) | ||
} | ||
} | ||
|
||
return <li className="Comment"> | ||
<div className="username-delete"> | ||
<h5>{author.username}</h5> | ||
|
||
{logic.getUserId() === author.id && <Button classname="remove-comment" onClick={handleRemove}>❌</Button>} | ||
</div> | ||
|
||
<p>{text}</p> | ||
|
||
<time>{getElapsedTime(date)}</time> | ||
</li> | ||
} |
5 changes: 0 additions & 5 deletions
5
staff/javier-romera/unsocial/src/components/functional/CommentItem.css
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
staff/javier-romera/unsocial/src/components/functional/CommentItem.jsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,6 @@ | |
margin: 1rem 0; | ||
} | ||
|
||
textarea { | ||
width: 100%; | ||
} | ||
|
||
p, | ||
time { | ||
padding-left: .5rem; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...al/src/components/functional/PostItem.css → ...social/src/components/functional/Post.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.PostItem { | ||
.Post { | ||
.post-header { | ||
display: flex; | ||
justify-content: space-between; | ||
|
85 changes: 85 additions & 0 deletions
85
staff/javier-romera/unsocial/src/components/functional/Post.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { Button } from '../library' | ||
import Comments from './Comments' | ||
|
||
import logic from '../../logic' | ||
|
||
import getElapsedTime from '../../utils/getElapsedTime' | ||
|
||
import { Component } from 'react' | ||
|
||
import './Post.css' | ||
|
||
export default class extends Component { | ||
constructor(props) { | ||
super(props) | ||
|
||
this.state = { view: null } | ||
} | ||
|
||
handleLikeClick = () => { | ||
try { | ||
logic.toggleLikePost(this.props.post.id) | ||
|
||
this.props.onLiked() | ||
} catch (error) { | ||
alert(error.message) | ||
console.error(error) | ||
} | ||
} | ||
|
||
handleDeleteClick = () => { | ||
if (confirm('Are you sure you want to delete this post?')) { | ||
logic.deletePost(this.props.post.id) | ||
|
||
this.props.onDeleted() | ||
} | ||
} | ||
|
||
handleCommentsClick = () => { | ||
this.setState({ view: this.state.view ? null : 'comments' }) | ||
} | ||
|
||
handleViewCommentsShow = () => { | ||
this.setState({ view: 'comments' }) | ||
} | ||
|
||
handleViewCommentsHide = () => { | ||
this.setState({ view: null }) | ||
} | ||
|
||
render() { | ||
const { post: { id, author, image, text, date, liked, likedBy, comments }, onCommentAdded, onCommentRemoved, } = this.props | ||
|
||
return <article className="Post"> | ||
<div className="post-header"> | ||
<h4>{author.username}</h4> | ||
|
||
{logic.getUserId() === author.id && <Button classname="delete-button" type="button" onClick={this.handleDeleteClick}>❌</Button>} | ||
</div> | ||
|
||
<img src={image}></img> | ||
|
||
<div className="likes-div"> | ||
<Button classname="like-button" onClick={this.handleLikeClick}>{`${liked ? '❤️' : '🤍'}`}</Button> | ||
|
||
<span>{likedBy.length}</span> | ||
|
||
<Button classname="comment-button" onClick={this.handleCommentsClick}>💬 </Button> | ||
<span>{comments}</span> | ||
</div> | ||
|
||
<p className="caption">{text}</p> | ||
|
||
{this.state.view === 'comments' && <Comments | ||
postId={id} | ||
onAdded={onCommentAdded} | ||
onRemoved={onCommentRemoved} />} | ||
|
||
{this.state.view === 'comments' && <p onClick={this.handleViewCommentsHide}>Hide comments</p>} | ||
|
||
{this.state.view !== 'comments' && <p onClick={this.handleViewCommentsShow}>View comments...</p>} | ||
|
||
<time>{getElapsedTime(date)} ago</time> | ||
</article > | ||
} | ||
} |
Oops, something went wrong.