import React, {Component} from 'react'; import { Platform, StyleSheet, Text, View, ScrollView, DeviceEventEmitter, Keyboard } from 'react-native'; import {Navigation} from 'react-native-navigation'; import KeyboardSpacer from "react-native-keyboard-spacer"; import {Button} from 'react-native-elements'; import Color from '../style/color'; import {Icon} from '../style/icon'; import Msg from '../util/msg'; import Api from '../util/api'; import Event from '../util/event'; import DiaryFull from '../component/diary/diaryFull'; import DiaryAction from '../component/diary/diaryAction'; import CommentInput from '../component/comment/commentInput'; function getTodayStr() { let now = new Date(); let year = now.getFullYear(); let month = now.getMonth() + 1; let date = now.getDate(); return year + '-' + (month > 9 ? month : '0' + month) + '-' + date; } export default class DiaryDetailPage extends Component { constructor(props) { super(props); Navigation.events().bindComponent(this); this.state = { selfInfo: null, diaryId: props.diaryId, diary: props.diary, user: props.user, isMine: false, todayCreated: false, expired: props.expired || false, needScrollToBottom: false, error: null } } static options(passProps) { let topBar = { title: { text: '日记详情' } } return { topBar }; } navigationButtonPressed({buttonId}) { if(!this.state.selfInfo || !this.state.diary) { return; } if(this.state.isMine) { let componentId = this.props.componentId; DiaryAction.action(componentId, this.state.diary, { onDelete: () => { Navigation.pop(componentId); } }); } else { ActionSheet.showActionSheetWithOptions({ options: ['举报', '取消'], cancelButtonIndex: 1, destructiveButtonIndex: 0 }, (index) => { if(index == 0) { Api.report(this.state.diary.user_id, this.state.diary.id).done(); Msg.showMsg('举报成功,感谢你的贡献 :)'); } }); } } componentDidMount() { Api.getSelfInfoByStore() .then(user => { this.setState({ selfInfo: user }, () =>{ this.refreshDiary(this.state.diary); }); }).done(); this.diaryListener = DeviceEventEmitter.addListener(Event.updateDiarys, (param) => { if(param != 'del') { this.refreshDiary(); } }); this.commentListener = DeviceEventEmitter.addListener(Event.updateComments, (param) => { this.setState({needScrollToBottom: true}); this.refreshDiary(); this.diaryFull.refreshComment(); Keyboard.dismiss(); }); } componentWillUnmount(){ this.diaryListener.remove(); this.commentListener.remove(); } async refreshDiary(diary) { if(!diary) { let diaryId = this.state.diaryId; if(!diaryId) { diaryId = this.state.diary.id; } try { diary = await Api.getDiary(diaryId); } catch(e) { this.setState({ error: e }); return; } } let isMine = this.state.selfInfo.id == diary.user_id; let createdTime = diary.created; let todayCreated = getTodayStr() == createdTime.substring(0, createdTime.indexOf(' ')); this.setState({ diary, isMine, todayCreated }, () => { if(this.props.refreshBack) { this.props.refreshBack(diary); } if(todayCreated || (this.props.expired && !isMine)) { Navigation.mergeOptions(this.props.componentId, { topBar: { rightButtons: [{ id: 'navButtonMore', icon: Icon.navButtonMore, color: Color.primary // android }] } }); } }); } render() { if(this.state.error) { return ( {'日记加载失败:('}