Skip to content

Commit

Permalink
replace Navigation mixin with History - fixes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
echenley committed Oct 5, 2015
1 parent e380b83 commit 044b2ef
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 37 deletions.
35 changes: 14 additions & 21 deletions src/js/components/NewPost.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import React from 'react/addons';
import { Navigation } from 'react-router';
import { History } from 'react-router';
import cx from 'classnames';

import Actions from '../actions/Actions';
Expand All @@ -14,9 +14,7 @@ const NewPost = React.createClass({
errorMessage: React.PropTypes.string
},

mixins: [
Navigation
],
mixins: [ History ],

getInitialState() {
return {
Expand All @@ -27,8 +25,8 @@ const NewPost = React.createClass({
},

componentWillReceiveProps(nextProps) {
let oldLatestPost = this.props.user.latestPost;
let newLatestPost = nextProps.user.latestPost;
const oldLatestPost = this.props.user.latestPost;
const newLatestPost = nextProps.user.latestPost;

if (oldLatestPost !== newLatestPost) {
// user just submitted a new post
Expand All @@ -50,14 +48,14 @@ const NewPost = React.createClass({

// hide modal/redirect to the new post
Actions.hideModal();
this.transitionTo(`/post/${postId}`);
this.history.pushState(null, `/post/${postId}`);
},

submitPost(e) {
e.preventDefault();

let { title, link } = this.state;
let { user } = this.props;
const { title, link } = this.state;
const { user } = this.props;

if (!title) {
this.setState({
Expand All @@ -77,7 +75,7 @@ const NewPost = React.createClass({
submitted: true
});

let post = {
const post = {
title: title.trim(),
url: link,
creator: user.username,
Expand All @@ -89,23 +87,18 @@ const NewPost = React.createClass({
},

render() {
let {
submitted,
highlight,
title,
link
} = this.state;

let titleInputCx = cx('panel-input', {
const { submitted, highlight, title, link } = this.state;

const titleInputCx = cx('panel-input', {
'input-error': highlight === 'title'
});

let linkInputCx = cx('panel-input', {
const linkInputCx = cx('panel-input', {
'input-error': highlight === 'link'
});

let errorMessage = this.props.errorMessage;
let error = errorMessage && (
const errorMessage = this.props.errorMessage;
const error = errorMessage && (
<div className="error modal-form-error">{ errorMessage }</div>
);

Expand Down
14 changes: 7 additions & 7 deletions src/js/views/Posts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react/addons';
import Reflux from 'reflux';
import Actions from '../actions/Actions';
import { Navigation, TransitionHook } from 'react-router';
import { History } from 'react-router';

import PostsStore from '../stores/PostsStore';
import UserStore from '../stores/UserStore';
Expand All @@ -19,8 +19,7 @@ const Posts = React.createClass({
},

mixins: [
TransitionHook,
Navigation,
History,
Reflux.listenTo(PostsStore, 'onStoreUpdate'),
Reflux.connect(UserStore, 'user')
],
Expand All @@ -42,7 +41,7 @@ const Posts = React.createClass({
const { pageNum } = this.props.params;

if (isNaN(pageNum) || pageNum < 1) {
this.transitionTo('/404');
this.history.pushState(null, '/404');
return;
}

Expand All @@ -53,15 +52,16 @@ const Posts = React.createClass({
const { pageNum } = nextProps.params;

if (isNaN(pageNum) || pageNum < 1) {
this.transitionTo('/404');
this.history.pushState(null, '/404');
return;
}

Actions.stopWatchingPosts();
Actions.watchPosts(pageNum);
},

routerWillLeave() {
componentWillUnmount() {
console.log('hey');
Actions.stopWatchingPosts();
},

Expand Down Expand Up @@ -92,7 +92,7 @@ const Posts = React.createClass({
Actions.setSortBy(sortByValue);

if (currentPage !== 1) {
this.transitionTo('/posts/1');
this.history.pushState(null, '/posts/1');
} else {
Actions.stopWatchingPosts();
Actions.watchPosts(currentPage);
Expand Down
8 changes: 4 additions & 4 deletions src/js/views/Profile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React from 'react/addons';
import Reflux from 'reflux';
import { Navigation } from 'react-router';
import { History } from 'react-router';

import Actions from '../actions/Actions';

Expand All @@ -20,7 +20,7 @@ const Profile = React.createClass({
},

mixins: [
Navigation,
History,
Reflux.listenTo(ProfileStore, 'updateProfileData'),
Reflux.listenTo(UserStore, 'updateUser')
],
Expand Down Expand Up @@ -56,7 +56,7 @@ const Profile = React.createClass({
}
},

routerWillLeave() {
componentWillUnmount() {
Actions.stopWatchingProfile();
},

Expand All @@ -67,7 +67,7 @@ const Profile = React.createClass({
});
} else {
// user has logged out
this.transitionTo('/');
this.history.pushState(null, '/');
}
},

Expand Down
9 changes: 4 additions & 5 deletions src/js/views/Single.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React from 'react/addons';
import Reflux from 'reflux';
import { Navigation, TransitionHook } from 'react-router';
import { History } from 'react-router';

import SingleStore from '../stores/SingleStore';
import UserStore from '../stores/UserStore';
Expand All @@ -21,8 +21,7 @@ const SinglePost = React.createClass({
},

mixins: [
Navigation,
TransitionHook,
History,
Reflux.listenTo(SingleStore, 'onUpdate'),
Reflux.connect(UserStore, 'user')
],
Expand Down Expand Up @@ -55,7 +54,7 @@ const SinglePost = React.createClass({
}
},

routerWillLeave() {
componentWillUnmount() {
const { postId } = this.props.params;
Actions.stopWatchingPost(postId);
},
Expand All @@ -65,7 +64,7 @@ const SinglePost = React.createClass({

if (!post) {
// post doesn't exist
this.transitionTo('/404');
this.history.pushState(null, '/404');
return;
}

Expand Down

0 comments on commit 044b2ef

Please sign in to comment.