From f262b2c90b5374d95ce4104e8abf10f3d4555d10 Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Tue, 18 Oct 2016 06:00:55 -0700 Subject: [PATCH] Convert entity collection stores to objects instead of arrays (#25) * Use camel case for current team & channel ids * Convert entity collection stores to objects --- src/actions/channels.js | 2 +- src/actions/teams.js | 2 +- src/components/channels_list.js | 8 ++++---- src/components/posts_list.js | 14 +++++++------- src/reducers/channels.js | 12 +++++++++--- src/reducers/posts.js | 5 ++--- src/reducers/teams.js | 7 +++---- 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/actions/channels.js b/src/actions/channels.js index 9a6fe8146..741c7ff8c 100644 --- a/src/actions/channels.js +++ b/src/actions/channels.js @@ -8,7 +8,7 @@ import {ChannelsTypes as types} from 'constants'; export function selectChannel(channel) { return { type: types.SELECT_CHANNEL, - channel_id: channel.id + channelId: channel.id }; } diff --git a/src/actions/teams.js b/src/actions/teams.js index f869d76e2..649fdaaaa 100644 --- a/src/actions/teams.js +++ b/src/actions/teams.js @@ -9,7 +9,7 @@ export function selectTeam(team) { Client.setTeamId(team.id); return { type: types.SELECT_TEAM, - team_id: team.id + teamId: team.id }; } diff --git a/src/components/channels_list.js b/src/components/channels_list.js index 970597f82..c2a27a2d6 100644 --- a/src/components/channels_list.js +++ b/src/components/channels_list.js @@ -7,11 +7,10 @@ import {connect} from 'react-redux'; import {ListView, StyleSheet, View, Text} from 'react-native'; const {DataSource} = ListView; -import {Actions as Routes} from 'react-native-router-flux'; // eslint-disable-line no-unused-vars +import {Actions as Routes} from 'react-native-router-flux'; +import _ from 'lodash'; import * as channelActions from 'actions/channels'; - -// import _ from 'lodash'; import ErrorText from 'components/error_text'; const styles = StyleSheet.create({ @@ -49,10 +48,11 @@ class ChannelsList extends Component { } render() { + const channels = _.values(this.props.channels.data); return ( ( this.props.actions.selectChannel(channel)}> diff --git a/src/components/posts_list.js b/src/components/posts_list.js index 374ee2b23..cfa3d2531 100644 --- a/src/components/posts_list.js +++ b/src/components/posts_list.js @@ -6,6 +6,7 @@ import React, {Component, PropTypes} from 'react'; import {bindActionCreators} from 'redux'; import {connect} from 'react-redux'; import {StyleSheet, Text, View} from 'react-native'; +import _ from 'lodash'; import * as postActions from 'actions/posts'; import ErrorText from 'components/error_text'; @@ -38,15 +39,14 @@ class PostsList extends Component { } render() { + const posts = _.values(this.props.posts.data); return ( - {this.props.posts.data.map((post) => { - return ( - - {`${post.user_id} - ${post.message}`} - - ); - })} + {posts.map((post) => ( + + {`${post.user_id} - ${post.message}`} + + ))} ); diff --git a/src/reducers/channels.js b/src/reducers/channels.js index 87ba25759..803a05339 100644 --- a/src/reducers/channels.js +++ b/src/reducers/channels.js @@ -1,18 +1,24 @@ +import _ from 'lodash'; import {ChannelsTypes as types} from 'constants'; const initState = { status: 'not fetched', error: null, - data: [], + data: {}, currentChannelId: null }; +// TODO: this can be extracted into a util +function zipById(channels) { + return _.zipObject(_.map(channels, 'id'), channels); +} + export default function reduceChannels(state = initState, action) { switch (action.type) { case types.SELECT_CHANNEL: return {...state, - currentChannelId: action.channel_id + currentChannelId: action.channelId }; case types.FETCH_CHANNELS_REQUEST: @@ -23,7 +29,7 @@ export default function reduceChannels(state = initState, action) { case types.FETCH_CHANNELS_SUCCESS: return {...state, status: 'fetched', - data: action.data.channels + data: zipById(action.data.channels) }; case types.FETCH_CHANNELS_FAILURE: return {...state, diff --git a/src/reducers/posts.js b/src/reducers/posts.js index cc80d2f86..eaab25913 100644 --- a/src/reducers/posts.js +++ b/src/reducers/posts.js @@ -1,10 +1,9 @@ -import _ from 'lodash'; import {PostsTypes as types} from 'constants'; const initState = { status: 'not fetched', error: null, - data: [] + data: {} }; export default function reducePosts(state = initState, action) { @@ -18,7 +17,7 @@ export default function reducePosts(state = initState, action) { case types.FETCH_POSTS_SUCCESS: return {...state, status: 'fetched', - data: _.values(action.data.posts) + data: action.data.posts }; case types.FETCH_POSTS_FAILURE: return {...state, diff --git a/src/reducers/teams.js b/src/reducers/teams.js index 188bf9faf..a8324a745 100644 --- a/src/reducers/teams.js +++ b/src/reducers/teams.js @@ -1,10 +1,9 @@ -import _ from 'lodash'; import {TeamsTypes as types} from 'constants'; const initState = { status: 'not fetched', error: null, - data: [], + data: {}, currentTeamId: null }; @@ -13,7 +12,7 @@ export default function reduceTeams(state = initState, action) { case types.SELECT_TEAM: return {...state, - currentTeamId: action.team_id + currentTeamId: action.teamId }; case types.FETCH_TEAMS_REQUEST: @@ -24,7 +23,7 @@ export default function reduceTeams(state = initState, action) { case types.FETCH_TEAMS_SUCCESS: return {...state, status: 'fetched', - data: _.values(action.data) + data: action.data }; case types.FETCH_TEAMS_FAILURE: return {...state,