add SET_ROOMS dispatch

This commit is contained in:
Sorrel Bri 2020-01-18 16:17:23 -08:00 committed by sorrelbri
parent d81be0b595
commit 1b7f802013
4 changed files with 43 additions and 2 deletions

View file

@ -4,7 +4,7 @@ import type { state } from '../stateReducer';
export const initState = (): state => { export const initState = (): state => {
return { return {
user: null, user: {},
errors: null errors: {}
}; };
} }

View file

@ -0,0 +1,19 @@
// @flow
import type { state, action } from '../stateReducer';
export const roomsReducer = (state: state, action: action):state => {
switch(action.message) {
case 'SET_ROOMS':
const rooms = roomsParse(action.body);
return {...state, rooms};
default:
return state;
}
}
function roomsParse(roomsData) {
const rooms = JSON.parse(roomsData);
return rooms
}

View file

@ -0,0 +1,18 @@
import {stateReducer} from '../stateReducer';
import { initState } from '../init/stateReducer.init';
const roomsData = [
{
description: "A general place to play Go",
id: 1,
language: "EN",
name: "main"
}
]
it('default returns state with rooms added', () => {
const state = initState();
const action = {type: 'ROOMS', message: 'SET_ROOMS', body: JSON.stringify(roomsData)};
expect(stateReducer(state, action)).toEqual({...state, rooms: roomsData});
});

View file

@ -3,6 +3,7 @@ import { initState } from './init/stateReducer.init';
import { authReducer } from './auth/stateReducer.auth'; 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';
export type state = { export type state = {
user: {}, user: {},
@ -27,6 +28,9 @@ export const stateReducer = (state: state, action: action): state => {
case 'INDEX': case 'INDEX':
return indexReducer(errorStrippedState, action); return indexReducer(errorStrippedState, action);
case 'ROOMS':
return roomsReducer(errorStrippedState, action);
case 'ERR': case 'ERR':
return errorReducer(errorStrippedState, action); return errorReducer(errorStrippedState, action);