Skip to content

Instantly share code, notes, and snippets.

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];
}
{
"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",
export function isTopicSelectionValid(state) {
return state.topics.selectedTopicUrls.length === 3;
}
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 });
};
}
import _ from 'lodash';
export default function reduce(state = initialState, action = {}) {
// reducer implementation here
}
// selectors
export function getTopicsByUrl(state) {
return state.topics.topicsByUrl;
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?
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) {
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 });
{
"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"
}
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));