Skip to content

Instantly share code, notes, and snippets.

@mikhuang
Created January 5, 2017 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikhuang/7071f9c12bfc8c35e471b3c35a326b51 to your computer and use it in GitHub Desktop.
Save mikhuang/7071f9c12bfc8c35e471b3c35a326b51 to your computer and use it in GitHub Desktop.
payment
import React, { Component } from 'react'
import { connect } from 'react-redux'
import {merge, assign, isEmpty} from 'lodash'
import Form from "react-jsonschema-form"
import {nestDataByLevels, getNestedData} from './../models/modelUtil'
//import { Popover, PopoverContent } from 'reactstrap';
import paymentActions from '../actions/payments'
import cuid from 'cuid'
const noop = () => {}
class AddPayment extends Component {
defaultState = {
popupOpen: false,
hasErrors : false, // if the form has JS errors
newId : null, // id passed when a new object is created
}
constructor(props) {
super(props)
this.state = assign({}, this.defaultState, {
paymentData: props.payments ? props.payments.asMutable({deep: true}) : {},
})
console.log(props.payments)
}
componentWillReceiveProps(nextProps) {
let obj = {}
if (nextProps.paymentData && this.props.paymentData !== nextProps.paymentData) {
obj.paymentData = nextProps.payments ? nextProps.payments.asMutable({deep: true}) : {}
}
if (!isEmpty(obj)) {
this.setState(obj)
}
}
toggle = () => {
this.setState({
popupOpen: !this.state.popupOpen,
})
}
renderButtons(success = noop) {
return <div>
{this.props.toggle && <button type="button" onClick={this.props.toggle} className="btn btn-default">Cancel</button> }
<button type="submit" onClick={success} className="btn btn-primary">Submit</button>
</div>
}
saveData = () => {
let data = this.state
// if there isn't any data worth saving, bail
// # TODO: verify that this is working, I see empty USER updates being made
if (isEmpty(data)) {
return;
}
console.log("!!!!!!!!!!!!!!!!!!!!!")
console.log(data)
console.log(data.formData)
data.wedding = this.props.user.profile.wedding.id
// has to be a vendorwedding which is going to be.. fun
const shouldCreate = !data.id
this.props.createOrUpdatePayment(data).then((results) => {
if (shouldCreate) {
const newId = results.data.id
this.setState({
newId,
}, () => {
this.props.createOrUpdatePayment(newId) // pass id of new request to parent
})
}
})
}
render() {
const schema={
"type": "object",
properties: {
paymentDate: {
title: `When did you make the payment?`,
"type": "string",
"minLength": 1,
},
paymentMethod: {
title: `How did you make the payment? (cash, check, ..)`,
"type": "string",
"minLength": 1,
},
},
}
return (
<div>
"hi!"
<Form
formData={this.state.paymentData}
showErrorList={false}
//onChange={(data) => {
// this.handleChange(data)
//}} // implement this for data input checks
schema={schema}
onSubmit={this.saveData}
>
//{this.renderButtons()}
</Form>
"bye"
</div>
)
}
}
/*AddPayment.propTypes = {
plan: React.PropTypes.object,
}*/
AddPayment = connect((state, props) => {
return {}
}, (dispatch) => {
return {
createOrUpdatePayment: (data) => {
return dispatch(paymentActions.createOrUpdate(data))
},
}
})(AddPayment)
export default AddPayment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment