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",
"redux": "^3.5.2",
"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) {
if (err) { return res.json({success: false}) }
return res.json({
id: newProject._id,
name: newProject.name,
file: {
name: newProject.file.name,

View file

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

View file

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

View file

@ -66,7 +66,8 @@ export function saveProject() {
// type: ActionTypes.PROJECT_SAVE_FAIL
// }));
// }
return function(dispatch) {
return function(dispatch, getState) {
var state = getState();
}
}
@ -78,8 +79,14 @@ export function createProject() {
.then(response => {
dispatch({
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({
type: ActionTypes.PROJECT_SAVE_FAIL

View file

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

View file

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