ReplyPopWin.js 5.9 KB
import React, {Component} from 'react';
import StorageUtil from '../utils/StorageUtil';
import Utils from '../utils/Utils';
import {xnToast} from "../../utils/utils";
import AppService from "../../service/AppService";

import {Button, Dimensions, Modal, StyleSheet, TextInput, TouchableOpacity, View, NativeModules} from 'react-native';

const {width} = Dimensions.get('window');

export default class ReplyPopWin extends Component {
    constructor(props) {
        super(props);

        this.state = {
            show: false,
            inputContent: '',
            name: ''
        };
        StorageUtil.get('username', (error, object) => {
            if (!error && object != null) {
                this.setState({username: object.username});
            }
        });
    }

    componentDidMount() {
        let input = this.refs.textInput;
        if (!Utils.isEmpty(input)) {
            input.focus();
        }
    }

    componentWillMount() {
        NativeModules.security.getTenantId().then((result) => {
                global.tenantId = result;
            }
        ).catch((error) => {
            console.log(error)
        });
    }
W
    render() {
        //console.log("param" + this.state.param.momentUserName);
        //(this.state.param.commentUserName && this.state.param.momentUserName)
        let name = this.state.name;
        let placeholderText = "回复" + name;
        return (
            <View style={styles.container}>
                <Modal transparent={true}
                       visible={this.state.show}
                       onRequestClose={() => this.closeModal()}>
                    <TouchableOpacity onPress={() => this.closeModal()} style={styles.modalContainer}>
                        <View style={styles.modalContainer}>
                            <View style={{
                                flexDirection: 'row',
                                alignItems: 'center',
                                backgroundColor: '#EEEEEE',
                                paddingLeft: 10,
                                paddingRight: 10
                            }}>
                                <TextInput
                                    style={{flex: 1}}
                                    ref="textInput"
                                    placeholder={placeholderText}
                                    autoFocus={true}
                                    onChangeText={(text) => this.setState({inputContent: text})}
                                />
                                {
                                    !Utils.isEmpty(this.state.inputContent) ? (
                                        <Button color={'#49BC1C'} title={"回复"}
                                                onPress={() => this.handleBtnClick()}/>
                                    ) : (null)
                                }
                            </View>
                        </View>
                    </TouchableOpacity>
                </Modal>
            </View>
        );
    }

    handleBtnClick() {
        this.sendPost();
    }

    sendPost() {
        let momentId = this.state.momentId;
        let replyContent = this.state.inputContent;
        if (Utils.isEmpty(replyContent)) {
            xnToast('回复内容不能为空');
            return;
        }
        let params = {
            objectId: momentId,
            objectType: "MOMENT",
            commentContent: replyContent,
            tenantId: global.tenantId,
            commentUserName: this.state.param.myName,
            toCommentId: this.state.param.toCommentId
        };


        AppService.createComment(params).then((data) => {
            if (data.message) {
                xnToast(data.message);
                return;
            }
            if (data.errors.length > 0) {
                xnToast(data.errors[0].message);
            } else {
                this.closeModal();
                // 刷新回复列表
                let data = {
                    commentContent: replyContent,
                    objectId: momentId,
                    commentUserName: this.state.param.myName,
                    toCommentId: this.state.toCommentId,
                    toCommentUserName:null
                }
                if(this.state.toCommentId){
                    data.toCommentUserName = this.state.commentUserName;
                }
                let param = {
                    momentId: momentId,
                    momentIndex: this.state.momentIndex
                }
                this.refreshReply(param, data);
            }
        }).catch((error) => {
            this.closeModal();
            xnToast(error)
        })
    }

    refreshReply(param, data) {
        let callback = this.state.successCallback;
        if (!Utils.isEmpty(callback)) {
            callback(param, data);
        }
    }

    closeModal() {
        this.setState({show: false})
    }

    //momentId, commentId, momentUsername, momentIndex, commentUserName
    showModal( param, successCallback) {
        //console.log(momentId + "-" +commentId + "-" + momentUsername + "-" + momentIndex)

        let toCommentId =  param.commentId;
        let name = param.commentUserName ? param.commentUserName: param.momentUserName;
        if(param.commentUserId === global.unionId){
            toCommentId = null;
            name = param.momentUserName;
        }


        this.setState({
            param: param,
            momentId: param.momentId,
            toCommentId:toCommentId,
            momentUserName: param.momentUserName,
            momentIndex: param.momentIndex,
            commentUserName:param.commentUserName,
            show : true,
            name : name,
            successCallback: successCallback
        });
    }
}

const styles = StyleSheet.create({
    container: {
        width: width
    },
    modalContainer: {
        flex: 1,
        flexDirection: 'column',
        justifyContent: 'flex-end'
    }
});