import React from 'react'; import { Checkbox, Form, Radio } from 'semantic-ui-react' import {ctx} from '../context.jsx' import SemanticDatepicker from 'react-semantic-ui-datepickers'; import 'react-semantic-ui-datepickers/dist/react-semantic-ui-datepickers.css'; class CreateSubscription extends React.Component { constructor(props) { super(props) this.state = { values: {} } } setValue(key, value) { let new_values = {...this.state.values} new_values[key] = value this.setState({ values: new_values }) } render() { const { values } = this.state const {contacts,chats} = this.context const fields = [ { type:'contacts', label:'Contact', name:'contact_id' }, { type:'chats', label:'Chat', name:'chat_id' }, { type:'radio', label:'Payment Interval', name:'interval', options:['daily','weekly','monthly'] }, { type:'checkbox', label:'Amount', name:'amount', options:[500,1000,2000,'custom'] }, { type:'multi', label:'End Rule', name:'endRule', options:[{ name:'end_number', type:'number', label:'Make Payments' },{ name:'end_date', type:'date', label:'Pay Until' }] } ] const ready = values.contact_id && //values.chat_id && values.interval && values.amount && ( (values.endRule==='date' && values.end_date) || (values.endRule==='number' && values.end_number) ) return (
{ const v = { interval: values.interval, contact_id: values.contact_id, amount: values.amount, } if(values.chat_id){ v.chat_id = values.chat_id } if(values.endRule==='date'){ v.end_date = values.end_date } if(values.endRule==='number'){ v.end_number = values.end_number } this.props.onSave(v) }}> {fields.map(f=>{ if(f.type==='contacts'){ return ( ) } if(f.type==='chats'){ return ( ) } if(f.type==='radio'){ return ( {f.options.map(o=>( this.setValue(f.name, o)} /> ))} ) } if(f.type==='checkbox'){ return ( {f.options.map(o=>{ if(o==='custom'){ return ( Custom amount: this.setValue(f.name, parseInt(e.target.value))} type="number" /> ) } return ( this.setValue(f.name, o)} /> ) })} ) } if(f.type==='multi'){ return ( {f.options.map(o=>{ if(o.type==='date'){ return (
this.setValue(f.name, o.type)} />   this.setValue(o.name,a.value.toDateString())} />
) } if(o.type==='number'){ return (
this.setValue(f.name, o.type)} />   this.setValue(o.name, parseInt(e.target.value))} type="number" style={{display:'inline-block',width:'80%',verticalAlign:'middle'}} />
) } })}
) } })}
) } } CreateSubscription.contextType = ctx export default CreateSubscription