-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
690 additions
and
4 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
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,61 @@ | ||
import { | ||
CREATE_COMMENTS, | ||
CREATE_COMMENTS_ERRORS, | ||
FETCH_COMMENTS, | ||
FETCH_COMMENTS_ERRORS | ||
} from "./types"; | ||
import axios from "axios"; | ||
import { toast } from "react-toastify"; | ||
|
||
export const articleComments = (data, slug) => async dispatch => { | ||
return await axios | ||
.post( | ||
`https://ah-backend-prime-staging.herokuapp.com/api/v1/articles/${slug}/comments/0/`, | ||
data, | ||
{ | ||
headers: { | ||
Authorization: "Bearer " + sessionStorage.token | ||
} | ||
} | ||
) | ||
.then(res => { | ||
dispatch({ | ||
type: CREATE_COMMENTS, | ||
payload: res.data | ||
}); | ||
toast.dismiss(); | ||
toast.success("thanks for your view", { | ||
hideProgressBar: false, | ||
autoClose: 3000 | ||
}); | ||
}) | ||
.catch(errors => { | ||
dispatch({ | ||
type: CREATE_COMMENTS_ERRORS, | ||
payload: errors.response.data | ||
}); | ||
}); | ||
}; | ||
export const fetchComments = slug => async dispatch => { | ||
return await axios | ||
.get( | ||
` https://ah-backend-prime-staging.herokuapp.com/api/v1/articles/${slug}/comments/0/`, | ||
{ | ||
headers: { | ||
Authorization: "Bearer " + sessionStorage.token | ||
} | ||
} | ||
) | ||
.then(res => { | ||
dispatch({ | ||
type: FETCH_COMMENTS, | ||
payload: res.data.message | ||
}); | ||
}) | ||
.catch(error => { | ||
dispatch({ | ||
type: FETCH_COMMENTS_ERRORS, | ||
payload: { errors: "error fetching your comments" } | ||
}); | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import React, { Component } from "react"; | ||
import moment from "moment"; | ||
import { articleComments, fetchComments } from "../actions/commentsActions"; | ||
import { connect } from "react-redux"; | ||
import "../styles/comments.scss"; | ||
|
||
|
||
export class Comments extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
comments: "" | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
const { fetchComments, articleSlug } = this.props; | ||
fetchComments(articleSlug); | ||
} | ||
|
||
handleOnChange = event => { | ||
event.preventDefault(); | ||
const { name, value } = event.target; | ||
this.setState({ [name]: value }); | ||
}; | ||
|
||
handleOnSubmit = event => { | ||
event.preventDefault(); | ||
const { articleSlug, articleComments, fetchComments } = this.props; | ||
const { comments } = this.state; | ||
const data = { | ||
body: comments | ||
}; | ||
articleComments(data, articleSlug); | ||
fetchComments(articleSlug); | ||
}; | ||
|
||
render() { | ||
const { commentsMessage } = this.props; | ||
const comment = commentsMessage.map(message => ( | ||
<section className="comments" key={message.id}> | ||
<article className="comment"> | ||
<a className="comment-img" href="#non"> | ||
<img src={message.author.image} alt="" width="50" height="50" /> | ||
</a> | ||
<div className="comment-body"> | ||
<div className="text"> | ||
<p>{message.body}</p> | ||
</div> | ||
<p className="attribution"> | ||
by <a href={`/profile/${message.author.username}`}>{message.author.username}</a> at {moment(message.createdAt).format( | ||
"h:mm:ss a [on] MMMM Do YYYY.")} | ||
|
||
</p> | ||
</div> | ||
</article> | ||
</section> | ||
)); | ||
return ( | ||
<div className="row"> | ||
<div className="col col-lg-1" /> | ||
<div className="col col-lg-10"> | ||
<div className="row"> | ||
<div className="col col-lg-1" /> | ||
<div className="col col-lg-11"> | ||
<form | ||
method="POST" | ||
onSubmit={this.handleOnSubmit} | ||
className="comment-forms" | ||
> | ||
<textarea | ||
className="form-control" | ||
name="comments" | ||
value={this.state.comments} | ||
onChange={this.handleOnChange} | ||
> | ||
comments here | ||
</textarea> | ||
<button | ||
type="submit" | ||
className="btn btn-comment" | ||
style={{ marginTop: "10px" }} | ||
> | ||
comment | ||
</button> | ||
</form> | ||
</div> | ||
</div> | ||
{comment} | ||
</div> | ||
<div className="col col-lg-1" /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export const mapStateToProps = state => ({ | ||
commentsSuccess: state.comments.success, | ||
commentsErrors: state.comments.errors, | ||
commentsMessage: state.getComments.comments | ||
}); | ||
export default connect( | ||
mapStateToProps, | ||
{ articleComments, fetchComments } | ||
)(Comments); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { CREATE_COMMENTS, CREATE_COMMENTS_ERRORS } from "../actions/types"; | ||
|
||
const initialState = { | ||
success: {}, | ||
errors: {} | ||
}; | ||
|
||
export default (state = initialState, action) => { | ||
switch (action.type) { | ||
case CREATE_COMMENTS: | ||
return { | ||
...state, | ||
success: action.payload | ||
}; | ||
case CREATE_COMMENTS_ERRORS: | ||
return { | ||
...state, | ||
errors: action.payload | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; |
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 @@ | ||
import { FETCH_COMMENTS } from "../actions/types"; | ||
|
||
const initialState = { | ||
comments: [] | ||
}; | ||
export default (state = initialState, action) => { | ||
switch (action.type) { | ||
case FETCH_COMMENTS: | ||
return { | ||
...state, | ||
comments: action.payload | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; |
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.