TemplateSave.js 4.3 KB
Newer Older
fukai committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
import React,{Component} from 'react'
import {Button,Table,Modal,Row,Col,Form,Input,Icon,notification} from 'antd'

import ServiceAPI from './ServiceAPI'

const createForm = Form.create;
const FormItem = Form.Item;


const formItemLayout = {
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
    };


export default class TemplateSaver extends Component{

    constructor(props)
    {
        super(props)
        this.state = {visible:false,loading:false}
    }

     showModal=()=> {

        this.setState({
            visible: true,
        });
    }
    handleOk=(e)=>{
        
        //console.log(this.inp.getForm().submit())
        this.inp.validateFields((errors, values) => {
            if (!!errors) {
                console.log('Errors in form!!!');
                return;
            }
            this.onSave(values)
        });
        //.handleSubmit(e)
    }
    onSave=vals=>{
        this.setState({ loading: true });
        //调用模板保存API
        let tempJsn = this.props.getTemplateJSON()
fukai committed
46
        let json = {...vals,sf2temp:tempJsn,mty:this.props.mty,user:this.props.mctid||'NONE'}
fukai committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        console.log(json)
        ServiceAPI.saveTemplate(json).then(data=>{
            if(typeof data=='string')
            {
                    data = JSON.parse(data)
            }
            if(data.errorCode !== '0000')
            {
                notification.error({description:'模板保存失败!',message:'错误'})
            }
            else
            {
                notification.success({description:'模板保存成功!',message:'提示'})
            }
            this.setState({ loading: false,visible:false });
            
        }).catch(err=>{
            notification.error({description:'模板保存失败!',message:'错误'})
            this.setState({ loading: false,visible:false });
        })
    }
    handleCancel=()=>{
        this.setState({ visible: false });
    }
    
    render()
    {
        
        return (
           <div >
            <Button style={this.props.style} icon="save" type="primary" onClick={this.showModal}>
            Template
            </Button>
            <Modal ref="modal"
            visible={this.state.visible}
82
            title="Save template" onOk={this.handleOk} onCancel={this.handleCancel}
fukai committed
83
            footer={[
84
                <Button key="back" type="ghost" size="large" onClick={this.handleCancel}>Back</Button>,
fukai committed
85
                <Button key="submit" type="primary" size="large" loading={this.state.loading} onClick={this.handleOk}>
86
                Submit
fukai committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
                </Button>,
            ]}
            >
            <TForm ref={ref=>this.inp=ref}  onSaveTemp={this.onSave}/>
            </Modal>
        </div>


        )
    }
}


class TempForm extends Component{

    handleSubmit=e=>{
        e.preventDefault();
        this.props.form.validateFields((errors, values) => {
            if (!!errors) {
                console.log('Errors in form!!!');
                return;
            }
            this.props.onSaveTemp(values)
        });

    }

    handleReset=(e)=>{
        e.preventDefault();
        this.props.form.resetFields();
    }

    render(){
        const { getFieldProps, getFieldError, isFieldValidating } = this.props.form;
        const nameProps = getFieldProps('nam', {
            rules: [
123
                { required: true, message: 'Fill in the template name' },
fukai committed
124 125 126 127
            ],
        });
        const textareaProps = getFieldProps('txt', {
            rules: [
128
                { required: true, message: 'Fill in message Remarks' },
fukai committed
129 130 131 132 133 134
            ],
        });
        return (
            <Form horizontal form={this.props.form}>
                 <FormItem
                    {...formItemLayout}
135
                    label="Template name"
fukai committed
136 137
                    hasFeedback
                    >
138
                        <Input name="nam" {...nameProps} type="text" maxLength={10} placeholder="Please fill in the template name" />
fukai committed
139 140 141
                    </FormItem>
                <FormItem
                    {...formItemLayout}
142
                    label="Remarks"
fukai committed
143
                    >
144
                        <Input {...textareaProps} type="textarea" placeholder="Fill in message Remarks"  name="txt" />
fukai committed
145 146 147 148 149 150 151 152 153 154
                    </FormItem>
                
            </Form>

        )
    }
}


 const TForm = createForm()(TempForm);