calendar.js 7.53 KB
/**
 * Created by Cassie on 2018/05/14
 */
import React, { Component } from 'react';
import { StyleSheet, View, Text, TouchableOpacity, Image, TouchableWithoutFeedback, ScrollView, DeviceEventEmitter } from 'react-native';

import _ from 'lodash';
import moment from 'moment';
import Timeline from 'react-native-timeline-listview';

import { width, height, zoomW, zoomH } from '../utils/getSize';
import AppService from '../service/AppService';
import Calendar from '../components/Calendar/Index';

import IMG_TaskEmpty from '../img/taskEmpty.png';

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

        const currentDate = moment().format('YYYY-MM-DD');
        this.minDate = moment().subtract(1, 'years').format('YYYY-MM-DD');
        this.maxDate = moment().add(1, 'years').format('YYYY-MM-DD');

        this.state = {
            currentDate,
            minDate: this.minDate,
            maxDate: this.maxDate,
            planList: [],
            markedDates: [],
        };
    }

    getPlanList = () => {
        const dateFrom = moment(this.minDate).format('YYYY-MM-DD HH:mm:ss');
        const dateTo = moment(this.maxDate).format('YYYY-MM-DD HH:mm:ss');

        AppService.getSchedule({
            pageNumber: 0,
            pageSize: 0,
            dateFrom,
            dateTo,
            userId: global.userId,
        }).then((res) => {
            const dataList = res.result;
            if (dataList && dataList.length > 0) {
                const markedDates = [];

                const planList = _.map(dataList, (item) => {
                    let { beginTime, endTime } = item;
                    beginTime = moment(parseFloat(beginTime));
                    endTime = moment(parseFloat(endTime));

                    const beginTime1 = beginTime.format('YYYY-MM-DD HH:mm:ss');
                    const endTime1 = endTime.format('YYYY-MM-DD HH:mm:ss');

                    // const temp = endTime.diff(beginTime, 'hours');
                    // if (temp >= 24) { // 跨天
                        
                    // } else {
                        
                    // }

                    markedDates.push(beginTime1);
                    markedDates.push(endTime1);

                    return { 
                        ...item, 
                        beginTime: beginTime1, 
                        endTime: endTime1,
                    };
                });

                this.setState({ planList, markedDates, });
            }
        });
    }

    componentDidMount() {
        this.getPlanList();

        let that = this;
        this.reloadPlanList = DeviceEventEmitter.addListener('reloadPlanList', function () {
            that.getPlanList();
        });
    }

    componentWillUnmount() {
        if (this.reloadPlanList) {
            this.reloadPlanList.remove();
        }
    }

    handleItemPress = (rowData) => {
        const { id } = rowData;
        this.props.navigation.navigate('EditSchedule', { id, });
    }

    render() {
        const { currentDate, minDate, maxDate, planList, markedDates } = this.state;

        let currentPlanList;
        if (planList && planList.length > 0) {
            currentPlanList = 
                _.filter(planList, (item) => {
                    const temp1 = moment(currentDate);
                    const temp2 = temp1.isSame(item.beginTime, 'year') && temp1.isSame(item.beginTime, 'month') && temp1.isSame(item.beginTime, 'day');
                    const temp3 = temp1.isSame(item.endTime, 'year') && temp1.isSame(item.endTime, 'month') && temp1.isSame(item.endTime, 'day');

                    return temp2 || temp3;
                }) || [];
        }
        
        let timelineDataList = [];
        if (currentPlanList && currentPlanList.length > 0) {
            timelineDataList = 
                _.map(currentPlanList, (item) => {
                    const { 
                        id, calendarId, userId, userName, 
                        title, address, description, 
                        isAllDay, beginTime, endTime 
                    } = item;

                    return {
                        id,
                        time: moment(beginTime).format('hh:mm'),
                        title, 
                        description: address ? address : '无日程地点',
                    };
                });
        }

        return (
            <View style={styles.calendarScreenWrap}>
                <Calendar 
                    markedDates={markedDates} 
                    minDate={minDate}
                    maxDate={maxDate}
                    onDaySelected={(date) => {
                        this.setState({ currentDate: date, });
                    }} 
                />

                <View style={{ display: 'flex', flex: 1, width: '100%', padding: 16, }}>
                    { currentPlanList && currentPlanList.length > 0 
                        ? <Timeline
                            showTime={true}
                            data={timelineDataList}
                            innerCircle={'dot'}
                            circleColor="#ff9797"
                            circleSize={16}
                            lineColor="#ff9797"
                            timeContainerStyle={{ minWidth: 48, }}
                            timeStyle={{ textAlign: 'center', backgroundColor: '#ff9797', color: 'white', fontWeight: '500', borderRadius: 12 }}
                            detailContainerStyle={{ paddingTop: 0, }}
                            titleStyle={{ marginTop: -12, color: '#555', fontSize: 14, }}
                            descriptionStyle={{ marginTop: 4, paddingLeft: 2, paddingBottom: 24, color: '#999', fontSize: 14, }}
                            rowContainerStyle={{  }}
                            options={{ style: { paddingTop: 8, marginBottom: 56, } }}
                            onEventPress={(rowData) => this.handleItemPress(rowData)}
                        />
                        : <View 
                            style={{
                                width: '100%',
                                height: '100%',
                                display: 'flex',
                                alignItems: 'center',
                                paddingTop: 30 / zoomH,
                            }}>
                                <Image 
                                    source={IMG_TaskEmpty} 
                                    resizeMode="contain" 
                                    style={{ marginBottom: 20 / zoomH, }} 
                                />
                                <Text style={{ fontSize: 14, color: '#666', }}>还没有日程呢</Text>
                                <Text style={{ fontSize: 14, color: '#00be3c', marginTop: 2 / zoomH, }}>点击右下角创建一个吧</Text>
                            </View> 
                        }
                </View>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    calendarScreenWrap: {
        position: 'relative',
        display: 'flex',
        flexDirection: 'column',
        flex: 1,
        backgroundColor: '#fff',
    },
    calendarHeaderWrap: {
        display: 'flex',
        flexDirection: 'row',
        height: 40 / zoomH,
    },
    calendarHeaderTitle: {
        flex: 1,
        height: '100%',
    },
    headerTitle: {
        color: '#333',
        fontSize: 18,
    },
    calendarHeaderAction: {
        width: 80 / zoomH,
        height: '100%',
        backgroundColor: '#f3f3f3',
    },
    actionText: {
        color: '#666',
        fontSize: 16,
    },
});