Skip to content

Commit 82084ba

Browse files
committed
[pwa] extract initial todo title form url
1 parent 6c36201 commit 82084ba

File tree

6 files changed

+128
-20
lines changed

6 files changed

+128
-20
lines changed

pwa/package-lock.json

Lines changed: 90 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pwa/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@material-ui/core": "^3.9.2",
88
"@material-ui/icons": "^3.0.2",
99
"gh-pages": "^2.0.1",
10+
"query-string": "^6.2.0",
1011
"react": "^16.8.1",
1112
"react-dom": "^16.8.1",
1213
"react-redux": "^6.0.0",

pwa/src/components/AddTodo.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,29 @@ const renderTodoTextField = ({input, ...other}) => (
3434
{...other}/>
3535
);
3636

37+
const onSubmit = (addTodo, reset, formValues) => {
38+
addTodo(formValues.todo);
39+
reset();
40+
};
41+
3742
const AddTodo = ({addTodo, classes, handleSubmit, reset}) => {
43+
const onFormSubmit = onSubmit.bind(null, addTodo, reset);
3844
return (
39-
<form className={classes.form} onSubmit={handleSubmit(formValues => {
40-
if (formValues.todo) {
41-
addTodo(formValues.todo);
42-
reset();
43-
}
44-
})}>
45-
<Field name="todo"
46-
className={classes.textField}
47-
component={renderTodoTextField}/>
45+
<form className={classes.form} onSubmit={handleSubmit(onFormSubmit)}>
46+
<Field name="todo" className={classes.textField} component={renderTodoTextField}/>
4847
</form>
4948
);
5049
};
5150

52-
export default connect(null, {addTodo})(reduxForm({
53-
form: 'addTodo'
54-
})(withStyles(styles)(AddTodo)));
51+
const styledAddTodo = withStyles(styles)(AddTodo);
52+
53+
const reduxFormAddTodo = reduxForm({
54+
form: 'addTodo',
55+
enableReinitialize: true
56+
})(styledAddTodo);
57+
58+
const mapStateToPropsForm = state => ({
59+
initialValues: {todo: state.onetimeParameters.todo}
60+
});
61+
62+
export default connect(mapStateToPropsForm, {addTodo})(reduxFormAddTodo);

pwa/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
1717
const persistConfig = {
1818
key: 'root',
1919
storage,
20+
whitelist: ['todo']
2021
};
2122

2223
const store = createStore(persistReducer(persistConfig, reducers), composeEnhancers());

pwa/src/reducers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import {combineReducers} from 'redux';
22
import {reducer as formReducer} from 'redux-form';
33

44
import todoReducer from './todoReducer';
5+
import oneTimeParametersReducer from './oneTimeParametersReducer';
56

67
export default combineReducers({
78
todo: todoReducer,
89
form: formReducer,
10+
onetimeParameters: oneTimeParametersReducer
911
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import queryString from 'query-string'
2+
3+
import {ADD_TODO} from '../actions/types';
4+
5+
const INITIAL_STATE = {todo: queryString.parse(document.location.search).todo};
6+
7+
export default (state = INITIAL_STATE, action) => {
8+
switch (action.type) {
9+
case ADD_TODO:
10+
return {...state, todo: null};
11+
default:
12+
return state;
13+
}
14+
};

0 commit comments

Comments
 (0)