targetHistory.js 8.63 KB
/**
 * Created by Cassie on 2018/05/14
 */
import React, {Component} from "react";
import {StyleSheet, View, Text, TouchableOpacity, Image, ScrollView, ActivityIndicator} from "react-native";
import moment from "moment";
import {width, zoomW, zoomH} from "../../utils/getSize";
import {NoDoublePress, xnToast} from "../../utils/utils";
import AppService from "../../service/AppService";

const back = require('../../img/returnB.png');
const avatar = require('../../img/avatar.png');
const taskEmpty = require('../../img/taskEmpty.png');
const approved = require('../../img/approved.png');

export default class TargetHistory extends Component {
    static navigationOptions = ({navigation, screenProps}) => ({
        title: '编辑历史',
        headerLeft: (<View style={{flex: 1, display: 'flex', flexDirection: 'row'}}>
            <TouchableOpacity style={styles.topIcon}
                              onPress={navigation.state.params ? navigation.state.params.back : null}>
                <Image source={back} style={{width: (16 / zoomH), height: (16 / zoomH)}} resizeMode="contain"/>
            </TouchableOpacity>
        </View>),
        headerRight: (<View style={{flex: 1, display: 'flex', flexDirection: 'row'}}/>)
    });

    constructor(props) {
        super(props);
        this.state = {
            sheetList: []
        };
    };

    componentWillMount() {

    };

    componentDidMount() {
        this.props.navigation.setParams({
            back: () => {
                NoDoublePress.onPress(() => {
                    this.props.navigation.goBack();
                })
            }
        });
        this.getList();
    };

    /*获取列表数据*/
    getList() {
        let params = {
            taskId: this.props.navigation.state.params.objectId
        };
        if (global.isConnected) {
            this.setState({
                loading: true
            });
            AppService.getHistoryList(params).then((data) => {
                this.setState({
                    loading: false
                });
                if (data.message) {
                    xnToast(data.message);
                    return;
                }
                if (data.errors.length > 0) {
                    xnToast(data.errors[0].message);
                } else {
                    this.setState({
                        sheetList: data.result
                    })
                }
            }).catch((error) => {
                this.setState({
                    loading: false
                });
                xnToast(error)
            })
        } else {
            xnToast('暂无网络连接,请稍后重试!')
        }
    }

    /*渲染列表*/
    renderList(item, index) {
        return (
            <View key={index} style={{width: '100%'}}>
                <View style={styles.listItem}>
                    <Image
                        source={item.avatarUrl ? {uri: item.avatarUrl + '?x-oss-process=image/resize,w_500'} : avatar}
                        style={{width: (36 / zoomH), height: (36 / zoomH), borderRadius: (18 / zoomH)}}
                        resizeMode="cover"/>
                    <View style={{marginLeft: (10 / zoomW)}}>
                        <Text style={{fontSize: 17, color: '#576b95',marginBottom:(6/zoomH)}}>{item.saveUserName}</Text>
                        <Text style={{
                            fontSize: 14,
                            color: '#666'
                        }}>{moment(Number(item.saveTime)).format('YYYY-MM-DD HH:mm')}</Text>
                        <Text style={{
                            fontSize: 16,
                            color: '#333',
                            marginTop: (18 / zoomH),
                            marginBottom: (18 / zoomH)
                        }}>{item.afterObjectName}</Text>
                        <View style={styles.historyContent}>
                            <View style={{
                                display: 'flex',
                                flexDirection: 'row',
                                alignItems: 'center',
                                marginBottom: (6 / zoomH)
                            }}>
                                <Image source={approved}
                                       style={{width: (14 / zoomH), height: (14 / zoomH), marginRight: (6 / zoomW)}}
                                       resizeMode="contain"/>
                                <Text style={{
                                    fontSize: 14,
                                    color: '#666'
                                }}>{moment(Number(item.beforeStartTime)).format('YYYY-MM-DD HH:mm') + '至' + moment(Number(item.beforeEndTime)).format('YYYY-MM-DD HH:mm')}</Text>
                            </View>
                            <Text style={{fontSize: 14, color: '#666'}}>{item.beforeObjectDescription}</Text>
                        </View>
                        <View style={styles.historyContent}>
                            <View style={{
                                display: 'flex',
                                flexDirection: 'row',
                                alignItems: 'center',
                                marginBottom: (6 / zoomH)
                            }}>
                                <Image source={approved}
                                       style={{width: (14 / zoomH), height: (14 / zoomH), marginRight: (6 / zoomW)}}
                                       resizeMode="contain"/>
                                <Text style={{
                                    fontSize: 14,
                                    color: '#666'
                                }}>{moment(Number(item.beforeStartTime)).format('YYYY-MM-DD HH:mm') + '至' + moment(Number(item.afterEndTime)).format('YYYY-MM-DD HH:mm')}</Text>
                            </View>
                            <Text style={{fontSize: 14, color: '#666'}}>{item.afterObjectDescription}</Text>
                        </View>
                    </View>
                </View>
                <View style={{width: '100%', height: (10 / zoomH), backgroundColor: '#ececf1'}}/>
            </View>
        )
    }

    componentWillUnmount() {
        this.setState = (state, callback) => {
            return;
        };
    };

    render() {
        return (
            <View style={styles.background}>
                <ScrollView style={{width: '100%'}}>
                    {this.state.sheetList && this.state.sheetList.map((item, index) => this.renderList(item, index))}
                </ScrollView>
                {this.state.loading && <View style={styles.loadingBg}>
                    <View style={styles.loadingBox}>
                        <ActivityIndicator size='large' color='#fff'/>
                        <Text style={{fontSize: 16, color: '#fff', marginTop: (6 / zoomH)}}>加载中...</Text>
                    </View>
                </View>}
                {!this.state.loading && this.state.sheetList.length == 0 && <View style={{
                    width: '100%',
                    height: '100%',
                    display: 'flex',
                    alignItems: 'center',
                    paddingTop: (150 / zoomH),
                    backgroundColor: '#fff'
                }}>
                    <Image source={taskEmpty} resizeMode="contain" style={{marginBottom: (20 / zoomH)}}/>
                    <Text style={{fontSize: 14, color: '#666'}}>还没有任何编辑历史~</Text>
                </View>}
            </View>
        );
    }
}

const styles = StyleSheet.create({
    topIcon: {
        paddingLeft: (10 / zoomW),
        paddingRight: (10 / zoomW),
        height: '100%',
        display: 'flex',
        justifyContent: 'center'
    },
    background: {
        flex: 1,
        backgroundColor: '#ececf1',
        display: 'flex',
        alignItems: 'center'
    },
    listItem: {
        width: '100%',
        padding: (15 / zoomW),
        backgroundColor: '#fff',
        display: 'flex',
        flexDirection: 'row',
    },
    loadingBg: {
        position: 'absolute',
        top: 0,
        width: '100%',
        height: '100%',
        display: 'flex',
        justifyContent: 'center',
        alignItems: 'center'
    },
    loadingBox: {
        width: (100 / zoomW),
        height: (120 / zoomH),
        backgroundColor: 'rgba(0,0,0,.5)',
        borderRadius: 8,
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center'
    },
    historyContent: {
        width: width - (70 / zoomW),
        paddingTop: (10 / zoomH),
        paddingBottom: (10 / zoomH),
        paddingRight: (15 / zoomH),
        borderTopWidth: StyleSheet.hairlineWidth,
        borderTopColor: '#ddd',
        borderStyle: 'solid'
    }
});