From 0071e09d7eb61b83843820f505d7890e15d20607 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Mon, 11 Sep 2017 23:59:35 -0700 Subject: [PATCH] refactored document setState to mobx & cancel reload --- frontend/scenes/Document/Document.js | 65 +++++++++++++--------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/frontend/scenes/Document/Document.js b/frontend/scenes/Document/Document.js index 4bc09784..e55a15e0 100644 --- a/frontend/scenes/Document/Document.js +++ b/frontend/scenes/Document/Document.js @@ -2,6 +2,7 @@ import React, { Component } from 'react'; import get from 'lodash/get'; import styled from 'styled-components'; +import { observable } from 'mobx'; import { observer, inject } from 'mobx-react'; import { withRouter, Prompt } from 'react-router'; import Flex from 'components/Flex'; @@ -39,17 +40,13 @@ type Props = { @observer class DocumentScene extends Component { props: Props; savedTimeout: number; - state: { - newDocument?: Document, - }; - state = { - isDragging: false, - isLoading: false, - isSaving: false, - newDocument: undefined, - showAsSaved: false, - notFound: false, - }; + + @observable newDocument: ?Document; + @observable isDragging = false; + @observable isLoading = false; + @observable isSaving = false; + @observable showAsSaved = false; + @observable notFound = false; componentDidMount() { this.loadDocument(this.props); @@ -60,7 +57,7 @@ type Props = { nextProps.match.params.documentSlug !== this.props.match.params.documentSlug ) { - this.setState({ notFound: false }); + this.notFound = false; this.loadDocument(nextProps); } } @@ -77,7 +74,7 @@ type Props = { title: '', text: '', }); - this.setState({ newDocument }); + this.newDocument = newDocument; } else { let document = this.document; if (document) { @@ -92,13 +89,13 @@ type Props = { document.view(); } else { // Render 404 with search - this.setState({ notFound: true }); + this.notFound = true; } } }; get document() { - if (this.state.newDocument) return this.state.newDocument; + if (this.newDocument) return this.newDocument; return this.props.documents.getByUrl( `/doc/${this.props.match.params.documentSlug}` ); @@ -120,31 +117,30 @@ type Props = { let document = this.document; if (!document) return; - this.setState({ isLoading: true, isSaving: true }); + this.isLoading = true; + this.isSaving = true; document = await document.save(); - this.setState({ isLoading: false }); + this.isLoading = false; if (redirect || this.props.newDocument) { this.props.history.push(document.url); } else { - this.showAsSaved(); + this.toggleShowAsSaved(); } }; - showAsSaved() { - this.setState({ showAsSaved: true, isSaving: false }); - this.savedTimeout = setTimeout( - () => this.setState({ showAsSaved: false }), - 2000 - ); + toggleShowAsSaved() { + this.showAsSaved = true; + this.isSaving = false; + this.savedTimeout = setTimeout(() => (this.showAsSaved = false), 2000); } onImageUploadStart = () => { - this.setState({ isLoading: true }); + this.isLoading = true; }; onImageUploadStop = () => { - this.setState({ isLoading: false }); + this.isLoading = false; }; onChange = text => { @@ -152,10 +148,11 @@ type Props = { this.document.updateData({ text }, true); }; - onCancel = () => { + onCancel = async () => { let url; if (this.document && this.document.url) { url = this.document.url; + await this.document.fetch(); } else { url = collectionUrl(this.props.match.params.id); } @@ -163,11 +160,11 @@ type Props = { }; onStartDragging = () => { - this.setState({ isDragging: true }); + this.isDragging = true; }; onStopDragging = () => { - this.setState({ isDragging: false }); + this.isDragging = false; }; renderNotFound() { @@ -181,18 +178,18 @@ type Props = { const titleText = get(this.document, 'title', ''); const document = this.document; - if (this.state.notFound) { + if (this.notFound) { return this.renderNotFound(); } return ( - {this.state.isDragging && + {this.isDragging && Drop files here to import into Atlas. } {titleText && } - {this.state.isLoading && } + {this.isLoading && } {isFetching && @@ -232,11 +229,11 @@ type Props = { {isEditing ?