Skip to content

Commit

Permalink
tasks state
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-casey committed Nov 29, 2016
1 parent 92b2ae0 commit 0c65806
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 10 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@types/react-router": "^2.0.39",
"@types/redux": "^3.6.31",
"@types/redux-logger": "^2.6.32",
"@types/reselect": "^2.0.27",
"autoprefixer": "6.5.1",
"babel-core": "6.17.0",
"babel-eslint": "7.0.0",
Expand Down Expand Up @@ -68,7 +69,8 @@
"react-router": "^3.0.0",
"redux": "^3.6.0",
"redux-logger": "^2.7.4",
"redux-persist": "^4.0.0-beta1"
"redux-persist": "^4.0.0-beta1",
"reselect": "^2.5.4"
},
"scripts": {
"start": "node scripts/start.js",
Expand Down
3 changes: 2 additions & 1 deletion src/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import * as logger from './redux/logger';
import * as persist from './redux/persist';

// Reducers
import { AppState } from './modules';
import { AppState, tasks } from './modules';

const middleware = applyMiddleware(logger.middleware);

const setupStore = () => {
const rootReducer = combineReducers<AppState>({
[tasks.KEY]: tasks.reducer,
[persist.KEY]: persist.reducer,
});

Expand Down
10 changes: 9 additions & 1 deletion src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export interface AppState {}
import * as tasks from './tasks';

export interface AppState {
tasks: tasks.State;
}

export {
tasks,
}
69 changes: 69 additions & 0 deletions src/modules/tasks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import * as reselect from 'reselect';

import { Action } from '../../redux/action';

type Activities = 'door knocking' | 'phone banking' | 'rally';

type Tags = 'open internet' | 'privacy';

type Location = {
latitude?: number;
longitude?: number;
}

type Step = {
name: string;
template: string;
data: { [index: string]: any }
}

export interface Task {
id: number;
name: string;
type: Activities[];
tags: Tags[];
duration: number;
startDate: Date;
endDate: Date;
location: Location;
notes: string;
steps: Step[];
}

export interface State {
list: Task[];
}

export const KEY = 'tasks';

export const actions = {
SET_LIST: 'civic/tasks/SET_LIST',
};

export const actionCreators = {
setList: (list: Task[]) => ({ type: actions.SET_LIST, payload: list }),
};

const initialState: State = {
list: [],
};

export const reducer: Redux.Reducer<State> = (state = initialState, action: Action) => {
switch (action.type) {
case actions.SET_LIST:
return {...state, list: action.payload };

default:
return state;
}
};

const getState = (state: any): State => state[KEY];
const getList = reselect.createSelector(getState, state => state.list);
const getTaskId = (_state: any, { taskId }: { taskId: number }) => taskId;
const getTask = reselect.createSelector(getList, getTaskId, (list, taskId) => list.find(task => task.id === taskId));
export const selectors = {
getState,
getList,
getTask,
};
22 changes: 15 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
version "3.6.31"
resolved "https://registry.yarnpkg.com/@types/redux/-/redux-3.6.31.tgz#40eafa7575db36b912ce0059b85de98c205b0708"

"@types/reselect@^2.0.27":
version "2.0.27"
resolved "https://registry.yarnpkg.com/@types/reselect/-/reselect-2.0.27.tgz#78a53338a99bb1bc4b6aea0e1a9315a0e37e7383"

Base64@~0.2.0:
version "0.2.1"
resolved "https://registry.yarnpkg.com/Base64/-/Base64-0.2.1.tgz#ba3a4230708e186705065e66babdd4c35cf60028"
Expand Down Expand Up @@ -4829,22 +4833,22 @@ readable-stream@^1.0.27-1, readable-stream@^1.1.13:
isarray "0.0.1"
string_decoder "~0.10.x"

readable-stream@^2.0.0, readable-stream@^2.0.5, readable-stream@~2.1.4:
version "2.1.5"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.1.5.tgz#66fa8b720e1438b364681f2ad1a63c618448c9d0"
readable-stream@^2.0.0, "readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@~2.0.0:
version "2.0.6"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e"
dependencies:
buffer-shims "^1.0.0"
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "~1.0.0"
process-nextick-args "~1.0.6"
string_decoder "~0.10.x"
util-deprecate "~1.0.1"

"readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@~2.0.0:
version "2.0.6"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e"
readable-stream@~2.1.4:
version "2.1.5"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.1.5.tgz#66fa8b720e1438b364681f2ad1a63c618448c9d0"
dependencies:
buffer-shims "^1.0.0"
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "~1.0.0"
Expand Down Expand Up @@ -5054,6 +5058,10 @@ [email protected], [email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"

reselect@^2.5.4:
version "2.5.4"
resolved "https://registry.yarnpkg.com/reselect/-/reselect-2.5.4.tgz#b7d23fdf00b83fa7ad0279546f8dbbbd765c7047"

resolve-from@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226"
Expand Down

0 comments on commit 0c65806

Please sign in to comment.