Comments (4)
@tsvetkovpro remove isRequired
. You are getting this warning because you have:
ManageCoursePage.contextTypes = {
router: PropTypes.object.isRequired,
};
instead of
ManageCoursePage.contextTypes = {
router: PropTypes.object,
};
from pluralsight-redux-starter.
ManageCoursePage.js
import React, {PropTypes} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as courseActions from '../../actions/courseActions';
import CourseForm from './CourseForm';
import toastr from 'toastr';
export class ManageCoursePage extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
course: Object.assign({}, props.course),
errors: {},
saving: false
};
this.updateCourseState = this.updateCourseState.bind(this);
this.saveCourse = this.saveCourse.bind(this);
}
componentWillReceiveProps(nextProps) {
if (this.props.course.id != nextProps.course.id) {
// necessary to populate form when existing course is loaded directly
this.setState({course: Object.assign({}, nextProps.course)});
}
}
updateCourseState(event) {
const field = event.target.name;
let course = this.state.course;
course[field] = event.target.value;
return this.setState({course: course});
}
courseFormIsValid() {
let formIsValid = true;
let errors = {};
if (this.state.course.title.length < 5) {
errors.title = 'Title must be at least 5 characters.';
formIsValid = false;
}
this.setState({errors: errors});
return formIsValid;
}
saveCourse(event) {
event.preventDefault();
if(!this.courseFormIsValid()) {
return;
}
this.setState({saving: true});
this.props.actions.saveCourse(this.state.course)
.then(() => this.redirect())
.catch(error => {
toastr.error(error);
this.setState({saving: false});
});
}
redirect() {
this.setState({saving: false});
toastr.success('ะััั ัะพั
ัะฐะฝัะฝ');
this.context.router.push('/courses'); // as promise result on 38 then
}
render() {
return (
<CourseForm
allAuthors={this.props.authors}
onChange={this.updateCourseState}
onSave={this.saveCourse}
course={this.state.course}
errors={this.state.errors}
saving={this.state.saving}
/>
);
}
}
ManageCoursePage.propTypes = {
course: PropTypes.object.isRequired,
authors: PropTypes.array.isRequired,
actions: PropTypes.object.isRequired
};
// pull in the React Router context so router is available on this.context.router
ManageCoursePage.contextTypes = {
router: PropTypes.object.isRequired
};
function getCourseById(courses, id) {
const course = courses.filter(course => course.id == id);
if (course) return course[0];
return null;
}
function mapStateToProps(state, ownProps) {
const courseId = ownProps.params.id; //from the path `/course/:id`
let course = {id: '', watchHref: '', title: '', authorId: '', length: '', category: ''};
if (courseId && state.courses.length > 0) {
course = getCourseById(state.courses, courseId);
}
const authorsFormattedForDropdown = state.authors.map(author => {
return {
value: author.id,
text: author.firstName + ' ' + author.lastName
};
});
return {
course: course,
authors: authorsFormattedForDropdown
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(courseActions, dispatch)
};
}
export default connect(mapStateToProps, mapDispatchToProps)(ManageCoursePage);
from pluralsight-redux-starter.
Since my code works fine and yours doesn't, have you diff'd the two? I don't see anything obviously wrong at a glance, but It's likely a simple typo.
from pluralsight-redux-starter.
Good catch @tmxyz. Thanks for the response! ๐
from pluralsight-redux-starter.
Related Issues (20)
- how to use mock api on server for demo purpose ??? HOT 1
- ManageCoursePage.js:72 Uncaught TypeError: Cannot read property 'map' of undefined HOT 1
- Chapter 13 - Testing React. HOT 1
- Chapter 9 - Redux Flow HOT 1
- Courses are not being loaded from the api - store.dispatch(loadCourses()); HOT 3
- npm start error HOT 11
- In production build getting some errors HOT 3
- Logic bug in courseActions HOT 6
- In mockAuthorApi.js, in deleteAuthor: findIndex is missing a return statement HOT 1
- In mock Api, in deleteAuthor and deleteCourse: findIndex is missing a return statement HOT 1
- How to Seo and share Facebook ? HOT 1
- Unexpected token < error when reloading course/:id route HOT 5
- Production build not using/loading correct css styling HOT 5
- Readme needs update - "Disable safe write" HOT 1
- How to deploy in Heroku the final project
- .
- ESLint: App not found in './components/App' (import/named) HOT 4
- Issue with CSS loading HOT 10
- delete functionality with starter kit HOT 1
- SyntaxError: Unexpected token import HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pluralsight-redux-starter.