add SET_MESSAGES to reducer

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

View file

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

View file

@ -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:

View file

@ -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});
})

View file

@ -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)
}

View file

@ -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
})
});

View file

@ -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);