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 CategoryData from './CategoryData'; import ArrowForwardIcon from 'material-ui/svg-icons/navigation/arrow-forward'; import NavigationArrowBack from 'material-ui/svg-icons/navigation/arrow-back'; import schema from 'async-validator' @inject('appstate') @observer export default class CategoriesDialog extends React.Component { constructor(props) { super(props); this.props = props; this.state = { stepIndex: 0, formData: {}, finished: false, openedDialog: false, formData: {}, errorMessage: '' }; this.defaultState = Object.assign({}, this.state); this.globalUI = props.appstate.globalUI; this.customer = props.appstate.customer; this.categoriesStore = props.appstate.category; } componentDidMount() { this.setState({ stepIndex: 0 }); } onChangeForm(formData) { this.setState({formData}); } getStepContent(stepIndex) { const {mode="create", defaultValue={}} = this.props; switch (stepIndex) { case 0: return ( this.setState({formData})}/> ); // case 1: // return ; } } handleOpen = () => { this.setState({confirmationDialog: true}) }; handleNext = () => { const {stepIndex} = this.state; if (stepIndex === 0) { const rules = { name: [ { required: true, message: 'Please input the name' } ] }; const validator = new schema(rules); validator.validate(this.state.formData, (errs, f) => { console.log(errs); if (errs) { this.globalUI.showNotification("Something's Wrong", errs[0].message); } else { this.setState({ stepIndex: stepIndex + 1, }); } }); } } handlePrev = () => { const {stepIndex} = this.state; if (stepIndex > 0) { this.setState({stepIndex: stepIndex - 1}); } } handleClose = () => { this.setState({confirmationDialog: false}) }; save = () => { this.globalUI.hideDialog(DIALOG.SETTING.CATEGORIES); this.globalUI.showDialogLoading(); const {mode="create", defaultValue={}} = this.props; if (mode === "create") { let data = this.state.formData; data.image = { icon : this.state.formData.icon, background_image : this.state.formData.background_image } delete data.icon; delete data.background_image; this.categoriesStore.postCategory(data) .then(res => { this.globalUI.hideDialogLoading(); this.globalUI.openSnackbar("Success Added New Category"); this.setState({ stepIndex: 0 }) }) .catch(err => { console.error(err); }); } else if (mode === "update") { let data = this.state.formData; data.image = { icon : this.state.formData.icon, background_image : this.state.formData.background_image } delete data.icon; delete data.background_image; this.categoriesStore.putCategory(defaultValue.id, data) .then(res => { // this.globalUI.hideDialog(DIALOG.EMPLOYEE.CREATE); this.globalUI.hideDialogLoading(); this.globalUI.openSnackbar("Success Updated Category") this.setState({ stepIndex: 0 }) // this.categoriesStore.getAll(); }) .catch(err => { this.globalUI.openSnackbar("Error, Check log for detail"); console.error(err); }); } } handlePost = () => { // this.agentStore.post().then(res => { // this.props.history.push(LINKS.SETTING); // this.globalUI.openSnackbar("Successfull Added Employee"); // }); }; continueButton() { if (this.state.stepIndex === 1) { return ( ); } else { return ( ); } } render() { const {finished, stepIndex} = this.state; const {mode="create", defaultValue={}} = this.props; const actions = [ , ] const title =

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

; return (
{title}
Category Data {/* Identification & Passport */}
} titleStyle={{paddingBottom: 10}} modal={false} actions={
(stepIndex === 0) ? this.globalUI.hideDialog(DIALOG.SETTING.CATEGORIES) : this.handlePrev()} /> {this.continueButton()}
} autoScrollBodyContent={true} repositionOnUpdate={true} open={this.globalUI[DIALOG.SETTING.CATEGORIES]} onRequestClose={this.handleClose} style={{zIndex: 999}} >
{this.getStepContent(stepIndex)}
this.handleClose()} > {this.state.errorMessage} ) } }