import React from 'react'; import {observer, inject} from 'mobx-react'; import { Dialog, FlatButton, RaisedButton, Step, StepLabel, Stepper, IconButton } from "material-ui"; import {DIALOG} from "../../../../stores/global_ui"; import schema from 'async-validator' import AddressDetail from './Address'; @inject('appstate') @observer export default class AddressAdd extends React.Component { constructor(props) { super(props); this.props = props; this.state = { stepIndex: 0, finished: false, openedDialog: false, openMaps : false, confirmationDialog : false, formData: {}, errorMessage: '', default:{ is_shipment : true, is_default : true } }; this.defaultState = Object.assign({}, this.state); this.globalUI = props.appstate.globalUI; this.userAddress = props.appstate.userAddress; this.userData = props.appstate.userData; } componentDidMount(){ this.setState({ stepIndex:0 }); } componentWillReceiveProps(nextProps){ this.setState({stepIndex : 0,formData : nextProps.defaultValue, default : nextProps.defaultValue}); } getStepContent(stepIndex) { const {mode="create", defaultValue={}} = this.props; switch (stepIndex) { case 0: return ( this.setState({formData})}/> ); } } handleOpen = () => { this.setState({confirmationDialog: true}) }; handleNext = () => { const {stepIndex} = this.state; if (stepIndex === 0) { const rules = { name: [ { required: true, message: "Please fill address name" } ], address: [ { required: true, message: 'Please fill address', } ], province_id: [ { required: true, message: 'Please select province', } ], city_id: [ { required: true, message: 'Please select city', } ], phone_number: [ { required: true, message: 'Please fill phone number', } ], postal_code: [ { required: true, message: 'Please fill postal code', } ], }; const validator = new schema(rules); console.log(this.state.formData); validator.validate(this.state.formData, (errs, f) => { console.log(errs); if (errs) { this.globalUI.showNotification("Something's Wrong", errs[0].message); } else { // let regex=/^(\([0-9]{4}\)\s*|[0-9]{4}\-|\([0-9]{3}\)\s*|[0-9]{3}\-|[0-9]{11,15})([0-9]{8}|\s*)$/; let regex=/^(^\+62\s?|^0)(\d{3,4}-?){2}\d{3,4}$/g; if(this.state.formData.phone_number.match(regex)) { this.setState({ stepIndex: stepIndex + 1, }); } else{ this.globalUI.showNotification("Phone number should be correct !"); } } }); } } handlePrev = () => { const {stepIndex} = this.state; if (stepIndex > 0) { this.setState({stepIndex: stepIndex - 1}); } } save = () => { this.globalUI.hideDialog(DIALOG.ADDRESS.CREATE); this.globalUI.showDialogLoading(); const {mode="create", defaultValue={}} = this.props; var data = this.state.formData; data.lat = this.userAddress.geoData.lat; data.long = this.userAddress.geoData.lng; data.geoaddress = this.userAddress.geoData.geoaddress; if(this.userData.role == 'store') { if (mode === "create") { this.userAddress.addAddress(data).then(res=>{ this.globalUI.openSnackbar('Successfully add new Address'); }).catch((err)=> { console.log(err); this.globalUI.closeLoading(); }) } else if (mode === "update") { this.userAddress.updateAddress(data).then(res=>{ this.globalUI.openSnackbar('Successfully update Address'); }).catch(err=>{ console.log(err); this.globalUI.openSnackbar('Something Error'); }) } } else if(this.userData.role == 'administrator'){ if (mode === "create") { this.userAddress.addAddressAdmin(this.props.id,data).then(res=>{ this.globalUI.openSnackbar('Successfully add new Address'); }).catch((err)=> { console.log(err); this.globalUI.closeLoading(); }) } else if (mode === "update") { this.userAddress.updateAddressAdmin(this.props.id,data).then(res=>{ this.globalUI.openSnackbar('Successfully update Address'); }).catch(err=>{ console.log(err); this.globalUI.openSnackbar('Something Error'); }) } } } handleClose = () => { this.setState({confirmationDialog: false,formData : {}}) }; continueButton() { if (this.state.stepIndex === 1) { return ( ); } else { return ( ); } } openWarningDialog = ()=>{ // this.userAddress.deletedAddress(data.id).then(res=>{ // this.globalUI.openSnackbar('Delete Success'); // }) // console.log(data); this.setState( { confirmationDialog :true,errorMessage: 'Are you sure want to delete this address?'} ) ;}; deletedAddress = (data)=>{ this.setState({confirmationDialog:false}); this.globalUI.hideDialog(DIALOG.ADDRESS.CREATE); if(this.userData.role == 'store') { this.userAddress.deletedAddress(data.id).then(res=>{ this.globalUI.openSnackbar('Delete success') }); } else if(this.userData.role == 'administrator'){ this.userAddress.deletedAddressAdmin(this.props.id,data.id).then(res=>{ this.globalUI.openSnackbar('Delete success') }); } }; render() { const {finished, stepIndex} = this.state; const {mode="create", defaultValue={}} = this.props; const actions = [ , this.deletedAddress(this.state.formData)} /> ]; const title =

{(mode === "create") ? "Add New" : "Update"} Address

; return (
{title}
{/*
*/} {/**/} {/**/} {/*Bank Account*/} {/**/} {/*/!* */} {/*Identification & Passport*/} {/* *!/*/} {/**/} {/*
*/}
} titleStyle={{paddingBottom: 10}} modal={false} actions={
(stepIndex === 0) ? this.globalUI.hideDialog(DIALOG.ADDRESS.CREATE) : this.handlePrev()} /> {this.continueButton()} {(this.state.default.is_shipment != true && this.state.default.is_default != true) ? this.openWarningDialog()} /> : ''}
} autoScrollBodyContent={true} repositionOnUpdate={true} open={this.globalUI[DIALOG.ADDRESS.CREATE]} onRequestClose={this.handleClose} paperClassName={'radius6'} contentClassName={'marketplace-dialog-small'} >
{this.getStepContent(stepIndex)}
this.handleClose()} > {this.state.errorMessage} ) } }