This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function getPosts(state) { | |
| const currentFilter = state.posts.currentFilter; | |
| const postsById = state.posts.postsById; | |
| const postsIdArray = currentFilter === 'all' ? | |
| _.keys(postsById) : | |
| _.filter(_.keys(postsById), (postId) => postsById[postId].topicUrl === currentFilter); | |
| return [postsById, postsIdArray]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "postsById": { | |
| "57jrtt": { | |
| "title": "My girlfriend left me because she couldn't handle my OCD.", | |
| "topicUrl": "/r/Jokes", | |
| "body": "I told her to close the door five times on her way out.", | |
| }, | |
| "57l6oa": { | |
| "title": "Inception style vertical panoramas done with a quadcopter.", | |
| "topicUrl": "/r/pics", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function isTopicSelectionValid(state) { | |
| return state.topics.selectedTopicUrls.length === 3; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function selectTopic(topicUrl) { | |
| return (dispatch, getState) => { | |
| const selectedTopics = topicsSelectors.getSelectedTopicUrls(getState()); | |
| if (_.indexOf(selectedTopics, topicUrl) !== -1) return; | |
| const newSelectedTopics = selectedTopics.length < 3 ? | |
| selectedTopics.concat(topicUrl) : | |
| selectedTopics.slice(1).concat(topicUrl); | |
| dispatch({ type: types.TOPICS_SELECTED, selectedTopicUrls: newSelectedTopics }); | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import _ from 'lodash'; | |
| export default function reduce(state = initialState, action = {}) { | |
| // reducer implementation here | |
| } | |
| // selectors | |
| export function getTopicsByUrl(state) { | |
| return state.topics.topicsByUrl; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from 'react'; | |
| import { connect } from 'react-redux'; | |
| import * as topicsActions from '../store/topics/actions'; | |
| import * as topicsSelectors from '../store/topics/reducer'; | |
| class TopicsScreen extends Component { | |
| // view implementation here | |
| } | |
| // which props do we want to inject, given the global store state? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as types from './actionTypes'; | |
| import Immutable from 'seamless-immutable'; | |
| const initialState = Immutable({ | |
| topicsByUrl: undefined, | |
| selectedTopicUrls: [] | |
| }); | |
| export default function reduce(state = initialState, action = {}) { | |
| switch (action.type) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import _ from 'lodash'; | |
| import * as types from './actionTypes'; | |
| import redditService from '../../services/reddit'; | |
| export function fetchTopics() { | |
| return async(dispatch, getState) => { | |
| try { | |
| const subredditArray = await redditService.getDefaultSubreddits(); | |
| const topicsByUrl = _.keyBy(subredditArray, (subreddit) => subreddit.url); | |
| dispatch({ type: types.TOPICS_FETCHED, topicsByUrl }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "topicsByUrl": { | |
| "/r/Jokes/": { | |
| "title": "Jokes", | |
| "description": "The funniest sub on reddit. Hundreds of jokes posted each day, and some of them aren't even reposts! FarCraft" | |
| }, | |
| "/r/pics/": { | |
| "title": "pics", | |
| "description": "I bet you can figure it out by reading the name of the subreddit" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| import ReactDOM from 'react-dom'; | |
| import { createStore, applyMiddleware, combineReducers } from 'redux'; | |
| import { Provider } from 'react-redux'; | |
| import thunk from 'redux-thunk'; | |
| import App from './App'; | |
| import './index.css'; | |
| import * as reducers from './store/reducers'; | |
| const store = createStore(combineReducers(reducers), applyMiddleware(thunk)); |