From c3eb7fe697fe3c3511d0cc7e114b0023ac803b95 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Wed, 22 Jan 2020 15:41:08 -0800 Subject: [PATCH] add SET_MESSAGES to reducer --- play-node-go/src/pages/Home/Home.js | 2 +- .../src/reducers/messages/stateReducer.messages.js | 4 ++-- .../messages/stateReducer.messages.test.js | 14 ++++++++++++-- .../src/reducers/rooms/stateReducer.rooms.js | 9 +++++---- .../src/reducers/rooms/stateReducer.rooms.test.js | 5 ++--- play-node-go/src/reducers/stateReducer.js | 9 +++++++-- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/play-node-go/src/pages/Home/Home.js b/play-node-go/src/pages/Home/Home.js index 56050ac..b5df138 100644 --- a/play-node-go/src/pages/Home/Home.js +++ b/play-node-go/src/pages/Home/Home.js @@ -27,7 +27,7 @@ const Home = props => { const action = { type: 'ROOMS', message: 'SET_ROOMS', - body: response + body: response.rooms } return dispatch(action) } diff --git a/play-node-go/src/reducers/messages/stateReducer.messages.js b/play-node-go/src/reducers/messages/stateReducer.messages.js index 431c38d..d795b29 100644 --- a/play-node-go/src/reducers/messages/stateReducer.messages.js +++ b/play-node-go/src/reducers/messages/stateReducer.messages.js @@ -6,8 +6,8 @@ export const messagesReducer = (state: state, action: action):state => { switch(action.message) { case 'SET_MESSAGES': - const rooms = parseData(action.body); - return {...state, rooms}; + const messages = action.body; + return {...state, messages}; default: diff --git a/play-node-go/src/reducers/messages/stateReducer.messages.test.js b/play-node-go/src/reducers/messages/stateReducer.messages.test.js index 1c0ee82..25b45cd 100644 --- a/play-node-go/src/reducers/messages/stateReducer.messages.test.js +++ b/play-node-go/src/reducers/messages/stateReducer.messages.test.js @@ -1,10 +1,20 @@ import {stateReducer} from '../stateReducer'; import { initState } from '../init/stateReducer.init'; -const messagesData = []; +const messagesData = [ + { + "content": "Hey! Welcome to the general room!", "username": "userOne", "admin": true + } +]; it('default returns state unaltered', () => { const state = initState(); - const action = {type: 'MESSAGES', message: '', body: JSON.stringify(messagesData)}; + const action = {type: 'MESSAGES', message: '', body: messagesData}; expect(stateReducer(state, action)).toEqual(state); +}) + +it('default returns state unaltered', () => { + const state = initState(); + const action = {type: 'MESSAGES', message: 'SET_MESSAGES', body: messagesData}; + expect(stateReducer(state, action)).toEqual({...state, messages: messagesData}); }) \ No newline at end of file diff --git a/play-node-go/src/reducers/rooms/stateReducer.rooms.js b/play-node-go/src/reducers/rooms/stateReducer.rooms.js index d41de3a..a77c30e 100644 --- a/play-node-go/src/reducers/rooms/stateReducer.rooms.js +++ b/play-node-go/src/reducers/rooms/stateReducer.rooms.js @@ -25,6 +25,7 @@ export const roomsReducer = (state: state, action: action):state => { // } // SET GAMES + return stateWithMessages; } @@ -33,11 +34,11 @@ export const roomsReducer = (state: state, action: action):state => { } } -function setMessages(state, data) { +function setMessages(state, body) { const messageAction = { - type: 'MESSAGE', + type: 'MESSAGES', message: 'SET_MESSAGES', - body: data.messages + body: body.messages } - stateReducer(state, messageAction) + return stateReducer(state, messageAction) } \ No newline at end of file diff --git a/play-node-go/src/reducers/rooms/stateReducer.rooms.test.js b/play-node-go/src/reducers/rooms/stateReducer.rooms.test.js index 254486b..fe8b255 100644 --- a/play-node-go/src/reducers/rooms/stateReducer.rooms.test.js +++ b/play-node-go/src/reducers/rooms/stateReducer.rooms.test.js @@ -47,8 +47,7 @@ it('join room returns state with current room, games and messages all populated' ...state, currentRoom: roomsData[0], messages: joinRoomData.messages, - roomGames: [ - joinRoomData.roomGames - ] + roomGames: normalizedRoomGames + }) }); \ No newline at end of file diff --git a/play-node-go/src/reducers/stateReducer.js b/play-node-go/src/reducers/stateReducer.js index 1bc49a5..1b27ee7 100644 --- a/play-node-go/src/reducers/stateReducer.js +++ b/play-node-go/src/reducers/stateReducer.js @@ -4,16 +4,18 @@ import { authReducer } from './auth/stateReducer.auth'; import { errorReducer } from './err/stateReducer.err'; import { indexReducer } from './index/stateReducer.index'; import { roomsReducer } from './rooms/stateReducer.rooms'; +import { messagesReducer } from './messages/stateReducer.messages'; export type state = { user: {}, - errors: {} + errors: {}, + messages: [] } export type action = { type: string, message: ?string, - body: {}, + body: {} | Array<{}>, } export const stateReducer = (state: state, action: action): state => { @@ -28,6 +30,9 @@ export const stateReducer = (state: state, action: action): state => { case 'INDEX': return indexReducer(errorStrippedState, action); + case 'MESSAGES': + return messagesReducer(errorStrippedState, action); + case 'ROOMS': return roomsReducer(errorStrippedState, action);