figure out how redux-thunk works

This commit is contained in:
catarak 2016-06-17 16:40:13 -04:00
parent e1a79ba7a6
commit 1045cddaeb
7 changed files with 24 additions and 5 deletions

View file

@ -55,6 +55,7 @@
"react-router": "^2.4.1", "react-router": "^2.4.1",
"redux": "^3.5.2", "redux": "^3.5.2",
"redux-form": "^5.2.5", "redux-form": "^5.2.5",
"redux-thunk": "^2.1.0" "redux-thunk": "^2.1.0",
"shortid": "^2.2.6"
} }
} }

View file

@ -8,6 +8,7 @@ export function createProject(req, res) {
}, function(err, newProject) { }, function(err, newProject) {
if (err) { return res.json({success: false}) } if (err) { return res.json({success: false}) }
return res.json({ return res.json({
id: newProject._id,
name: newProject.name, name: newProject.name,
file: { file: {
name: newProject.file.name, name: newProject.file.name,

View file

@ -1,5 +1,6 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
import shortid from 'shortid';
const fileSchema = new Schema({ const fileSchema = new Schema({
name: {type: String, default: 'sketch.js'}, name: {type: String, default: 'sketch.js'},
@ -9,7 +10,8 @@ const fileSchema = new Schema({
const projectSchema = new Schema({ const projectSchema = new Schema({
name: {type: String, default: "Hello p5.js, it's the server"}, name: {type: String, default: "Hello p5.js, it's the server"},
user: {type: Schema.Types.ObjectId, ref: 'User'}, user: {type: Schema.Types.ObjectId, ref: 'User'},
file: {type: fileSchema} file: {type: fileSchema},
_id: {type: String, default: shortid.generate}
}, {timestamps: true}); }, {timestamps: true});
export default mongoose.model('Project', projectSchema); export default mongoose.model('Project', projectSchema);

View file

@ -13,6 +13,8 @@ class IDEView extends React.Component {
return ( return (
<div className="ide"> <div className="ide">
<Nav user={this.props.user} <Nav user={this.props.user}
project={this.props.project}
file={this.props.file}
createProject={this.props.createProject} createProject={this.props.createProject}
saveProject={this.props.saveProject}/> saveProject={this.props.saveProject}/>
<Toolbar <Toolbar

View file

@ -66,7 +66,8 @@ export function saveProject() {
// type: ActionTypes.PROJECT_SAVE_FAIL // type: ActionTypes.PROJECT_SAVE_FAIL
// })); // }));
// } // }
return function(dispatch) { return function(dispatch, getState) {
var state = getState();
} }
} }
@ -78,8 +79,14 @@ export function createProject() {
.then(response => { .then(response => {
dispatch({ dispatch({
type: ActionTypes.NEW_PROJECT, type: ActionTypes.NEW_PROJECT,
name: response.data.name name: response.data.name,
id: response.data.id,
file: {
name: response.data.file.name,
content: response.data.file.content
}
}); });
browserHistory.push('/' + response.data.id);
}) })
.catch(response => dispatch({ .catch(response => dispatch({
type: ActionTypes.PROJECT_SAVE_FAIL type: ActionTypes.PROJECT_SAVE_FAIL

View file

@ -18,6 +18,11 @@ const file = (state = initialState, action) => {
name: action.name, name: action.name,
content: action.content content: action.content
} }
case ActionTypes.NEW_PROJECT:
return {
name: action.file.name,
content: action.file.conent
}
default: default:
return state return state
} }

View file

@ -12,6 +12,7 @@ const project = (state = initialState, action) => {
} }
case ActionTypes.NEW_PROJECT: case ActionTypes.NEW_PROJECT:
return { return {
id: action.id,
name: action.name name: action.name
} }
default: default: