add SET_MESSAGES to reducer

This commit is contained in:
Sorrel Bri 2020-01-22 15:41:08 -08:00 committed by sorrelbri
parent 6467004a89
commit 695d1ea2b1
6 changed files with 29 additions and 14 deletions

View file

@ -27,7 +27,7 @@ const Home = props => {
const action = { const action = {
type: 'ROOMS', type: 'ROOMS',
message: 'SET_ROOMS', message: 'SET_ROOMS',
body: response body: response.rooms
} }
return dispatch(action) return dispatch(action)
} }

View file

@ -6,8 +6,8 @@ export const messagesReducer = (state: state, action: action):state => {
switch(action.message) { switch(action.message) {
case 'SET_MESSAGES': case 'SET_MESSAGES':
const rooms = parseData(action.body); const messages = action.body;
return {...state, rooms}; return {...state, messages};
default: default:

View file

@ -1,10 +1,20 @@
import {stateReducer} from '../stateReducer'; import {stateReducer} from '../stateReducer';
import { initState } from '../init/stateReducer.init'; 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', () => { it('default returns state unaltered', () => {
const state = initState(); 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); 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});
}) })

View file

@ -25,6 +25,7 @@ export const roomsReducer = (state: state, action: action):state => {
// } // }
// SET GAMES // 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 = { const messageAction = {
type: 'MESSAGE', type: 'MESSAGES',
message: 'SET_MESSAGES', message: 'SET_MESSAGES',
body: data.messages body: body.messages
} }
stateReducer(state, messageAction) return stateReducer(state, messageAction)
} }

View file

@ -47,8 +47,7 @@ it('join room returns state with current room, games and messages all populated'
...state, ...state,
currentRoom: roomsData[0], currentRoom: roomsData[0],
messages: joinRoomData.messages, messages: joinRoomData.messages,
roomGames: [ roomGames: normalizedRoomGames
joinRoomData.roomGames
]
}) })
}); });

View file

@ -4,16 +4,18 @@ import { authReducer } from './auth/stateReducer.auth';
import { errorReducer } from './err/stateReducer.err'; import { errorReducer } from './err/stateReducer.err';
import { indexReducer } from './index/stateReducer.index'; import { indexReducer } from './index/stateReducer.index';
import { roomsReducer } from './rooms/stateReducer.rooms'; import { roomsReducer } from './rooms/stateReducer.rooms';
import { messagesReducer } from './messages/stateReducer.messages';
export type state = { export type state = {
user: {}, user: {},
errors: {} errors: {},
messages: []
} }
export type action = { export type action = {
type: string, type: string,
message: ?string, message: ?string,
body: {}, body: {} | Array<{}>,
} }
export const stateReducer = (state: state, action: action): state => { export const stateReducer = (state: state, action: action): state => {
@ -28,6 +30,9 @@ export const stateReducer = (state: state, action: action): state => {
case 'INDEX': case 'INDEX':
return indexReducer(errorStrippedState, action); return indexReducer(errorStrippedState, action);
case 'MESSAGES':
return messagesReducer(errorStrippedState, action);
case 'ROOMS': case 'ROOMS':
return roomsReducer(errorStrippedState, action); return roomsReducer(errorStrippedState, action);