Skip to content

Commit f9edcf0

Browse files
committed
connect components with reselect
1 parent 65cfee5 commit f9edcf0

File tree

14 files changed

+59
-41
lines changed

14 files changed

+59
-41
lines changed

lib/components/Page/Tasks/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ var react_redux_1 = require('react-redux');
1818
var Card_1 = require('material-ui/Card');
1919
var Tabs_1 = require('material-ui/Tabs');
2020
var Task_1 = require('../Task');
21-
var TasksComplete_1 = require('../TasksComplete');
2221
var Tests_1 = require('../Tests');
2322
var TaskActions_1 = require('../TaskActions');
2423
var Hints_1 = require('../Hints');
2524
var AddButton_1 = require('../AddButton');
2625
var actions_1 = require('../../../actions');
26+
var selectors_1 = require('../../../selectors');
2727
var styles = {
2828
card: {
2929
margin: '5px',
@@ -54,11 +54,13 @@ var Tasks = (function (_super) {
5454
}
5555
Tasks.prototype.render = function () {
5656
var _this = this;
57-
var _a = this.props, tasks = _a.tasks, page = _a.page, config = _a.config, taskAdd = _a.taskAdd, markdownOpen = _a.markdownOpen, pagePosition = _a.pagePosition;
58-
return (React.createElement("div", null, tasks.map(function (task, index) { return (React.createElement(Card_1.Card, {key: index.toString(), style: styles.card, initiallyExpanded: index === 0}, React.createElement(Card_1.CardHeader, {actAsExpander: true, showExpandableButton: true}, React.createElement("span", {style: styles.title}, "Task ", index + 1), React.createElement(Tests_1.default, {style: styles.test, tests: task.tests, config: config})), React.createElement(Card_1.CardText, {expandable: true, style: styles.cardContent}, React.createElement(Tabs_1.Tabs, {tabItemContainerStyle: styles.tabBar}, React.createElement(Tabs_1.Tab, {label: 'Description'}, React.createElement("div", {onClick: markdownOpen.bind(_this, task.description)}, React.createElement(Task_1.default, {key: index.toString(), index: index, task: task}))), React.createElement(Tabs_1.Tab, {label: 'Actions'}, React.createElement(TaskActions_1.default, {actions: task.actions, taskPosition: index})), React.createElement(Tabs_1.Tab, {label: 'Hints'}, React.createElement(Hints_1.default, {hints: task.hints, taskPosition: index})))))); }), React.createElement(AddButton_1.default, {callback: taskAdd}), React.createElement(TasksComplete_1.default, {page: page})));
57+
var _a = this.props, tasks = _a.tasks, taskAdd = _a.taskAdd, markdownOpen = _a.markdownOpen;
58+
return (React.createElement("div", null, tasks.map(function (task, index) { return (React.createElement(Card_1.Card, {key: index.toString(), style: styles.card, initiallyExpanded: index === 0}, React.createElement(Card_1.CardHeader, {actAsExpander: true, showExpandableButton: true}, React.createElement("span", {style: styles.title}, "Task ", index + 1), React.createElement(Tests_1.default, {style: styles.test, tests: task.tests})), React.createElement(Card_1.CardText, {expandable: true, style: styles.cardContent}, React.createElement(Tabs_1.Tabs, {tabItemContainerStyle: styles.tabBar}, React.createElement(Tabs_1.Tab, {label: 'Description'}, React.createElement("div", {onClick: markdownOpen.bind(_this, task.description)}, React.createElement(Task_1.default, {key: index.toString(), index: index, task: task}))), React.createElement(Tabs_1.Tab, {label: 'Actions'}, React.createElement(TaskActions_1.default, {actions: task.actions, taskPosition: index})), React.createElement(Tabs_1.Tab, {label: 'Hints'}, React.createElement(Hints_1.default, {hints: task.hints, taskPosition: index})))))); }), React.createElement(AddButton_1.default, {callback: taskAdd})));
5959
};
6060
Tasks = __decorate([
61-
react_redux_1.connect(null, function (dispatch) { return ({
61+
react_redux_1.connect(function (state) { return ({
62+
tasks: selectors_1.tasksSelector(state),
63+
}); }, function (dispatch) { return ({
6264
taskAdd: function () { return dispatch(actions_1.tutorialTaskAdd()); },
6365
markdownOpen: function (content) {
6466
dispatch(actions_1.editorMarkdownOpen(null, content));

lib/components/Page/Tests/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var path_1 = require('path');
1919
var actions_1 = require('../../../actions');
2020
var config_options_1 = require('../../../config-options');
2121
var FlatButton_1 = require('material-ui/FlatButton');
22+
var selectors_1 = require('../../../selectors');
2223
var styles = {
2324
button: {
2425
bottom: '8px',
@@ -36,7 +37,9 @@ var Tests = (function (_super) {
3637
return (React.createElement("div", {style: style}, tests.map(function (test, index) { return (React.createElement(FlatButton_1.default, {key: index.toString(), style: styles.button, onClick: open.bind(_this, test.concat('.' + suffix)), label: 'Test', secondary: true})); })));
3738
};
3839
Tests = __decorate([
39-
react_redux_1.connect(null, function (dispatch) { return ({
40+
react_redux_1.connect(function (state) { return ({
41+
config: selectors_1.configSelector(state),
42+
}); }, function (dispatch) { return ({
4043
open: function (file) {
4144
dispatch(actions_1.editorOpen(path_1.join('tutorial', file)));
4245
},

lib/components/Page/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ var React = require('react');
1717
var react_redux_1 = require('react-redux');
1818
var PageDescription_1 = require('./PageDescription');
1919
var Tasks_1 = require('./Tasks');
20+
var TasksComplete_1 = require('./TasksComplete');
2021
var Top_1 = require('../TopPanel/Top');
2122
var actions_1 = require('../../actions');
23+
var selectors_1 = require('../../selectors');
2224
var styles = {
2325
width: '100%',
2426
overflowY: 'scroll',
@@ -35,18 +37,15 @@ var Page = (function (_super) {
3537
Top_1.default.toggle(false);
3638
};
3739
Page.prototype.render = function () {
38-
var _a = this.props, tutorial = _a.tutorial, pagePosition = _a.pagePosition, packageJson = _a.packageJson, page = _a.page, markdownOpen = _a.markdownOpen;
40+
var _a = this.props, page = _a.page, markdownOpen = _a.markdownOpen;
3941
if (!page) {
4042
return null;
4143
}
42-
return (React.createElement("section", {style: styles, className: 'cr-page'}, React.createElement(PageDescription_1.default, {title: page.title, content: page.description, open: true, click: markdownOpen.bind(this)}), React.createElement(Tasks_1.default, {tasks: page.tasks, page: page, config: packageJson.config, pagePosition: pagePosition})));
44+
return (React.createElement("section", {style: styles, className: 'cr-page'}, React.createElement(PageDescription_1.default, {title: page.title, content: page.description, open: true, click: markdownOpen.bind(this)}), React.createElement(Tasks_1.default, null), React.createElement(TasksComplete_1.default, {page: page})));
4345
};
4446
Page = __decorate([
4547
react_redux_1.connect(function (state) { return ({
46-
tutorial: state.tutorial,
47-
pagePosition: state.pagePosition,
48-
packageJson: state.packageJson,
49-
page: state.tutorial.pages[state.pagePosition]
48+
page: selectors_1.pageSelector(state),
5049
}); }, function (dispatch) { return ({
5150
markdownOpen: function (content) { return dispatch(actions_1.editorMarkdownOpen(null, content)); },
5251
}); }),

lib/components/TopPanel/TopPanel.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var TopPanel = (function (_super) {
4242
}
4343
TopPanel.prototype.render = function () {
4444
var _this = this;
45-
var _a = this.props, tutorial = _a.tutorial, pagePosition = _a.pagePosition, pageSet = _a.pageSet, pageAdd = _a.pageAdd;
45+
var _a = this.props, tutorial = _a.tutorial, pageSet = _a.pageSet, pageAdd = _a.pageAdd;
4646
if (!tutorial || !tutorial.pages) {
4747
return null;
4848
}
@@ -53,7 +53,6 @@ var TopPanel = (function (_super) {
5353
TopPanel = __decorate([
5454
react_redux_1.connect(function (state) { return ({
5555
tutorial: state.tutorial,
56-
pagePosition: state.pagePosition,
5756
}); }, function (dispatch) { return ({
5857
pageSet: function (index) {
5958
dispatch(actions_1.pageSet(index));

lib/selectors.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
var reselect_1 = require('reselect');
3+
exports.pageSelector = function (state) { return state.tutorial.pages[state.pagePosition]; };
4+
exports.tasksSelector = reselect_1.createSelector(exports.pageSelector, function (page) { return page.tasks; });
5+
exports.configSelector = function (state) { return state.packageJson.config; };

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"redux-logger": "2.6.1",
4040
"redux-throttle-actions": "1.0.1",
4141
"redux-thunk": "2.1.0",
42+
"reselect": "2.5.1",
4243
"sort-package-json": "1.4.0"
4344
},
4445
"activationCommands": {

src/components/Page/Hints/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import FontIcon from 'material-ui/FontIcon';
55
import AddButton from '../AddButton';
66
import {tutorialHintAdd, editorMarkdownOpen} from '../../../actions';
77
import TextField from 'material-ui/TextField';
8+
import {hintsSelector} from '../../../selectors';
89

910
const styles = {
1011
text: {

src/components/Page/PageDescription/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ const PageDescription: React.StatelessComponent<{
2323
/> : null}
2424
<CardText expandable={true}>
2525
{contentArray.map((c: string, index) => (
26-
<Markdown key={index.toString()} onClick={click.bind(this, c)}>{c}</Markdown>
26+
<Markdown
27+
key={index.toString()}
28+
onClick={click.bind(this, c)}
29+
>
30+
{c}
31+
</Markdown>
2732
))}
2833
</CardText>
2934
</Card>

src/components/Page/Tasks/index.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import {Tabs, Tab} from 'material-ui/Tabs';
66
import Subheader from 'material-ui/Subheader';
77
import Task from '../Task';
88
import {lightGreen200} from 'material-ui/styles/colors';
9-
import TasksComplete from '../TasksComplete';
109
import Tests from '../Tests';
1110
import TaskActions from '../TaskActions';
1211
import Hints from '../Hints';
1312
import AddButton from '../AddButton';
1413
import {tutorialTaskAdd, editorMarkdownOpen} from '../../../actions';
1514
import twoDigitify from '../../../services/twoDigitify';
1615
import {join} from 'path';
16+
import {tasksSelector} from '../../../selectors';
1717

1818
const styles = {
1919
card: {
@@ -39,17 +39,19 @@ const styles = {
3939
},
4040
};
4141

42-
@connect(null, dispatch => ({
42+
@connect(state => ({
43+
tasks: tasksSelector(state),
44+
}), dispatch => ({
4345
taskAdd: () => dispatch(tutorialTaskAdd()),
4446
markdownOpen: (content: string) => {
4547
dispatch(editorMarkdownOpen(null, content));
4648
},
4749
}))
4850
export default class Tasks extends React.Component<{
49-
tasks: CR.Task[], page: CR.Page, config: Tutorial.Config, taskAdd?: any, markdownOpen?: any, pagePosition: number
51+
tasks?: CR.Task[], taskAdd?: any, markdownOpen?: any
5052
}, {}> {
5153
render() {
52-
const {tasks, page, config, taskAdd, markdownOpen, pagePosition} = this.props;
54+
const {tasks, taskAdd, markdownOpen} = this.props;
5355
return (
5456
<div>
5557
{tasks.map((task: CR.Task, index: number) => (
@@ -66,7 +68,6 @@ export default class Tasks extends React.Component<{
6668
<Tests
6769
style={styles.test}
6870
tests={task.tests}
69-
config={config}
7071
/>
7172
</CardHeader>
7273
<CardText expandable={true} style={styles.cardContent}>
@@ -103,10 +104,6 @@ export default class Tasks extends React.Component<{
103104

104105
<AddButton callback={taskAdd}/>
105106

106-
<TasksComplete
107-
page={page}
108-
/>
109-
110107
</div>
111108
);
112109
}

src/components/Page/Tests/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ import {join} from 'path';
44
import {editorOpen} from '../../../actions';
55
import tutorialConfigOptions from '../../../config-options';
66
import FlatButton from 'material-ui/FlatButton';
7+
import {configSelector} from '../../../selectors';
78

89
const styles = {
910
button: {
1011
bottom: '8px',
1112
},
1213
};
1314

14-
@connect(null, dispatch => ({
15+
@connect(state => ({
16+
config: configSelector(state),
17+
}), dispatch => ({
1518
open: (file: string) => {
1619
dispatch(editorOpen(join('tutorial', file)));
1720
},
1821
}))
1922
export default class Tests extends React.Component<{
20-
tests: string[], config: Tutorial.Config, style?: Object,
23+
tests: string[], config?: Tutorial.Config, style?: Object,
2124
open?: (file: string) => any
2225
}, {}> {
2326
render() {

0 commit comments

Comments
 (0)