Skip to content

Commit f151c06

Browse files
committed
reselect selectors
1 parent bc22661 commit f151c06

File tree

19 files changed

+68
-104
lines changed

19 files changed

+68
-104
lines changed

lib/modules/tutorial/reducer.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function tutorialReducer(tutorial, action) {
1919
var packageJson = require(path_1.join(packagePath, 'package.json'));
2020
var config = config_1.tutorialConfig(packageJson, dir);
2121
var _b = require(path_1.join(packagePath, packageJson.main)), info = _b.info, pages = _b.pages;
22+
console.log(packageJson, packageJson.name);
2223
pages = config_paths_1.default(dir, title, config, pages || []);
2324
return {
2425
title: packageJson.name,

lib/selectors.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"use strict";

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"redux": "3.5.2",
4343
"redux-logger": "2.6.1",
4444
"redux-throttle-actions": "1.0.1",
45-
"redux-thunk": "2.1.0"
45+
"redux-thunk": "2.1.0",
46+
"reselect": "^2.5.1"
4647
},
4748
"engines": {
4849
"atom": ">=1.0.0 <2.0.0"

src/modules/page/Continue/index.tsx renamed to src/components/Page/Continue/index.tsx

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import {connect} from 'react-redux';
33
import RaisedButton from 'material-ui/RaisedButton';
4-
import {pageNext} from '../actions';
4+
import {pageNext} from '../../../actions';
55

66
const styles = {
77
zIndex: '10000',
@@ -12,11 +12,9 @@ const styles = {
1212
top: '10px',
1313
};
1414

15-
@connect(null, (dispatch, state) => {
16-
return {
17-
callNextPage: () => dispatch(pageNext())
18-
};
19-
})
15+
@connect(null, dispatch => ({
16+
callNextPage: () => dispatch(pageNext())
17+
}))
2018
export default class Continue extends React.Component<{
2119
callNextPage?: any
2220
}, {}> {

src/components/Page/Tasks/index.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import Task from '../Task';
77
import {lightGreen200} from 'material-ui/styles/colors';
88
import TasksComplete from '../TasksComplete';
99

10-
function visibleTasks(tasks: CR.Task[], taskPosition: number): CR.Task[] {
11-
return tasks.slice(0, taskPosition + 1);
12-
}
13-
1410
const margin = '10px 5px';
1511

1612
export default class Tasks extends React.Component<{
@@ -24,15 +20,19 @@ export default class Tasks extends React.Component<{
2420
componentDidUpdate() {
2521
ReactDOM.findDOMNode<HTMLElement>(this.refs.listEnd).scrollIntoView();
2622
}
23+
visibleTasks(tasks: CR.Task[], taskPosition: number): CR.Task[] {
24+
return tasks.slice(0, taskPosition + 1);
25+
}
2726
render() {
2827
const {tasks, taskPosition, testRun, completed, page} = this.props;
29-
const visTasks = visibleTasks(tasks, taskPosition);
28+
const visTasks = this.visibleTasks(tasks, taskPosition);
3029
const backgroundColor = completed ? lightGreen200 : 'white';
3130
return (
3231
<div>
3332
<Card style={{backgroundColor, margin}}>
3433
<List>
3534
<Subheader>Tasks</Subheader>
35+
3636
{visTasks.map((task: CR.Task, index: number) => (
3737
<Task
3838
key={index}
@@ -42,6 +42,7 @@ export default class Tasks extends React.Component<{
4242
testRun={testRun}
4343
/>)
4444
)}
45+
4546
</List>
4647
</Card>
4748

src/components/Page/index.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import Tasks from './Tasks';
77
import Hints from './Hints';
88
import PageToolbar from './PageToolbar';
99
import ProgressBar from './ProgressBar';
10+
import {pageSelector, tasksSelector} from '../../selectors';
1011

1112
const styles = {
1213
width: '100%',
1314
overflowY: 'scroll',
1415
};
1516

1617
@connect(state => ({
17-
page: state.page,
18-
tasks: state.tasks,
19-
taskPosition: state.taskPosition,
20-
hintPosition: state.hintPosition,
18+
page: pageSelector(state),
19+
tasks: tasksSelector(state),
2120
testRun: state.testRun,
2221
progress: state.progress,
22+
taskPosition: state.taskPosition,
23+
hintPosition: state.hintPosition,
2324
pagePosition: state.pagePosition,
2425
}))
2526
export default class Page extends React.Component<{

src/components/Tutorials/SelectTutorial/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function displayName(name: string): string {
1616
selectTutorial: (title: string) => dispatch(tutorialSet(name)),
1717
}))
1818
export default class SelectTutorial extends React.Component<{
19-
tutorial: Tutorial.Info, selectTutorial?: any
19+
tutorial: Tutorial.Info, selectTutorial?: any
2020
}, {}> {
2121
render() {
2222
const {tutorial, selectTutorial} = this.props;

src/modules/package-json/actions.ts

Whitespace-only changes.

src/modules/package-json/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default as reducer} from './reducer';

src/modules/package-json/reducer.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {PJ_SAVE} from './types';
2+
import {readPackageJson, writePackageJson} from './utils/packageJson';
3+
import {sortPackageJson} from 'sort-package-json';
4+
5+
const _pj = null;
6+
7+
export default function packageJson(
8+
p = _pj, action: Action
9+
): Tutorial.PJ {
10+
switch (action.type) {
11+
12+
default:
13+
return p;
14+
}
15+
}

src/modules/package-json/types.ts

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {join} from 'path';
2+
import {readFileSync} from 'fs';
3+
import fileExists from 'node-file-exists';
4+
5+
export function readPackageJson(dir: string): Object|boolean {
6+
const pathToPJ = join(dir, './package.json');
7+
if (!fileExists(pathToPJ)) { return false; }
8+
try {
9+
return JSON.parse(readFileSync(pathToPJ, 'utf8'));
10+
} catch (e) {
11+
return null;
12+
}
13+
}

src/modules/page/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
export {default as page} from './page';
21
export {default as pagePosition} from './page-position';
32
export {default as taskActions} from './task-actions';
43
export {default as taskTests} from './task-tests';
5-
export {default as tasks} from './tasks';
64

75
export {default as Continue} from './Continue';

src/modules/page/page/index.ts

-28
This file was deleted.

src/modules/page/tasks/index.ts

-21
This file was deleted.

src/modules/page/tasks/tasks.spec.ts

-32
This file was deleted.

src/modules/tutorial/reducer.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default function tutorialReducer(
2222
const packageJson: PackageJson = require(join(packagePath, 'package.json'));
2323
const config: Tutorial.Config = tutorialConfig(packageJson, dir);
2424
let {info, pages} = require(join(packagePath, packageJson.main));
25+
console.log(packageJson, packageJson.name);
2526
// configure test paths to absolute paths
2627
pages = configPaths(dir, title, config, pages || []);
2728
return {

src/selectors.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {createSelector} from 'reselect';
2+
3+
export const pageSelector = state => state.tutorial.pages[state.pagePosition];
4+
5+
export const tasksSelector = createSelector(
6+
pageSelector,
7+
page => page.tasks
8+
);
9+
10+
export const configSelector = state => state.packageJson.config;

tsconfig.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@
3131
"src/modules/hints/index.ts",
3232
"src/modules/hints/reducer.ts",
3333
"src/modules/hints/types.ts",
34+
"src/modules/package-json/actions.ts",
35+
"src/modules/package-json/index.ts",
36+
"src/modules/package-json/reducer.ts",
37+
"src/modules/package-json/types.ts",
38+
"src/modules/package-json/utils/packageJson.ts",
3439
"src/modules/page/actions.ts",
3540
"src/modules/page/index.ts",
3641
"src/modules/page/page-position/index.ts",
37-
"src/modules/page/page/index.ts",
3842
"src/modules/page/task-actions/handle-action-string.ts",
3943
"src/modules/page/task-actions/handle-actions.ts",
4044
"src/modules/page/task-actions/index.ts",
4145
"src/modules/page/task-actions/parse-params.ts",
4246
"src/modules/page/task-actions/parser.ts",
4347
"src/modules/page/task-tests/index.ts",
44-
"src/modules/page/tasks/index.ts",
4548
"src/modules/page/types.ts",
4649
"src/modules/progress/actions.ts",
4750
"src/modules/progress/index.ts",
@@ -79,6 +82,7 @@
7982
"src/modules/tutorials/reducer.ts",
8083
"src/modules/tutorials/types.ts",
8184
"src/reducers.ts",
85+
"src/selectors.ts",
8286
"src/store.ts",
8387
"src/subscriptions.ts",
8488
"src/typings.d.ts",
@@ -98,6 +102,7 @@
98102
"src/components/common/Save.tsx",
99103
"src/components/common/ToggleDevTools.tsx",
100104
"src/components/FinalPage/index.tsx",
105+
"src/components/Page/Continue/index.tsx",
101106
"src/components/Page/EditPage/index.tsx",
102107
"src/components/Page/Hints/HintButton.tsx",
103108
"src/components/Page/Hints/index.tsx",
@@ -127,8 +132,7 @@
127132
"src/components/Tutorials/SelectTutorial/index.tsx",
128133
"src/components/Tutorials/UpdateTutorial/index.tsx",
129134
"src/modules/hints/Hints/HintButton.tsx",
130-
"src/modules/hints/Hints/index.tsx",
131-
"src/modules/page/Continue/index.tsx"
135+
"src/modules/hints/Hints/index.tsx"
132136
],
133137
"exclude": [
134138
"node_modules"

0 commit comments

Comments
 (0)