From 5c5c224617ad16a61e45cfbbe579dc80218f74a3 Mon Sep 17 00:00:00 2001 From: Laura Eckman Date: Tue, 6 Jun 2017 16:56:35 -0700 Subject: [PATCH 01/14] determine who can edit #7 --- .../app/bundles/KudosApp/components/Kudo.jsx | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/client/app/bundles/KudosApp/components/Kudo.jsx b/client/app/bundles/KudosApp/components/Kudo.jsx index 594d55c..6a46238 100644 --- a/client/app/bundles/KudosApp/components/Kudo.jsx +++ b/client/app/bundles/KudosApp/components/Kudo.jsx @@ -9,14 +9,18 @@ export default class Kudo extends React.Component { constructor(props, context) { super(props, context); - _.bindAll(this, 'formatTimestamp', 'likedBySelf', 'formatLikeText'); + _.bindAll(this, 'formatTimestamp', 'likedBySelf', 'formatLikeText', 'postedByActiveUser'); this.state = { likeAction: this.props.likeKudo(this.props.id), timestamp: this.formatTimestamp(this.props.kudo.given_at), thumbColor: grey400, - likeText: this.formatLikeText(this.props.kudo.likes.length) + likeText: this.formatLikeText(this.props.kudo.likes.length), + editMsg: this.postedByActiveUser() ? "Edit" : "" }; + console.log("here's the props"); + console.log(this.props); + if (this.likedBySelf(this.props.kudo.likes, this.props.giverId)) { this.state.thumbColor = lightBlue400; this.state.likeAction = this.props.unlikeKudo(this.props.id); @@ -25,7 +29,6 @@ export default class Kudo extends React.Component { formatTimestamp(t) { let ts = moment(t); - console.log("Parsed Zone:", ts); return `At ${ts.format('h:mm a')} on ${ts.format('MMM D, YYYY')}` } @@ -49,6 +52,12 @@ export default class Kudo extends React.Component { return match } + postedByActiveUser() { + let user = this.props.giverId; + let poster = this.props.kudo.giver_id; + return (user == poster); + } + componentWillReceiveProps(props) { if (this.props.kudo.likes != props.kudo.likes) { this.setState({ likeText: this.formatLikeText(props.kudo.likes.length) }); @@ -79,6 +88,9 @@ export default class Kudo extends React.Component {
{this.state.timestamp}
+
+ {this.state.editMsg} +
} From e74703374bd7df2e3bb4ed0fc7e20f6a7a76ebf6 Mon Sep 17 00:00:00 2001 From: Laura Eckman Date: Wed, 7 Jun 2017 16:40:25 -0700 Subject: [PATCH 02/14] scaffolding to attach updates to the store --- .../KudosApp/actions/actionCreators.jsx | 43 +++++++++++++++++++ .../app/bundles/KudosApp/components/Kudo.jsx | 31 ++++++++----- .../KudosApp/constants/appConstants.jsx | 1 + 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/client/app/bundles/KudosApp/actions/actionCreators.jsx b/client/app/bundles/KudosApp/actions/actionCreators.jsx index 78e947d..80da3e0 100644 --- a/client/app/bundles/KudosApp/actions/actionCreators.jsx +++ b/client/app/bundles/KudosApp/actions/actionCreators.jsx @@ -11,6 +11,14 @@ const postedKudo = (receiverEmail, messageBody) => { } } +const updatedKudo = ( kudoId, newMessage ) => { + return { + type: actionTypes.UPDATED_KUDO, + kudoId, + newMessage + } +} + const serverReceivedKudo = (res) => { const receiverId = res.data.kudo.receiver_id const messageBody = res.data.kudo.body @@ -95,6 +103,41 @@ const createKudo = (receiverEmail, messageBody, onSuccess = null, onFailure = nu } } +const editKudo = ({ id, message }) => { + // stub + return dispatch => { + dispatch(updatedKudo( id, message )) + dispatch(resetErrorMessage()); + + return request({ + method: 'PATCH', + url: '/kudos.json', + responseType: 'json', + // headers: { + // 'X-CSRF-Token': Config.getCSRFToken(), + // }, + data: { + kudo: { + id: id, + body: messageBody, + } + }, + }).then(res => { + console.log(res) + if (onSuccess) { + onSuccess(res); + } + dispatch(serverReceivedKudo(res)); + }).catch(err => { + console.log(err) + if (onFailure) { + onFailure(err); + } + dispatch(serverRejectedKudo(err)) + }) + } +} + const initialize = ({ id, name }) => { return { type: actionTypes.INITIALIZE, diff --git a/client/app/bundles/KudosApp/components/Kudo.jsx b/client/app/bundles/KudosApp/components/Kudo.jsx index 6a46238..1de6933 100644 --- a/client/app/bundles/KudosApp/components/Kudo.jsx +++ b/client/app/bundles/KudosApp/components/Kudo.jsx @@ -9,18 +9,16 @@ export default class Kudo extends React.Component { constructor(props, context) { super(props, context); - _.bindAll(this, 'formatTimestamp', 'likedBySelf', 'formatLikeText', 'postedByActiveUser'); + _.bindAll(this, 'formatTimestamp', 'likedBySelf', 'formatLikeText', 'postedByActiveUser', 'makeEditable'); this.state = { likeAction: this.props.likeKudo(this.props.id), + updateAction: () => true, // placeholder, initialize from props timestamp: this.formatTimestamp(this.props.kudo.given_at), thumbColor: grey400, likeText: this.formatLikeText(this.props.kudo.likes.length), - editMsg: this.postedByActiveUser() ? "Edit" : "" + editing: false }; - console.log("here's the props"); - console.log(this.props); - if (this.likedBySelf(this.props.kudo.likes, this.props.giverId)) { this.state.thumbColor = lightBlue400; this.state.likeAction = this.props.unlikeKudo(this.props.id); @@ -29,11 +27,11 @@ export default class Kudo extends React.Component { formatTimestamp(t) { let ts = moment(t); - return `At ${ts.format('h:mm a')} on ${ts.format('MMM D, YYYY')}` + return `At ${ts.format('h:mm a')} on ${ts.format('MMM D, YYYY')}`; } formatLikeText(numLikes) { - if (numLikes == 0) { + if (numLikes === 0) { return ""; } return `${numLikes} ${numLikes === 1 ? 'person likes': 'people like'} this`; @@ -58,6 +56,10 @@ export default class Kudo extends React.Component { return (user == poster); } + makeEditable(e) { + this.setState({editing: true}); + } + componentWillReceiveProps(props) { if (this.props.kudo.likes != props.kudo.likes) { this.setState({ likeText: this.formatLikeText(props.kudo.likes.length) }); @@ -70,6 +72,17 @@ export default class Kudo extends React.Component { } render() { + + const Edit = () =>
+ +
+ + const Save = () =>
+ +
+ return

Kudos, {this.props.kudo.receiver}!

@@ -88,9 +101,7 @@ export default class Kudo extends React.Component {
{this.state.timestamp}
-
- {this.state.editMsg} -
+ {this.postedByActiveUser() ? (this.state.editing ? : ) : (null)}
} diff --git a/client/app/bundles/KudosApp/constants/appConstants.jsx b/client/app/bundles/KudosApp/constants/appConstants.jsx index a0ce1eb..b9a2ad6 100644 --- a/client/app/bundles/KudosApp/constants/appConstants.jsx +++ b/client/app/bundles/KudosApp/constants/appConstants.jsx @@ -5,6 +5,7 @@ import mirrorCreator from 'mirror-creator'; const actionTypes = mirrorCreator([ 'POSTED_KUDO', + 'UPDATED_KUDO', 'INITIALIZE', 'SERVER_RECEIVED_KUDO', 'SERVER_REJECTED_KUDO', From 94fd911ec69a5211d2398214dd091dc0f2002f64 Mon Sep 17 00:00:00 2001 From: Laura Eckman Date: Wed, 7 Jun 2017 17:43:06 -0700 Subject: [PATCH 03/14] update handler & route #7 --- app/controllers/kudos_controller.rb | 11 +++++++++++ .../app/bundles/KudosApp/actions/actionCreators.jsx | 6 +----- config/routes.rb | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/controllers/kudos_controller.rb b/app/controllers/kudos_controller.rb index 2fcb371..5df0be4 100644 --- a/app/controllers/kudos_controller.rb +++ b/app/controllers/kudos_controller.rb @@ -71,6 +71,17 @@ def create end end + def update + kudo = Kudo.find_by(id: kudo_params[:id]) + kudo.body = kudo_params[:body] + + if kudo.save + render json: { kudo: kudo }, status: :created + else + render json: { error: kudo.errors.messages.values.flatten.to_sentence }, status: :unprocessable_entity + end + end + private def basic_user_info_by_id(id) diff --git a/client/app/bundles/KudosApp/actions/actionCreators.jsx b/client/app/bundles/KudosApp/actions/actionCreators.jsx index 80da3e0..1824316 100644 --- a/client/app/bundles/KudosApp/actions/actionCreators.jsx +++ b/client/app/bundles/KudosApp/actions/actionCreators.jsx @@ -103,8 +103,7 @@ const createKudo = (receiverEmail, messageBody, onSuccess = null, onFailure = nu } } -const editKudo = ({ id, message }) => { - // stub +const editKudo = ({ id, message, onSuccess = null, onFailure = null }) => { return dispatch => { dispatch(updatedKudo( id, message )) dispatch(resetErrorMessage()); @@ -113,9 +112,6 @@ const editKudo = ({ id, message }) => { method: 'PATCH', url: '/kudos.json', responseType: 'json', - // headers: { - // 'X-CSRF-Token': Config.getCSRFToken(), - // }, data: { kudo: { id: id, diff --git a/config/routes.rb b/config/routes.rb index 26a8200..746afb3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true Rails.application.routes.draw do - resources :kudos, only: [:index, :create], constraints: { format: :json }, defaults: { format: :json } + resources :kudos, only: [:index, :create, :update], constraints: { format: :json }, defaults: { format: :json } get 'kudos_app', to: 'kudos_app#index' # Auth From 7ee00c265223d9bccbc6772e5560bdf359da5b70 Mon Sep 17 00:00:00 2001 From: Laura Eckman Date: Thu, 8 Jun 2017 14:18:21 -0700 Subject: [PATCH 04/14] Textarea support for editable message --- .../app/bundles/KudosApp/components/Kudo.jsx | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/client/app/bundles/KudosApp/components/Kudo.jsx b/client/app/bundles/KudosApp/components/Kudo.jsx index 1de6933..09f030b 100644 --- a/client/app/bundles/KudosApp/components/Kudo.jsx +++ b/client/app/bundles/KudosApp/components/Kudo.jsx @@ -3,19 +3,21 @@ import _ from 'lodash'; import moment from 'moment'; import { grey400, lightBlue400 } from 'material-ui/styles/colors'; import FloatingActionButton from 'material-ui/FloatingActionButton'; +import Textarea from 'react-textarea-autosize'; import ThumbUp from 'material-ui/svg-icons/action/thumb-up'; export default class Kudo extends React.Component { constructor(props, context) { super(props, context); - _.bindAll(this, 'formatTimestamp', 'likedBySelf', 'formatLikeText', 'postedByActiveUser', 'makeEditable'); + _.bindAll(this, 'formatTimestamp', 'likedBySelf', 'formatLikeText', 'postedByActiveUser', 'makeEditable', 'setMessage'); this.state = { likeAction: this.props.likeKudo(this.props.id), - updateAction: () => true, // placeholder, initialize from props + updateAction: () => console.log("Saved"), // placeholder, initialize from props timestamp: this.formatTimestamp(this.props.kudo.given_at), thumbColor: grey400, likeText: this.formatLikeText(this.props.kudo.likes.length), + body: this.props.kudo.body, editing: false }; @@ -71,6 +73,10 @@ export default class Kudo extends React.Component { } } + setMessage(e) { + this.setState({body: e.target.value}) + } + render() { const Edit = () =>
@@ -80,7 +86,7 @@ export default class Kudo extends React.Component {
const Save = () =>
- +
return
@@ -89,10 +95,18 @@ export default class Kudo extends React.Component { {this.props.kudo.receiver}
+ {this.state.editing ? ( +