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.
add comments and delete them WIP b00tc4mp#167
- Loading branch information
1 parent
c515c89
commit 2984bad
Showing
43 changed files
with
716 additions
and
321 deletions.
There are no files selected for viewing
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
36 changes: 36 additions & 0 deletions
36
staff/anna-gonzalez/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,36 @@ | ||
import { Label, Button, Form, Field } from '../library' | ||
|
||
import logic from '../../logic' | ||
|
||
export default ({ postId, onAdded }) => { | ||
console.log('AddComment -> render') | ||
|
||
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> | ||
<Label htmlFor="text">New comment</Label> | ||
<textarea id="text"></textarea> | ||
</Field> | ||
|
||
<Button type="submit">Send</Button> | ||
</Form> | ||
} |
32 changes: 32 additions & 0 deletions
32
staff/anna-gonzalez/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,32 @@ | ||
import { Button } from '../library' | ||
|
||
import logic from '../../logic' | ||
|
||
import getElapsedTime from '../../utils/getElapsedTime' | ||
|
||
export default ({ postId, comment: { id, author, text, date }, onRemoved }) => { | ||
console.log('Comment -> render') | ||
|
||
const handleRemove = () => { | ||
if (confirm('Delete comment?')) | ||
try { | ||
logic.removeComment(postId, id) | ||
|
||
onRemoved() | ||
} catch (error) { | ||
alert(error.message) | ||
|
||
console.error(error) | ||
} | ||
} | ||
|
||
return <li> | ||
<h4>{author.username}</h4> | ||
|
||
<p>{text}</p> | ||
|
||
<time>{getElapsedTime(date)}</time> | ||
|
||
{logic.getUserId() === author.id && <Button className="no-style-button" onClick={handleRemove}>❌</Button>} | ||
</li> | ||
} |
105 changes: 75 additions & 30 deletions
105
staff/anna-gonzalez/unsocial/src/components/functional/Comments.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 |
---|---|---|
@@ -1,30 +1,75 @@ | ||
export default () => <section> | ||
<ul> | ||
<li> | ||
<h4>username</h4> | ||
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Numquam in maxime id, expedita quaerat, corporis dolorum earum vero culpa omnis officia ducimus ipsum ipsam sequi praesentium dolores dolor. Ipsam, possimus.</p> | ||
<time>4 days</time> | ||
</li> | ||
|
||
<li> | ||
<h4>username</h4> | ||
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Numquam in maxime id, expedita quaerat, corporis dolorum earum vero culpa omnis officia ducimus ipsum ipsam sequi praesentium dolores dolor. Ipsam, possimus.</p> | ||
<time>4 days</time> | ||
</li> | ||
|
||
<li> | ||
<h4>username</h4> | ||
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Numquam in maxime id, expedita quaerat, corporis dolorum earum vero culpa omnis officia ducimus ipsum ipsam sequi praesentium dolores dolor. Ipsam, possimus.</p> | ||
<time>4 days</time> | ||
</li> | ||
|
||
<form onSubmit={event => { | ||
event.preventDefault() | ||
}}> | ||
<label htmlFor="text">New comment</label> | ||
<textarea id="text"></textarea> | ||
|
||
<button type="submit">Send</button> | ||
</form> | ||
</ul> | ||
</section> | ||
import { Component } from 'react' | ||
|
||
import Comment from './Comment' | ||
import AddComment from './AddComment' | ||
|
||
import logic from '../../logic' | ||
|
||
export default class extends Component { | ||
constructor(props) { | ||
console.log('Comments -> constructor') | ||
|
||
super(props) | ||
|
||
let comments | ||
|
||
try { | ||
comments = logic.getComments(props.postId) | ||
} catch (error) { | ||
alert(error.message) | ||
|
||
console.error(error) | ||
} | ||
|
||
this.state = { comments } | ||
} | ||
|
||
onAdded = () => { | ||
try { | ||
const comments = logic.getComments(this.props.postId) | ||
|
||
this.setState({ comments }) | ||
|
||
this.props.onAdded() | ||
} catch (error) { | ||
alert(error.message) | ||
|
||
console.error(error) | ||
} | ||
} | ||
|
||
onRemoved = () => { | ||
try { | ||
const comments = logic.getComments(this.props.postId) | ||
|
||
this.setState({ comments }) | ||
|
||
this.props.onRemoved() | ||
} catch (error) { | ||
alert(error.message) | ||
|
||
console.error(error) | ||
} | ||
} | ||
|
||
render() { | ||
console.log('Comments -> render') | ||
|
||
return <section> | ||
<ul> | ||
{this.state.comments.map(comment => | ||
<Comment | ||
postId={this.props.postId} | ||
comment={comment} | ||
onRemoved={this.onRemoved} | ||
/>) | ||
} | ||
</ul> | ||
|
||
<AddComment | ||
postId={this.props.postId} | ||
onAdded={this.onAdded} | ||
/> | ||
</section> | ||
} | ||
} |
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
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
Oops, something went wrong.