From 3cb7c15d15f33e9b0ce06dbf0072021f6921dbe7 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Tue, 20 Mar 2018 16:02:22 +0100 Subject: Rewrite users.js without using redux --- src/client/react/users.js | 65 +++++++---------------------------------------- 1 file changed, 9 insertions(+), 56 deletions(-) (limited to 'src/client/react') diff --git a/src/client/react/users.js b/src/client/react/users.js index 5bdf12f..492bf60 100644 --- a/src/client/react/users.js +++ b/src/client/react/users.js @@ -20,68 +20,21 @@ /* global USERS */ -import { combineReducers, createStore } from 'redux'; +import keyBy from 'lodash/keyBy'; const getId = ({ type, value }) => `${type}/${value}`; -const byId = (state = {}, action) => { - switch (action.type) { - case 'USERS/ADD_USER': - return { - ...state, - [action.user.id]: { - ...action.user, - }, - }; - default: - return state; - } -}; +const users = {}; -const allIds = (state = [], action) => { - switch (action.type) { - case 'USERS/ADD_USER': - return [ - ...state, - action.user.id, - ]; - default: - return state; - } -}; - -const allUsers = (state = [], action) => { - switch (action.type) { - case 'USERS/ADD_USER': - return [ - ...state, - { - ...action.user, - }, - ]; - default: - return state; - } -}; - -const store = createStore(combineReducers({ - byId, - allIds, - allUsers, +users.allUsers = USERS.map(user => ({ + type: user.type, + value: user.value, + alt: user.alt, + id: getId(user), })); -USERS.forEach((user) => { - store.dispatch({ - type: 'USERS/ADD_USER', - user: { - type: user.type, - value: user.value, - alt: user.alt, - id: getId(user), - }, - }); -}); +users.allIds = users.allUsers.map(user => user.id); -const users = store.getState(); +users.byId = keyBy(users.allUsers, 'id'); export default users; -- cgit v1.1