From 314ac75850571759540f323d107224a596a30af9 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 9 Jul 2017 20:56:36 -0700 Subject: [PATCH] Fixed: Preserved state bug --- frontend/components/Modal/Modal.js | 3 ++ frontend/models/Collection.js | 3 +- .../scenes/CollectionNew/CollectionNew.js | 47 +++++++++++-------- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/frontend/components/Modal/Modal.js b/frontend/components/Modal/Modal.js index 21f734182..343e527bb 100644 --- a/frontend/components/Modal/Modal.js +++ b/frontend/components/Modal/Modal.js @@ -11,15 +11,18 @@ class Modal extends Component { render() { const { children, + isOpen, title = 'Untitled Modal', onRequestClose, ...rest } = this.props; + if (!isOpen) return null; return ( diff --git a/frontend/models/Collection.js b/frontend/models/Collection.js index bc1755cd6..3215b2253 100644 --- a/frontend/models/Collection.js +++ b/frontend/models/Collection.js @@ -70,11 +70,12 @@ class Collection { }); } catch (e) { this.errors.add('Collection failed saving'); + return false; } finally { this.isSaving = false; } - return this; + return true; }; updateData(data: Object = {}) { diff --git a/frontend/scenes/CollectionNew/CollectionNew.js b/frontend/scenes/CollectionNew/CollectionNew.js index 113d449d2..ad6095d97 100644 --- a/frontend/scenes/CollectionNew/CollectionNew.js +++ b/frontend/scenes/CollectionNew/CollectionNew.js @@ -1,6 +1,5 @@ // @flow import React, { Component } from 'react'; -import { observer } from 'mobx-react'; import Button from 'components/Button'; import Input from 'components/Input'; import HelpText from 'components/HelpText'; @@ -8,32 +7,42 @@ import HelpText from 'components/HelpText'; import Collection from 'models/Collection'; import CollectionsStore from 'stores/CollectionsStore'; -@observer class CollectionNew extends Component { - props: { - history: Object, - collection: Collection, - collections: CollectionsStore, - onCollectionCreated: () => void, - }; - state: { name: string, isSaving: boolean }; - state = { name: '', isSaving: false }; +type Props = { + history: Object, + collections: CollectionsStore, + onCollectionCreated: () => void, +}; - static defaultProps = { - collection: new Collection(), - }; +class CollectionNew extends Component { + props: Props; + state: { collection: Collection, name: string, isSaving: boolean }; + + constructor(props: Props) { + super(props); + + this.state = { + name: '', + isSaving: false, + collection: new Collection(), + }; + } handleSubmit = async (ev: SyntheticEvent) => { ev.preventDefault(); this.setState({ isSaving: true }); - const { collection, collections } = this.props; + const { collection } = this.state; + const { collections } = this.props; collection.updateData(this.state); - await collection.save(); - collections.add(collection); + const success = await collection.save(); + + if (success) { + collections.add(collection); + this.props.onCollectionCreated(); + this.props.history.push(collection.url); + } this.setState({ isSaving: false }); - this.props.onCollectionCreated(); - this.props.history.push(collection.url); }; handleNameChange = (ev: SyntheticInputEvent) => { @@ -41,7 +50,7 @@ import CollectionsStore from 'stores/CollectionsStore'; }; render() { - const { collection } = this.props; + const { collection } = this.state; return (