WfQuickCommentView.js 15.4 KB
import React, {Component} from 'react';
import {
    View,
    Text,
    TextInput,
    TouchableOpacity, FlatList
} from 'react-native';
import AppService from "../service/AppService";
import {xnToast} from "../utils/utils";
import {NoDoublePress} from "../utils/Common";
import {OrderFlatList} from 'react-native-order-flat-list'
import {width} from "../utils/getSize";
import {SwipeRow} from "react-native-swipe-list-view";
const defaultHeight = (62);
const titleViewHeight = (24);
const HPading = (16);
const VPading = (6);

export default class WfQuickCommentView extends Component {

    //自定义属性类型申明(暂时注掉,RN版本会出现propTypes报错的问题)
    // static propTypes = {
    //     category:React.PropTypes.string,// approve:同意 reject:拒绝
    //     chooseCallback:React.PropTypes.func,// 选择审批意见后的回调
    //     userId:React.PropTypes.string, // 用户id
    // }
    constructor(props) {
        super(props);
        this.maxCount = 10;// 最多允许10条审批意见
        this.userId = !!this.props.userId?this.props.userId:'';
        this.category = !!this.props.category?this.props.category:'approve';
        this.chooseCallback = !!this.props.chooseCallback?this.props.chooseCallback:null;
        this.state = {
            category:!!this.props.category?this.props.category:'approve',
            mode:'choose',// 模式  choose:选择模式  edit:编辑模式  add:新增模式  sort:排序模式
            originData:{}, // 查询到的常用审批意见
            currentItem:[],// 当前处理的数据
        }
        // this.currentItem = [];// 当前处理的数据
        this.swipeRowIndex = '';// 侧滑下标
    }

    componentDidMount() {
        this._query(this.userId);
    }

    _query(userId){
        let _this = this;
        let vm = {
            userId: userId,
            pageSize:this.maxCount,
            pageNum:0,
        };
        AppService.findWfComment(vm).then(data => {
            if (data.message) {
                xnToast(data.message);
                return;
            }
            if (data.errors.length > 0) {
                xnToast(data.errors[0].message);
                return;
            }
            console.log(data);
            _this.setState({
                originData: data.result,
            })
        });
    }

    _update(id,text,rowVersion,userId){
        let _this = this;
        let vm = {
            id: id,
            text: text,
            rowVersion:rowVersion
        };

        AppService.updateWfComment(vm).then(data => {
            _this.setState({
                mode:'choose',
            },()=>{
                if (data.message) {
                    xnToast(data.message);
                    return;
                }
                if (data.errors.length > 0) {
                    xnToast(data.errors[0].message);
                    return;
                }
                console.log(data);
                _this.setState({
                    mode:'choose',
                },()=>{
                    _this._query(userId);
                })
            })
        });
    }

    _delete(id,userId){
        let _this = this;
        let vm = {
            id: id
        };
        AppService.deleteWfComment(vm).then(data => {
            _this.setState({
                mode:'choose',
            },()=>{
                if (data.message) {
                    xnToast(data.message);
                    return;
                }
                if (data.errors.length > 0) {
                    xnToast(data.errors[0].message);
                    return;
                }
                console.log(data);
                _this.setState({
                    mode:'choose',
                },()=>{
                    _this._query(userId);
                })
            })
        });
    }

    _add(text,userId){
        let _this = this;
        let vm = {
            text:text
        };
        AppService.createWfComment(vm).then(data => {
            _this.setState({
                mode:'choose',
            },()=>{
                if (data.message) {
                    xnToast(data.message);
                    return;
                }
                if (data.errors.length > 0) {
                    xnToast(data.errors[0].message);
                    return;
                }
                console.log(data);
                _this.setState({
                    mode:'choose',
                },()=>{
                    _this._query(userId);
                })
            })
        });
    }

    _sort(){

    }

    keyExtractor = (item, index) => index;
    renderItem({item,index}){
        let _this = this;
        const ref = 'SwipeRow' + index;
        if(this.state.mode == 'edit' && this.state.currentItem.id == item.id){
            return (
                <View style={{width:'100%',height:120,paddingVertical:10,
                    paddingHorizontal:16,flexDirection:'row',justifyContent:'flex-start',alignItems:'center',backgroundColor:'#fff'
                }}>
                    <TextInput
                        style={{fontSize: 16,
                            color: 'black',
                            flex:1,
                            backgroundColor: '#fff',
                            textAlign: 'left',
                            marginTop:4,
                            marginBottom:4,
                            padding:0,
                            height:120
                        }}
                        minLength={0}
                        maxLength={100}
                        numberOfLines={6}
                        multiline={true}
                        onChangeText={(text) => {
                            let temp = this.state.currentItem;
                            temp.text = text;
                            this.setState({
                                currentItem:temp,
                            })
                        }}
                        value={this.state.currentItem.text||''}
                        placeholderTextColor={'#999'}
                        placeholder={'0-100字'}
                        underlineColorAndroid="transparent"
                    />
                    <TouchableOpacity activeOpacity={1} style={{width:40,marginLeft:10}} onPress={()=>NoDoublePress.onPress(()=>{
                        if(!!!this.state.currentItem.text || this.state.currentItem.text.length == 0){
                            xnToast('请填写内容');
                            this.setState({
                                mode:'choose',
                            })
                            return;
                        }
                        this._update(this.state.currentItem.id,this.state.currentItem.text,this.state.currentItem.rowVersion,this.userId);
                    })}>
                        <Text numberOfLines={1} style={{fontSize:16,color:"#3399ff",marginRight:0}}>{'保存'}</Text>
                    </TouchableOpacity>
                </View>
            )
        }else{
            return (
                <SwipeRow
                    // closeOnRowPress={true}
                    key={ref}
                    ref={(swipeRow) => {
                        this[ref] = swipeRow;
                    }}
                    leftOpenValue={138}
                    rightOpenValue={-138}
                    stopRightSwipe={-138}
                    disableRightSwipe={true}   //禁止向右滑动
                    onRowOpen={(rowData, rowMap) =>{
                        // 打开侧滑
                        if (this.swipeRowIndex !== 'SwipeRow'+index && this.swipeRowIndex !== '') {
                            this[this.swipeRowIndex].closeRow();
                            this.swipeRowIndex = '';
                        }
                        this.swipeRowIndex = 'SwipeRow'+index;
                    }}
                >
                    <View style={{width: 138, height: 48, alignItems: 'center', flexDirection: 'row', position: 'absolute', right: 0}}>
                        <TouchableOpacity
                            style={{width: 69, height: 48, backgroundColor: 'rgba(255, 141, 26, 1)', justifyContent: 'center', alignItems: 'center'}}
                            activeOpacity={0.8}
                            onPress={() =>NoDoublePress.onPress(()=>{
                                if (this.swipeRowIndex !== '') {
                                    this[this.swipeRowIndex].closeRow();
                                    this.swipeRowIndex = '';
                                }
                                this.setState({
                                    mode: 'edit',
                                    currentItem:item,
                                });
                            })}
                        >
                            <Text style={{fontSize: 14, color: 'rgba(255, 255, 255, 1)'}} allowFontScaling={false}>编辑</Text>
                        </TouchableOpacity>
                        <TouchableOpacity
                            style={{width: 69, height: 48, backgroundColor: 'rgba(212, 48, 48, 1)', justifyContent: 'center', alignItems: 'center'}}
                            activeOpacity={0.8}
                            onPress={() => NoDoublePress.onPress(()=>{
                                if (this.swipeRowIndex !== '') {
                                    this[this.swipeRowIndex].closeRow();
                                    this.swipeRowIndex = '';
                                }
                                this._delete(item.id)
                            })}
                        >
                            <Text style={{fontSize: 14, color: 'rgba(255, 255, 255, 1)'}} allowFontScaling={false}>删除</Text>
                        </TouchableOpacity>
                    </View>
                    <TouchableOpacity
                        style={{flex:1,backgroundColor:'#fff'}}
                        activeOpacity={1}
                        onPress={()=>{
                            if(this.state.mode != 'choose'){
                                if (this.swipeRowIndex !== '') {
                                    this[this.swipeRowIndex].closeRow();
                                    this.swipeRowIndex = '';
                                }
                                this.setState({
                                    mode:'choose'
                                })
                            }else{
                                if(!!this.chooseCallback){
                                    this.chooseCallback(item.text||'');
                                }
                            }
                        }}
                    >
                        <View style={{minHeight: 48,maxHeight:120, flexDirection: 'row', alignItems: 'center', paddingHorizontal: 20, backgroundColor: 'white'}}>
                            <Text style={{flex: 1, fontSize: 14, color: 'rgba(0, 0, 0 , 1)', marginLeft: 10}}>{item.text||''}</Text>
                        </View>
                    </TouchableOpacity>
                </SwipeRow>
            )
        }
    }

    render() {
        let count = this.state.originData.length||0;
        let str = '已添加常用审批意见(' + count + '/' + this.maxCount + ')'
        return (
            <View style={{flex:1, backgroundColor: '#fff', paddingTop: VPading,paddingHorizontal: HPading,height:defaultHeight}}>
                {this.state.mode != 'add' &&<TouchableOpacity activeOpacity={1} style={{width:'100%',height:54,paddingVertical:10,
                    paddingHorizontal:16,flexDirection:'row',justifyContent:'flex-start',alignItems:'center',backgroundColor:'#fff'
                }} onPress={()=>NoDoublePress.onPress(()=>{
                    if(count == this.maxCount){
                        xnToast('最多添加'+ count + '条审批意见');
                    }else{
                        this.setState({
                            mode:'add',
                            currentItem:[]
                        })
                    }
                })}>
                    <Text style={{fontSize:16,color:'#999',flex:1}}>{'+ 常用审批意见'}</Text>
                </TouchableOpacity>}
                {this.state.mode == 'add' &&<View style={{width:'100%',height:120,paddingVertical:10,
                    paddingHorizontal:16,flexDirection:'row',justifyContent:'flex-start',alignItems:'center',backgroundColor:'#fff'
                }}>
                    <TextInput
                        style={{fontSize: 16,
                            color: 'black',
                            flex:1,
                            backgroundColor: '#fff',
                            textAlign: 'left',
                            marginTop:4,
                            marginBottom:4,
                            padding:0,
                            height:120
                        }}
                        minLength={0}
                        maxLength={100}
                        numberOfLines={6}
                        multiline={true}
                        onChangeText={(text) => {
                            let temp = this.state.currentItem;
                            temp.text = text
                            this.setState({
                                currentItem:temp,
                            })
                        }}
                        placeholderTextColor={'#999'}
                        placeholder={'0-100字'}
                        underlineColorAndroid="transparent"
                    />
                    <TouchableOpacity activeOpacity={1} style={{width:40,marginLeft:10}} onPress={()=>NoDoublePress.onPress(()=>{
                        if(this.state.mode == 'add'){
                            if(!!!this.state.currentItem.text || this.state.currentItem.text.length == 0){
                                xnToast('请填写内容');
                                this.setState({
                                    mode:'choose',
                                })
                                return;
                            }
                            this._add(this.state.currentItem.text,this.userId);
                        }else{
                            if (this.swipeRowIndex !== '') {
                                this[this.swipeRowIndex].closeRow();
                                this.swipeRowIndex = '';
                            }
                            this.setState({
                                mode:'choose',
                            })
                        }
                    })}>
                        <Text numberOfLines={1} style={{fontSize:16,color:"#3399ff",marginRight:0}}>{'添加'}</Text>
                    </TouchableOpacity>
                </View>}
                <View style={{width:'100%',height:20,justifyContent:'center'}}>
                    <Text style={{color:'#666',fontSize:12}}>{str}</Text>
                </View>
                <FlatList
                    style={{flex:1,backgroundColor:'#f6f6f6'}}
                    data={this.state.originData}
                    renderItem={this.renderItem.bind(this)}
                    keyExtractor={this.keyExtractor}
                    extraData={this.state}
                    showsVerticalScrollIndicator={false}
                    ItemSeparatorComponent={()=><View style={{width:width,height:1,backgroundColor:'#fff'}} >
                        <View style={{marginLeft:30,width:width-30,height:1,backgroundColor:'#eee'}}/>
                    </View>}
                />
            </View>
        );
    }
}