From 9511e9817f55ab9390fc599ebfbe6c81ebc1264e Mon Sep 17 00:00:00 2001 From: Oskari Hiltunen Date: Tue, 2 May 2017 12:17:23 +0300 Subject: [PATCH 1/5] Support cateringId in fetchComments action Refs #154. --- app/api/actions/comments.js | 6 +++--- app/api/actions/comments.spec.js | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/api/actions/comments.js b/app/api/actions/comments.js index f8ab9abf..85ead1d2 100644 --- a/app/api/actions/comments.js +++ b/app/api/actions/comments.js @@ -1,13 +1,13 @@ import createApiAction from './createApiAction'; -function fetchComments({ reservationId }) { +function fetchComments({ cateringId, reservationId }) { return createApiAction({ // TODO: Replace with correct endpoint once the backend supports it. endpoint: 'http://www.mocky.io/v2/58ffa5c7110000ef16f60030', - params: { reservationId }, + params: { cateringId, reservationId }, method: 'GET', type: 'COMMENTS', - options: { meta: { reservationId } }, + options: { meta: { cateringId, reservationId } }, }); } diff --git a/app/api/actions/comments.spec.js b/app/api/actions/comments.spec.js index 9cdfed6d..3a993d4e 100644 --- a/app/api/actions/comments.spec.js +++ b/app/api/actions/comments.spec.js @@ -4,7 +4,9 @@ import { createComment, fetchComments } from './comments'; describe('api/actions/comments', () => { describe('fetchComments', () => { + const cateringId = 85559; const reservationId = 3819; + createApiTest({ name: 'fetchComments', action: fetchComments, @@ -23,6 +25,25 @@ describe('api/actions/comments', () => { }, }, }); + + createApiTest({ + name: 'fetchComments', + action: fetchComments, + args: [{ cateringId }], + tests: { + method: 'GET', + endpoint: `http://www.mocky.io/v2/58ffa5c7110000ef16f60030?catering_id=${cateringId}`, + request: { + type: types.COMMENTS_GET_REQUEST, + }, + success: { + type: types.COMMENTS_GET_SUCCESS, + }, + error: { + type: types.COMMENTS_GET_ERROR, + }, + }, + }); }); describe('createComment', () => { From df10941309b5d8fde12dfb54f59e482e2115b80d Mon Sep 17 00:00:00 2001 From: Oskari Hiltunen Date: Tue, 2 May 2017 12:24:25 +0300 Subject: [PATCH 2/5] Support cateringId in comments get reducer Refs #154. --- app/api/reducers/dataReducer.js | 12 +++++++++--- app/shared/comments/CommentsContainer.js | 5 ++++- app/shared/comments/CommentsContainer.spec.js | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/api/reducers/dataReducer.js b/app/api/reducers/dataReducer.js index 30f69a5f..4af7fe74 100644 --- a/app/api/reducers/dataReducer.js +++ b/app/api/reducers/dataReducer.js @@ -55,18 +55,24 @@ function dataReducer(state = initialState, action) { } case actionTypes.COMMENTS_GET_SUCCESS: { + const key = ( + action.meta.reservationId + ? `reservation-${action.meta.reservationId}` + : `catering-${action.meta.cateringId}` + ); return state.merge({ comments: state.comments.merge({ - [action.meta.reservationId]: action.payload.results, + [key]: action.payload.results, }), }); } case actionTypes.COMMENTS_POST_SUCCESS: { // TODO: Use response data instead of data from meta + const key = `reservation-${action.meta.reservationId}`; return state.merge({ comments: state.comments.merge({ - [action.meta.reservationId]: [ - ...state.comments[action.meta.reservationId], + [key]: [ + ...state.comments[key], action.meta.data, ], }), diff --git a/app/shared/comments/CommentsContainer.js b/app/shared/comments/CommentsContainer.js index 489aa201..ff40a6c4 100644 --- a/app/shared/comments/CommentsContainer.js +++ b/app/shared/comments/CommentsContainer.js @@ -7,7 +7,10 @@ import { createComment, fetchComments } from 'api/actions'; import { currentUserSelector } from 'auth/selectors'; import Comments from './Comments'; -function commentsSelector(state, props) { return state.data.comments[props.reservationId]; } +function commentsSelector(state, props) { + const key = `reservation-${props.reservationId}`; + return state.data.comments[key]; +} export const selector = createStructuredSelector({ comments: commentsSelector, diff --git a/app/shared/comments/CommentsContainer.spec.js b/app/shared/comments/CommentsContainer.spec.js index ef013c1d..e31a782b 100644 --- a/app/shared/comments/CommentsContainer.spec.js +++ b/app/shared/comments/CommentsContainer.spec.js @@ -166,7 +166,7 @@ describe('shared/comments/CommentsContainer', () => { it('selects reservation comments', () => { const comments = [{ id: 1 }, { id: 2 }]; const actual = getSelected( - { 'data.comments': { 1437: comments } }, + { 'data.comments': { 'reservation-1437': comments } }, { reservationId: 1437 }, ); expect(actual.comments).to.deep.equal(comments); From 09ce5c665e957199cef247ac8e1e0039ac8e49a0 Mon Sep 17 00:00:00 2001 From: Oskari Hiltunen Date: Tue, 2 May 2017 12:30:25 +0300 Subject: [PATCH 3/5] Support cateringId in CommentsContainer Refs #154. --- app/shared/comments/CommentsContainer.js | 14 ++++++++--- app/shared/comments/CommentsContainer.spec.js | 25 ++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/shared/comments/CommentsContainer.js b/app/shared/comments/CommentsContainer.js index ff40a6c4..0de4cab9 100644 --- a/app/shared/comments/CommentsContainer.js +++ b/app/shared/comments/CommentsContainer.js @@ -8,7 +8,11 @@ import { currentUserSelector } from 'auth/selectors'; import Comments from './Comments'; function commentsSelector(state, props) { - const key = `reservation-${props.reservationId}`; + const key = ( + props.reservationId + ? `reservation-${props.reservationId}` + : `catering-${props.cateringId}` + ); return state.data.comments[key]; } @@ -39,16 +43,20 @@ function mergeProps(state, dispatch, props) { export class CommentsContainer extends React.Component { static propTypes = { + cateringId: PropTypes.number, comments: PropTypes.array, createComment: PropTypes.func.isRequired, fetchComments: PropTypes.func.isRequired, name: PropTypes.string.isRequired, - reservationId: PropTypes.number.isRequired, + reservationId: PropTypes.number, } state = { isOpen: false } componentWillMount() { - this.props.fetchComments({ reservationId: this.props.reservationId }); + this.props.fetchComments({ + cateringId: this.props.cateringId, + reservationId: this.props.reservationId, + }); } getCommentCount() { diff --git a/app/shared/comments/CommentsContainer.spec.js b/app/shared/comments/CommentsContainer.spec.js index e31a782b..3e5c7305 100644 --- a/app/shared/comments/CommentsContainer.spec.js +++ b/app/shared/comments/CommentsContainer.spec.js @@ -32,7 +32,21 @@ describe('shared/comments/CommentsContainer', () => { const reservationId = 1938; getWrapper({ fetchComments: fetch, reservationId }); expect(fetch.callCount).to.equal(1); - expect(fetch.lastCall.args).to.deep.equal([{ reservationId }]); + expect(fetch.lastCall.args).to.deep.equal([{ + cateringId: undefined, + reservationId, + }]); + }); + + it('calls fetchComments with catering id', () => { + const fetch = simple.mock(); + const cateringId = 1938; + getWrapper({ fetchComments: fetch, cateringId, reservationId: undefined }); + expect(fetch.callCount).to.equal(1); + expect(fetch.lastCall.args).to.deep.equal([{ + cateringId, + reservationId: undefined, + }]); }); }); @@ -171,5 +185,14 @@ describe('shared/comments/CommentsContainer', () => { ); expect(actual.comments).to.deep.equal(comments); }); + + it('selects catering comments', () => { + const comments = [{ id: 1 }, { id: 2 }]; + const actual = getSelected( + { 'data.comments': { 'catering-1437': comments } }, + { cateringId: 1437 }, + ); + expect(actual.comments).to.deep.equal(comments); + }); }); }); From dd07aa38ef1b644e5159b9b3ce3f21446e2d2bac Mon Sep 17 00:00:00 2001 From: Oskari Hiltunen Date: Tue, 2 May 2017 12:37:59 +0300 Subject: [PATCH 4/5] Show catering comments in reservation info modal Refs #154. --- app/shared/comments/CommentsContainer.js | 26 ++++++++++--------- app/shared/comments/CommentsContainer.spec.js | 2 +- .../reservation-info/ReservationInfo.js | 5 ++++ .../reservation-info/ReservationInfo.spec.js | 8 ++++++ 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/app/shared/comments/CommentsContainer.js b/app/shared/comments/CommentsContainer.js index 0de4cab9..a2428d70 100644 --- a/app/shared/comments/CommentsContainer.js +++ b/app/shared/comments/CommentsContainer.js @@ -7,19 +7,21 @@ import { createComment, fetchComments } from 'api/actions'; import { currentUserSelector } from 'auth/selectors'; import Comments from './Comments'; -function commentsSelector(state, props) { - const key = ( - props.reservationId - ? `reservation-${props.reservationId}` - : `catering-${props.cateringId}` - ); - return state.data.comments[key]; -} +export function selector() { + function commentsSelector(state, props) { + const key = ( + props.reservationId + ? `reservation-${props.reservationId}` + : `catering-${props.cateringId}` + ); + return state.data.comments[key]; + } -export const selector = createStructuredSelector({ - comments: commentsSelector, - user: currentUserSelector, -}); + return createStructuredSelector({ + comments: commentsSelector, + user: currentUserSelector, + }); +} export const actions = { createComment, diff --git a/app/shared/comments/CommentsContainer.spec.js b/app/shared/comments/CommentsContainer.spec.js index 3e5c7305..9ec29b7f 100644 --- a/app/shared/comments/CommentsContainer.spec.js +++ b/app/shared/comments/CommentsContainer.spec.js @@ -21,7 +21,7 @@ function getWrapper(props) { } function getSelected(state, props = { reservationId: 1234 }) { - return selector(getState(state), props); + return selector()(getState(state), props); } describe('shared/comments/CommentsContainer', () => { diff --git a/app/shared/modals/reservation-info/ReservationInfo.js b/app/shared/modals/reservation-info/ReservationInfo.js index 595f663c..361d4763 100644 --- a/app/shared/modals/reservation-info/ReservationInfo.js +++ b/app/shared/modals/reservation-info/ReservationInfo.js @@ -93,6 +93,11 @@ export default function ReservationInfoModal(props) { } +
{reservation.eventDescription && ( diff --git a/app/shared/modals/reservation-info/ReservationInfo.spec.js b/app/shared/modals/reservation-info/ReservationInfo.spec.js index 8e35b897..37180dc2 100644 --- a/app/shared/modals/reservation-info/ReservationInfo.spec.js +++ b/app/shared/modals/reservation-info/ReservationInfo.spec.js @@ -123,6 +123,14 @@ describe('shared/modal/ReservationInfo', () => { expect(comments.prop('name')).to.equal('Varauksen viestit'); expect(comments.prop('reservationId')).to.equal(reservation.id); }); + + it('renders catering comments', () => { + const comments = getBodyWrapper().find('.catering-comments'); + expect(comments).to.have.length(1); + expect(comments.is(Comments)).to.be.true; + expect(comments.prop('name')).to.equal('Tarjoilun viestit'); + expect(comments.prop('cateringId')).to.equal(reservation.id); + }); }); describe('Modal footer', () => { From 509cb892477a1b875e7bed6d84d7d5c82c1a4f38 Mon Sep 17 00:00:00 2001 From: Oskari Hiltunen Date: Tue, 2 May 2017 15:14:16 +0300 Subject: [PATCH 5/5] Small fixes Refs #154. --- app/api/actions/comments.spec.js | 4 ++-- app/shared/comments/CommentsContainer.js | 2 +- app/shared/comments/comments.less | 4 +++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/api/actions/comments.spec.js b/app/api/actions/comments.spec.js index 3a993d4e..94a56f0f 100644 --- a/app/api/actions/comments.spec.js +++ b/app/api/actions/comments.spec.js @@ -8,7 +8,7 @@ describe('api/actions/comments', () => { const reservationId = 3819; createApiTest({ - name: 'fetchComments', + name: 'fetchComments(reservationId)', action: fetchComments, args: [{ reservationId }], tests: { @@ -27,7 +27,7 @@ describe('api/actions/comments', () => { }); createApiTest({ - name: 'fetchComments', + name: 'fetchComments(cateringId)', action: fetchComments, args: [{ cateringId }], tests: { diff --git a/app/shared/comments/CommentsContainer.js b/app/shared/comments/CommentsContainer.js index a2428d70..9db6a910 100644 --- a/app/shared/comments/CommentsContainer.js +++ b/app/shared/comments/CommentsContainer.js @@ -77,7 +77,7 @@ export class CommentsContainer extends React.Component { render() { return (