diff --git a/Intl/localizationData/en.js b/Intl/localizationData/en.js index 79b481d3c..8018a7560 100644 --- a/Intl/localizationData/en.js +++ b/Intl/localizationData/en.js @@ -12,6 +12,12 @@ export default { postTitle: 'Post Title', postContent: 'Post Content', submit: 'Submit', + cancel: 'Cancel', + deleteComment: 'Delete Comment', + showComments: 'Show Comments', + hideComments: 'Hide Comments', + editComment: 'Edit Comment', + createNewComment: 'Create New Comment', comment: `user {name} {value, plural, =0 {does not have any comments} =1 {has # comment} diff --git a/client/modules/Comment/CommentActions.js b/client/modules/Comment/CommentActions.js new file mode 100644 index 000000000..93a3354bc --- /dev/null +++ b/client/modules/Comment/CommentActions.js @@ -0,0 +1,70 @@ +import callApi from '../../util/apiCaller'; + +// Export Constants +export const GET_COMMENTS = 'GET_COMMENTS'; +export const ADD_COMMENT = 'ADD_COMMENT'; +export const DELETE_COMMENT = 'DELETE_COMMENT'; +export const EDIT_COMMENT = 'EDIT_COMMENT'; + +// Export Actions + +export function getComments(comments) { + return { + type: GET_COMMENTS, + comments, + }; +} + +export function fetchComments(id) { + return (dispatch) => { + return callApi(`/posts/${id}/comments`).then(res => { + dispatch(getComments(res.comments)); + }); + }; +} + +export function addComment(comment) { + return { + type: ADD_COMMENT, + comment, + }; +} + +export function addCommentRequest(comment, id) { + return (dispatch) => { + return callApi(`posts/${id}/comment`, 'post', { + comment: { + content: comment.content, + createdBy: comment.createdBy, + }, + }).then(res => dispatch(addComment(res.comment))); + }; +} + + +export function editComment(id, content) { + return { + type: EDIT_COMMENT, + id, + content, + }; +} + +export function editCommentRequest(id, content) { + return (dispatch) => { + return callApi(`comment/${id}`, 'put', { content }).then(() => dispatch(editComment(id, content))); + }; +} + +export function deleteComment(id) { + return { + type: DELETE_COMMENT, + id, + }; +} + +export function deleteCommentRequest(id) { + return (dispatch) => { + return callApi(`comment/${id}`, 'delete').then(() => dispatch(deleteComment(id))); + }; +} diff --git a/client/modules/Comment/CommentReducer.js b/client/modules/Comment/CommentReducer.js new file mode 100644 index 000000000..609b89c6e --- /dev/null +++ b/client/modules/Comment/CommentReducer.js @@ -0,0 +1,46 @@ +import { GET_COMMENTS, ADD_COMMENT, DELETE_COMMENT, EDIT_COMMENT } from './CommentActions'; + +// Initial State +const initialState = { comments: [] }; + +const CommentReducer = (state = initialState, action) => { + switch (action.type) { + case GET_COMMENTS : + return { + comments: action.comments, + }; + + case ADD_COMMENT : + return { + comments: [action.comment, ...state.comments], + }; + + case EDIT_COMMENT : + return { + comments: state.comments.map(comment => { + if (comment._id === action.id) { + const newComment = { + content: action.content, + postId: comment.postId, + createdBy: comment.createdBy, + dateAdded: comment.dateAdded, + }; + + return newComment; + } + return comment; + }), + }; + + case DELETE_COMMENT : + return { + comments: state.comments.filter(comment => comment._id !== action.id), + }; + + default: + return state; + } +}; + +// Export Reducer +export default CommentReducer; diff --git a/client/modules/Comment/components/CommentCreateWidget/CommentCreateWidget.css b/client/modules/Comment/components/CommentCreateWidget/CommentCreateWidget.css new file mode 100644 index 000000000..7d6c370ff --- /dev/null +++ b/client/modules/Comment/components/CommentCreateWidget/CommentCreateWidget.css @@ -0,0 +1,55 @@ +.form { + display: block; + background: #FAFAFA; + padding: 32px 0; + border: 1px solid #eee; + border-radius: 4px; +} + +.form-content{ + width: 100%; + margin: auto; + font-size: 14px; +} + +.form-title{ + font-size: 16px; + font-weight: 700; + margin-bottom: 16px; + color: #757575; +} + +.form-field{ + width: 100%; + margin-bottom: 12px; + font-family: 'Lato', sans-serif; + font-size: 13px; + line-height: normal; + padding: 6px 10px; + border-radius: 4px; + border: 1px solid #ddd; + outline: none; + color: #212121; +} + +.form-field-large { + min-height: 120px; +} + +.comment-button { + display: inline-block; + padding: 6px 10px; + margin-left: 10px; + font-size: 14px; + color: #FFF; + text-decoration: none; + border-radius: 4px; +} + +.comment-submit-button { + background: #03A9F4; +} + +.comment-cancel-button { + background: #9a533c; +} diff --git a/client/modules/Comment/components/CommentCreateWidget/CommentCreateWidget.js b/client/modules/Comment/components/CommentCreateWidget/CommentCreateWidget.js new file mode 100644 index 000000000..db886b959 --- /dev/null +++ b/client/modules/Comment/components/CommentCreateWidget/CommentCreateWidget.js @@ -0,0 +1,49 @@ +import React, { useState } from 'react'; +import PropTypes from 'prop-types'; +import { FormattedMessage } from 'react-intl'; + +// Import Style +import styles from './CommentCreateWidget.css'; + +const CommentCreateWidget = ({ addComment, closeForm }) => { + const [authorName, changeAuthor] = useState(''); + const [content, changeContent] = useState(''); + + const addNewComment = () => { + addComment(authorName, content); + closeForm(); + }; + + return ( +
+
+

+ changeAuthor(e.target.value)} /> +