1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-28 16:48:04 +02:00

add DELETE http support to form

This commit is contained in:
Tim Daubenschütz 2015-08-06 10:09:25 +02:00
parent d66549478f
commit dbd0e14a64
5 changed files with 52 additions and 12 deletions

View File

@ -15,13 +15,25 @@ import { mergeOptionsWithDuplicates } from '../../utils/general_utils';
let Form = React.createClass({ let Form = React.createClass({
propTypes: { propTypes: {
url: React.PropTypes.string, url: React.PropTypes.string,
method: React.PropTypes.string,
handleSuccess: React.PropTypes.func, handleSuccess: React.PropTypes.func,
getFormData: React.PropTypes.func, getFormData: React.PropTypes.func,
children: React.PropTypes.oneOfType([ children: React.PropTypes.oneOfType([
React.PropTypes.object, React.PropTypes.object,
React.PropTypes.array React.PropTypes.array
]), ]),
className: React.PropTypes.string className: React.PropTypes.string,
spinner: React.PropTypes.element,
buttons: React.PropTypes.oneOfType([
React.PropTypes.element,
React.PropTypes.arrayOf(React.PropTypes.element)
])
},
getDefaultProps() {
return {
method: 'post'
};
}, },
getInitialState() { getInitialState() {
@ -31,6 +43,7 @@ let Form = React.createClass({
errors: [] errors: []
}; };
}, },
reset(){ reset(){
for (let ref in this.refs){ for (let ref in this.refs){
if (typeof this.refs[ref].reset === 'function'){ if (typeof this.refs[ref].reset === 'function'){
@ -39,22 +52,38 @@ let Form = React.createClass({
} }
this.setState(this.getInitialState()); this.setState(this.getInitialState());
}, },
submit(event){ submit(event){
if (event) {
if(event) {
event.preventDefault(); event.preventDefault();
} }
this.setState({submitted: true}); this.setState({submitted: true});
this.clearErrors(); this.clearErrors();
let action = (this.httpVerb && this.httpVerb()) || 'post';
window.setTimeout(() => this[action](), 100); // selecting http method based on props
if(this[this.props.method]) {
window.setTimeout(() => this[this.props.method](), 100);
} else {
throw new Error('This HTTP method is not supported by form.js (' + this.props.method + ')');
}
}, },
post(){
post() {
requests requests
.post(this.props.url, { body: this.getFormData() }) .post(this.props.url, { body: this.getFormData() })
.then(this.handleSuccess) .then(this.handleSuccess)
.catch(this.handleError); .catch(this.handleError);
}, },
delete() {
requests
.delete(this.props.url, this.getFormData())
.then(this.handleSuccess)
.catch(this.handleError);
},
getFormData(){ getFormData(){
let data = {}; let data = {};
for (let ref in this.refs){ for (let ref in this.refs){
@ -70,6 +99,7 @@ let Form = React.createClass({
handleChangeChild(){ handleChangeChild(){
this.setState({edited: true}); this.setState({edited: true});
}, },
handleSuccess(response){ handleSuccess(response){
if ('handleSuccess' in this.props){ if ('handleSuccess' in this.props){
this.props.handleSuccess(response); this.props.handleSuccess(response);
@ -79,8 +109,12 @@ let Form = React.createClass({
this.refs[ref].handleSuccess(); this.refs[ref].handleSuccess();
} }
} }
this.setState({edited: false, submitted: false}); this.setState({
edited: false,
submitted: false
});
}, },
handleError(err){ handleError(err){
if (err.json) { if (err.json) {
for (var input in err.json.errors){ for (var input in err.json.errors){
@ -104,6 +138,7 @@ let Form = React.createClass({
} }
this.setState({submitted: false}); this.setState({submitted: false});
}, },
clearErrors(){ clearErrors(){
for (var ref in this.refs){ for (var ref in this.refs){
if ('clearErrors' in this.refs[ref]){ if ('clearErrors' in this.refs[ref]){
@ -112,6 +147,7 @@ let Form = React.createClass({
} }
this.setState({errors: []}); this.setState({errors: []});
}, },
getButtons() { getButtons() {
if (this.state.submitted){ if (this.state.submitted){
return this.props.spinner; return this.props.spinner;
@ -134,6 +170,7 @@ let Form = React.createClass({
} }
return buttons; return buttons;
}, },
getErrors() { getErrors() {
let errors = null; let errors = null;
if (this.state.errors.length > 0){ if (this.state.errors.length > 0){
@ -143,6 +180,7 @@ let Form = React.createClass({
} }
return errors; return errors;
}, },
renderChildren() { renderChildren() {
return ReactAddons.Children.map(this.props.children, (child) => { return ReactAddons.Children.map(this.props.children, (child) => {
if (child) { if (child) {
@ -153,6 +191,7 @@ let Form = React.createClass({
} }
}); });
}, },
render() { render() {
let className = 'ascribe-form'; let className = 'ascribe-form';

View File

@ -26,7 +26,6 @@ let ConsignForm = React.createClass({
}, },
render() { render() {
return ( return (
<Form <Form
ref='form' ref='form'

View File

@ -17,15 +17,17 @@ let EditionDeleteForm = React.createClass({
handleSuccess: React.PropTypes.func handleSuccess: React.PropTypes.func
}, },
getBitcoinIds(){ getBitcoinIds() {
return this.props.editions.map(function(edition){ return this.props.editions.map(function(edition){
return edition.bitcoin_id; return edition.bitcoin_id;
}); });
}, },
// Since this form can be used for either deleting a single edition or multiple
// we need to call getBitcoinIds to get the value of edition_id
getFormData() { getFormData() {
return { return {
edition_id: this.getBitcoinIds().join() edition_id: this.getBitcoinIds().join(',')
}; };
}, },
@ -35,6 +37,7 @@ let EditionDeleteForm = React.createClass({
ref='form' ref='form'
url={ApiUrls.edition_delete} url={ApiUrls.edition_delete}
getFormData={this.getFormData} getFormData={this.getFormData}
method="delete"
handleSuccess={this.props.handleSuccess} handleSuccess={this.props.handleSuccess}
buttons={ buttons={
<div className="modal-footer"> <div className="modal-footer">

View File

@ -59,8 +59,7 @@ let RequestActionForm = React.createClass({
onClick={this.handleRequest} onClick={this.handleRequest}
className='btn btn-default btn-sm ascribe-margin-1px'>{getLangText('ACCEPT')} className='btn btn-default btn-sm ascribe-margin-1px'>{getLangText('ACCEPT')}
</div>); </div>);
if (edition.request_action === 'unconsign'){ if (edition.request_action === 'unconsign') {
console.log(this.props)
buttonAccept = ( buttonAccept = (
<AclButton <AclButton
availableAcls={{'acl_unconsign': true}} availableAcls={{'acl_unconsign': true}}

View File

@ -91,7 +91,7 @@ class Requests {
} catch(err) { } catch(err) {
throw err; throw err;
} }
newUrl = newUrl.replace(re, (match, key) => { newUrl = newUrl.replace(re, (match, key) => {
let val = params[key]; let val = params[key];
if (!val) { if (!val) {