From 9a375a8d7fdef7f80430a247e15f5e518d9e3a65 Mon Sep 17 00:00:00 2001 From: Diz00 Date: Thu, 16 Nov 2017 14:36:50 +0200 Subject: [PATCH 1/9] modified gitignore file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d41d351f8..6ab7cf84b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ public/* dist coverage/ .nyc_output/ +yarn.lock \ No newline at end of file From 8b681eff165b56af84a25e8f21333bf1d2c1fb20 Mon Sep 17 00:00:00 2001 From: Diz00 Date: Thu, 16 Nov 2017 17:49:30 +0200 Subject: [PATCH 2/9] added comment element --- package.json | 54 +++++++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 861cb82e6..71d00fa99 100644 --- a/package.json +++ b/package.json @@ -1,25 +1,29 @@ { "name": "mern-starter", "version": "2.0.0", - "description": "Boilerplate project for building Isomorphic apps using React and Redux", + "description": + "Boilerplate project for building Isomorphic apps using React and Redux", "scripts": { - "test": "cross-env NODE_ENV=test PORT=8080 MONGO_URL=mongodb://localhost:27017/mern-test node_modules/.bin/nyc node --harmony-proxies node_modules/.bin/ava", + "test": + "cross-env NODE_ENV=test PORT=8080 MONGO_URL=mongodb://localhost:27017/mern-test node_modules/.bin/nyc node --harmony-proxies node_modules/.bin/ava", "watch:test": "npm run test -- --watch", "cover": "nyc npm run test", - "check-coverage": "nyc check-coverage --statements 100 --branches 100 --functions 100 --lines 100", - "start": "cross-env BABEL_DISABLE_CACHE=1 NODE_ENV=development nodemon index.js", + "check-coverage": + "nyc check-coverage --statements 100 --branches 100 --functions 100 --lines 100", + "start": + "cross-env BABEL_DISABLE_CACHE=1 NODE_ENV=development nodemon index.js", "start:prod": "cross-env NODE_ENV=production node index.js", - "bs": "npm run clean && npm run build && npm run build:server && npm run start:prod", - "build": "cross-env NODE_ENV=production webpack --config webpack.config.prod.js", - "build:server": "cross-env NODE_ENV=production webpack --config webpack.config.server.js", + "bs": + "npm run clean && npm run build && npm run build:server && npm run start:prod", + "build": + "cross-env NODE_ENV=production webpack --config webpack.config.prod.js", + "build:server": + "cross-env NODE_ENV=production webpack --config webpack.config.server.js", "clean": "rimraf dist", "slate": "rimraf node_modules && npm install", "lint": "eslint client server" }, - "pre-commit": [ - "lint", - "test" - ], + "pre-commit": ["lint", "test"], "repository": { "type": "git", "url": "git+https://github.com/Hashnode/mern-starter.git" @@ -28,7 +32,8 @@ "url": "https://github.com/Hashnode/mern-starter/issues" }, "homepage": "https://github.com/Hashnode/mern-starter#readme", - "author": "Prashant Abhishek , Mayank Chandola , Sandeep Panda , Syed Fazle Rahman , Alkshendra Maurya ", + "author": + "Prashant Abhishek , Mayank Chandola , Sandeep Panda , Syed Fazle Rahman , Alkshendra Maurya ", "license": "MIT", "dependencies": { "babel-core": "^6.9.1", @@ -115,25 +120,14 @@ "node": ">=4" }, "ava": { - "files": [ - "client/**/*.spec.js", - "server/**/*.spec.js" - ], - "source": [ - "client/**/*.js", - "server/**/*.js" - ], + "files": ["client/**/*.spec.js", "server/**/*.spec.js"], + "source": ["client/**/*.js", "server/**/*.js"], "failFast": true, "babel": "inherit", - "require": [ - "./server/util/setup-test-env.js" - ] + "require": ["./server/util/setup-test-env.js"] }, "nyc": { - "include": [ - "client/**/*.js", - "server/**/*.js" - ], + "include": ["client/**/*.js", "server/**/*.js"], "exclude": [ "**/*.spec.js", "client/reducers.js", @@ -144,10 +138,6 @@ "server/config.js", "server/dummyData.js" ], - "reporter": [ - "lcov", - "text", - "html" - ] + "reporter": ["lcov", "text", "html"] } } From a12714efbedaec732f565e5ef69bdef054d4fe30 Mon Sep 17 00:00:00 2001 From: Diz00 Date: Thu, 16 Nov 2017 19:25:36 +0200 Subject: [PATCH 3/9] implemented CommentReducer --- client/modules/Post/CommentActions.js | 23 +++++ client/modules/Post/CommentReducer.js | 57 ++++++++++++ .../Comments/CommentCreateWidget.css | 51 +++++++++++ .../Comments/CommentCreateWidget.js | 45 ++++++++++ .../Post/components/Comments/CommentList.css | 4 + .../Post/components/Comments/CommentList.js | 30 +++++++ .../components/Comments/CommentListItem.css | 43 +++++++++ .../components/Comments/CommentListItem.js | 51 +++++++++++ .../pages/PostDetailPage/PostDetailPage.js | 90 ++++++++++++++----- client/reducers.js | 10 ++- package.json | 54 ++++++----- 11 files changed, 409 insertions(+), 49 deletions(-) create mode 100644 client/modules/Post/CommentActions.js create mode 100644 client/modules/Post/CommentReducer.js create mode 100644 client/modules/Post/components/Comments/CommentCreateWidget.css create mode 100644 client/modules/Post/components/Comments/CommentCreateWidget.js create mode 100644 client/modules/Post/components/Comments/CommentList.css create mode 100644 client/modules/Post/components/Comments/CommentList.js create mode 100644 client/modules/Post/components/Comments/CommentListItem.css create mode 100644 client/modules/Post/components/Comments/CommentListItem.js diff --git a/client/modules/Post/CommentActions.js b/client/modules/Post/CommentActions.js new file mode 100644 index 000000000..1a41191e7 --- /dev/null +++ b/client/modules/Post/CommentActions.js @@ -0,0 +1,23 @@ +import callApi from "../../util/apiCaller"; + +export const ADD_COMMENTS = "ADD_COMMENTS"; +export const ADD_COMMENT = "ADD_COMMENT"; +export const EDIT_COMMENT = "EDIT_COMMENT"; +export const EDIT_COMMENT_MODE = "EDIT_COMMENT_MODE"; +export const DELETE_COMMENT = "DELETE_COMMENT"; +export const CANCEL_EDIT_COMMENT = "CANCEL_EDIT_COMMENT"; + +export function addComments(comments) { + return { + type: ADD_COMMENTS, + comments + }; +} + +export function fetchComments(cuid) { + return dispatch => { + return callApi(`posts/${cuid}/comments`).then(res => { + dispatch(addComments(res.comments)); + }); + }; +} diff --git a/client/modules/Post/CommentReducer.js b/client/modules/Post/CommentReducer.js new file mode 100644 index 000000000..63bd8a059 --- /dev/null +++ b/client/modules/Post/CommentReducer.js @@ -0,0 +1,57 @@ +import { + ADD_COMMENTS, + ADD_COMMENT, + EDIT_COMMENT, + EDIT_COMMENT_MODE, + CANCEL_EDIT_COMMENT, + DELETE_COMMENT +} from "./CommentActions"; + +const initialState = { + comments: [], + editComment: null +}; + +const CommentReducer = (state = initialState, action) => { + switch (action.type) { + case ADD_COMMENTS: + return { ...state, comments: action.comments }; + case ADD_COMMENT: { + return { ...state, comments: [...state.comments, action.comment] }; + } + case EDIT_COMMENT: { + let index = state.comments.findIndex(comment => { + if (comment.cuid === action.comment.cuid) { + return true; + } + return false; + }); + + let startCommentArray = state.comments.slice(0, index); + let endCommentArray = state.comments.slice( + index + 1, + state.comments.length + ); + return { + ...state, + comments: [...startCommentArray, action.comment, ...endCommentArray] + }; + } + case EDIT_COMMENT_MODE: + return { ...state, editComment: action.cuid }; + case CANCEL_EDIT_COMMENT: + return { ...state, editComment: null }; + case DELETE_COMMENT: + return { + editComment: null, + comments: state.comments.filter(comment => comment.cuid !== action.cuid) + }; + default: + return state; + } +}; + +/* Selectors */ + +// Get corresponding comments +export const getComments = state => state.comments; diff --git a/client/modules/Post/components/Comments/CommentCreateWidget.css b/client/modules/Post/components/Comments/CommentCreateWidget.css new file mode 100644 index 000000000..effce4ae6 --- /dev/null +++ b/client/modules/Post/components/Comments/CommentCreateWidget.css @@ -0,0 +1,51 @@ +.form { + background: #fafafa; + padding: 32px 0; + border: 1px solid #eee; + border-radius: 4px; +} + +.form-content { + width: 100%; + max-width: 600px; + margin: auto; + font-size: 14px; +} + +.form-title { + font-size: 16px; + font-weight: 700; + margin-bottom: 16px; + color: #757575; +} + +.form-field { + width: 100%; + margin-bottom: 16px; + font-family: "Lato", sans-serif; + font-size: 16px; + line-height: normal; + padding: 12px 16px; + border-radius: 4px; + border: 1px solid #ddd; + outline: none; + color: #212121; +} + +textarea { + min-height: 200px; +} + +.post-submit-button { + display: inline-block; + padding: 8px 16px; + font-size: 18px; + color: #fff; + background: #03a9f4; + text-decoration: none; + border-radius: 4px; +} + +.appear { + display: block; +} diff --git a/client/modules/Post/components/Comments/CommentCreateWidget.js b/client/modules/Post/components/Comments/CommentCreateWidget.js new file mode 100644 index 000000000..fe97aee9d --- /dev/null +++ b/client/modules/Post/components/Comments/CommentCreateWidget.js @@ -0,0 +1,45 @@ +import React, { Component } from "react"; +import { connect } from "react-redux"; + +import styles from "./CommentCreateWidget.css"; + +class CommentCreateWidget extends Component { + createComment = e => { + e.preventDefault(); + alert("hey"); + }; + render() { + return ( +
+
+

Write a new comment

+ + +