Router fixes

This commit is contained in:
Jori Lallo
2017-05-17 20:31:33 -07:00
parent b25298c8f1
commit 451b6ad058
3 changed files with 50 additions and 36 deletions

View File

@ -2,7 +2,7 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import get from 'lodash/get'; import get from 'lodash/get';
import { observer } from 'mobx-react'; import { observer } from 'mobx-react';
import { browserHistory, withRouter } from 'react-router'; import { withRouter } from 'react-router';
import { Flex } from 'reflexbox'; import { Flex } from 'reflexbox';
import DocumentStore from './DocumentStore'; import DocumentStore from './DocumentStore';
@ -19,10 +19,12 @@ Are you sure you want to discard them?
`; `;
type Props = { type Props = {
route: Object, match: Object,
router: Object, history: Object,
params: Object,
keydown: Object, keydown: Object,
editDocument?: boolean,
newChildDocument?: boolean,
editDocument?: boolean,
}; };
@withRouter @withRouter
@ -33,39 +35,39 @@ class Document extends Component {
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
this.store = new DocumentStore({}); this.store = new DocumentStore({ history: this.props.history });
} }
componentDidMount = () => { componentDidMount = () => {
if (this.props.route.newDocument) { if (this.props.newDocument) {
this.store.collectionId = this.props.params.id; this.store.collectionId = this.props.match.params.id;
this.store.newDocument = true; this.store.newDocument = true;
} else if (this.props.route.editDocument) { } else if (this.props.editDocument) {
this.store.documentId = this.props.params.id; this.store.documentId = this.props.match.params.id;
this.store.fetchDocument(); this.store.fetchDocument();
} else if (this.props.route.newChildDocument) { } else if (this.props.newChildDocument) {
this.store.documentId = this.props.params.id; this.store.documentId = this.props.match.params.id;
this.store.newChildDocument = true; this.store.newChildDocument = true;
this.store.fetchDocument(); this.store.fetchDocument();
} else { } else {
this.store.documentId = this.props.params.id; this.store.documentId = this.props.match.params.id;
this.store.newDocument = false; this.store.newDocument = false;
this.store.fetchDocument(); this.store.fetchDocument();
} }
// Prevent user from accidentally leaving with unsaved changes // // Prevent user from accidentally leaving with unsaved changes
const remove = this.props.router.setRouteLeaveHook(this.props.route, () => { // const remove = this.props.router.setRouteLeaveHook(this.props.route, () => {
if (this.store.hasPendingChanges) { // if (this.store.hasPendingChanges) {
return confirm(DISCARD_CHANGES); // return confirm(DISCARD_CHANGES);
} // }
remove(); // remove();
return null; // return null;
}); // });
}; };
onEdit = () => { onEdit = () => {
const url = `${this.store.document.url}/edit`; const url = `${this.store.document.url}/edit`;
browserHistory.push(url); this.props.history.push(url);
}; };
onSave = (options: { redirect?: boolean } = {}) => { onSave = (options: { redirect?: boolean } = {}) => {
@ -85,20 +87,22 @@ class Document extends Component {
}; };
onCancel = () => { onCancel = () => {
browserHistory.goBack(); this.props.history.goBack();
}; };
render() { render() {
const { route } = this.props; const isNew = this.props.newDocument || this.props.newChildDocument;
const isNew = route.newDocument || route.newChildDocument; const isEditing = this.props.editDocument;
const isEditing = route.editDocument;
const title = ( const title = (
<Breadcrumbs <Breadcrumbs
document={this.store.document} document={this.store.document}
pathToDocument={this.store.pathToDocument} pathToDocument={this.store.pathToDocument}
/> />
); );
const titleText = `${get(this.store, 'document.collection.name')} - ${get(this.store, 'document.title')}`;
const titleText =
this.store.document &&
`${get(this.store, 'document.collection.name')} - ${get(this.store, 'document.title')}`;
const actions = ( const actions = (
<Flex> <Flex>
@ -107,7 +111,7 @@ class Document extends Component {
? <SaveAction ? <SaveAction
onClick={this.onSave} onClick={this.onSave}
disabled={this.store.isSaving} disabled={this.store.isSaving}
isNew={isNew} isNew={!!isNew}
/> />
: <a onClick={this.onEdit}>Edit</a>} : <a onClick={this.onEdit}>Edit</a>}
</HeaderAction> </HeaderAction>
@ -140,7 +144,7 @@ class Document extends Component {
onChange={this.store.updateText} onChange={this.store.updateText}
onSave={this.onSave} onSave={this.onSave}
onCancel={this.onCancel} onCancel={this.onCancel}
readOnly={!this.props.route.editDocument} readOnly={!this.props.editDocument}
/>} />}
</Layout> </Layout>
); );

View File

@ -1,6 +1,5 @@
// @flow // @flow
import { observable, action, computed, toJS } from 'mobx'; import { observable, action, computed, toJS } from 'mobx';
import { browserHistory } from 'react-router';
import get from 'lodash/get'; import get from 'lodash/get';
import invariant from 'invariant'; import invariant from 'invariant';
import { client } from 'utils/ApiClient'; import { client } from 'utils/ApiClient';
@ -23,6 +22,10 @@ const parseHeader = text => {
return ''; return '';
}; };
type Options = {
history: Object,
};
class DocumentStore { class DocumentStore {
@observable collapsedNodes: string[] = []; @observable collapsedNodes: string[] = [];
@observable documentId = null; @observable documentId = null;
@ -38,6 +41,8 @@ class DocumentStore {
@observable isSaving: boolean = false; @observable isSaving: boolean = false;
@observable isUploading: boolean = false; @observable isUploading: boolean = false;
history: Object;
/* Computed */ /* Computed */
@computed get isCollection(): boolean { @computed get isCollection(): boolean {
@ -136,7 +141,7 @@ class DocumentStore {
const { url } = res.data; const { url } = res.data;
this.hasPendingChanges = false; this.hasPendingChanges = false;
if (redirect) browserHistory.push(url); if (redirect) this.history.push(url);
} catch (e) { } catch (e) {
console.error('Something went wrong'); console.error('Something went wrong');
} }
@ -162,7 +167,7 @@ class DocumentStore {
const { url } = res.data; const { url } = res.data;
this.hasPendingChanges = false; this.hasPendingChanges = false;
if (redirect) browserHistory.push(url); if (redirect) this.history.push(url);
} catch (e) { } catch (e) {
console.error('Something went wrong'); console.error('Something went wrong');
} }
@ -174,7 +179,7 @@ class DocumentStore {
try { try {
await client.post('/documents.delete', { id: this.documentId }); await client.post('/documents.delete', { id: this.documentId });
browserHistory.push(this.document.collection.id); this.history.push(this.document.collection.url);
} catch (e) { } catch (e) {
console.error('Something went wrong'); console.error('Something went wrong');
} }
@ -192,6 +197,10 @@ class DocumentStore {
@action updateUploading = (uploading: boolean) => { @action updateUploading = (uploading: boolean) => {
this.isUploading = uploading; this.isUploading = uploading;
}; };
constructor(options: Options) {
this.history = options.history;
}
} }
export default DocumentStore; export default DocumentStore;

View File

@ -2,13 +2,14 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import invariant from 'invariant'; import invariant from 'invariant';
import get from 'lodash/get'; import get from 'lodash/get';
import { browserHistory } from 'react-router'; import { withRouter } from 'react-router-dom';
import { observer } from 'mobx-react'; import { observer } from 'mobx-react';
import type { Document as DocumentType } from 'types'; import type { Document as DocumentType } from 'types';
import DropdownMenu, { MenuItem, MoreIcon } from 'components/DropdownMenu'; import DropdownMenu, { MenuItem, MoreIcon } from 'components/DropdownMenu';
import DocumentStore from '../DocumentStore'; import DocumentStore from '../DocumentStore';
type Props = { type Props = {
history: Object,
document: DocumentType, document: DocumentType,
collectionTree: ?Object, collectionTree: ?Object,
store: DocumentStore, store: DocumentStore,
@ -19,12 +20,12 @@ type Props = {
onCreateDocument = () => { onCreateDocument = () => {
invariant(this.props.collectionTree, 'collectionTree is not available'); invariant(this.props.collectionTree, 'collectionTree is not available');
browserHistory.push(`${this.props.collectionTree.url}/new`); this.props.history.push(`${this.props.collectionTree.url}/new`);
}; };
onCreateChild = () => { onCreateChild = () => {
invariant(this.props.document, 'Document is not available'); invariant(this.props.document, 'Document is not available');
browserHistory.push(`${this.props.document.url}/new`); this.props.history.push(`${this.props.document.url}/new`);
}; };
onDelete = () => { onDelete = () => {
@ -75,4 +76,4 @@ type Props = {
} }
} }
export default Menu; export default withRouter(Menu);