From 0a4dae850b962588702255af8f928918d1c5063f Mon Sep 17 00:00:00 2001 From: ddejongh Date: Thu, 28 May 2015 18:18:13 +0200 Subject: [PATCH] solved error + reusability refactor --- .../ascribe_forms/form_share_email.js | 57 +++++++++ js/components/ascribe_modal/modal_share.js | 117 +----------------- js/mixins/form_alert_mixin.js | 75 +++++++++++ 3 files changed, 138 insertions(+), 111 deletions(-) create mode 100644 js/components/ascribe_forms/form_share_email.js create mode 100644 js/mixins/form_alert_mixin.js diff --git a/js/components/ascribe_forms/form_share_email.js b/js/components/ascribe_forms/form_share_email.js new file mode 100644 index 00000000..d78c02ec --- /dev/null +++ b/js/components/ascribe_forms/form_share_email.js @@ -0,0 +1,57 @@ +import fetch from 'isomorphic-fetch'; + +import React from 'react'; + +import AppConstants from '../../constants/application_constants'; +import FetchApiUtils from '../../utils/fetch_api_utils'; +import FormMixin from '../../mixins/form_alert_mixin'; + +let ShareForm = React.createClass({ + mixins: [FormMixin], + + submit(e) { + e.preventDefault(); + fetch(AppConstants.baseUrl + 'ownership/shares/mail/', { + method: 'post', + headers: { + 'Authorization': 'Basic ' + AppConstants.debugCredentialBase64, + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(this.getFormData()) + }) + .then( + (response) => this.handleResponse(response) + ); + }, + getFormData() { + return { + bitcoin_id: this.props.edition.bitcoin_id, + share_emails: this.refs.share_emails.getDOMNode().value, + share_message: this.refs.share_message.getDOMNode().value + } + }, + renderMessage() { + return "" + + "Hi,\n" + + "\n" + + "I am sharing \"" + this.props.edition.title + "\" with you.\n" + + "\n" + + "Truly yours,\n" + + this.props.currentUser.username; + }, + render() { + return ( +
+ {this.renderTextInput("share_emails", "email", "Comma separated emails", "required")} + {this.renderTextArea("share_message", this.renderMessage(), "")} +
+ + +
+
+ ); + } +}); + +export default ShareForm; \ No newline at end of file diff --git a/js/components/ascribe_modal/modal_share.js b/js/components/ascribe_modal/modal_share.js index a3a4141d..4cc05d5a 100644 --- a/js/components/ascribe_modal/modal_share.js +++ b/js/components/ascribe_modal/modal_share.js @@ -1,22 +1,19 @@ -import fetch from 'isomorphic-fetch'; - -import AppConstants from '../../constants/application_constants'; -import FetchApiUtils from '../../utils/fetch_api_utils'; - import React from 'react'; import Modal from 'react-bootstrap/lib/Modal'; import OverlayTrigger from 'react-bootstrap/lib/OverlayTrigger'; import ModalTrigger from 'react-bootstrap/lib/ModalTrigger'; import Tooltip from 'react-bootstrap/lib/Tooltip'; -import Alert from 'react-bootstrap/lib/Alert'; + +import ShareForm from '../ascribe_forms/form_share_email' + let ShareModalButton = React.createClass({ render() { return ( Share the artwork}> }> + currentUser={this.props.currentUser}/>}>
@@ -36,115 +33,13 @@ let ShareModal = React.createClass({
+ currentUser={this.props.currentUser} + onRequestHide={this.onRequestHide}/>
) } }); -let ShareForm = React.createClass({ - getInitialState() { - return {errors: null} - }, - submit(e) { - e.preventDefault(); - let url = "http://localhost:8000/api/ownership/shares/mail/"; - fetch(url, { - method: 'post', - headers: { - 'Authorization': 'Basic ' + AppConstants.debugCredentialBase64, - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - body: JSON.stringify(this.getFormData()) - }) - .then((response) => { - if (response.status >= 200 && response.status < 300) - return response - response.json().then((response) => this.setState({errors: response.errors})) - } - ); - //.then(FetchApiUtils.status) - - }, - getFormData() { - return { - bitcoin_id: this.props.edition.bitcoin_id, - share_emails: this.refs.share_emails.getDOMNode().value, - share_message: this.refs.share_message.getDOMNode().value - } - }, - renderAlert(id){ - if (this.state.errors && id in this.state.errors) { - return this.state.errors[id].map(function(error) { - return - } - ) - } - return - }, - renderTextInput(id, placeHolder, required) { - - return ( -
- {this.renderAlert(id)} - -
- ) - }, - renderTextArea(id, placeHolder, required) { - let alert = ""; - if (this.state.errors && id in this.state.errors) { - alert = - } - return ( -
- {alert} - -
- ) - }, - render() { - let content = "Hi,\n\nI am sharing \"" + this.props.edition.title + - "\" with you.\n\nTruly yours,\n" + this.props.currentUser.username; - return ( -
- {this.renderTextInput("share_emails", "Comma separated emails", "required")} - {this.renderTextArea("share_message", content, "")} -
- - -
-
- ); - } -}); - -const AlertDismissable = React.createClass({ - getInitialState() { - return { - alertVisible: true - }; - }, - render() { - if (this.state.alertVisible) { - return ( - - {this.props.error} - - ); - } - return ( - - ); - }, - handleAlertDismiss() { - this.setState({alertVisible: false}); - } -}); export default ShareModalButton; diff --git a/js/mixins/form_alert_mixin.js b/js/mixins/form_alert_mixin.js new file mode 100644 index 00000000..ff9a5d52 --- /dev/null +++ b/js/mixins/form_alert_mixin.js @@ -0,0 +1,75 @@ +import React from 'react'; +import Alert from 'react-bootstrap/lib/Alert'; + +let FormMixin = { + getInitialState() { + return {errors: null, + retry: 0 // used for unique keying + } + }, + handleResponse(response){ + if (response.status >= 200 && response.status < 300) + return response; + this.handleError(response); + }, + handleError(response){ + response.json().then((response) => this.setState({errors: response.errors, + retry: this.state.retry + 1})) + }, + renderAlert(id){ + if (this.state.errors && id in this.state.errors) { + return this.state.errors[id].map(function(error) { + let key = error + this.state.retry; + return ; + }.bind(this) + ) + } + return + }, + renderTextInput(id, type, placeHolder, required) { + return ( +
+ {this.renderAlert(id)} + +
+ ) + }, + renderTextArea(id, placeHolder, required) { + return ( +
+ {this.renderAlert(id)} + +
+ ) + } +}; + +let AlertDismissable = React.createClass({ + getInitialState() { + return { + alertVisible: true + }; + }, + show() { + this.setState({alertVisible: true}); + }, + hide() { + this.setState({alertVisible: false}); + }, + render() { + if (this.state.alertVisible) { + return ( + + {this.props.error} + + ); + } + return ( + + ); + } +}); + +export default FormMixin; \ No newline at end of file