1. 代码整理,bug修复

This commit is contained in:
xuwenyang 2019-06-21 23:11:00 +08:00
parent 5a4a8c9f78
commit 6a3c7333ad
12 changed files with 192 additions and 84 deletions

View file

@ -10,10 +10,25 @@ import UserIcon from '../userIcon';
export default class Comment extends Component {
constructor(props) {
super(props);
this.state = {
comment: props.comment,
isMyDiary: props.isMyDiary || false,
isMyComment: props.isMyComment || false,
expired: props.expired || false
};
}
render() {
let comment = this.props.comment;
let comment = this.state.comment;
let user = comment.user;
let editable = this.props.editable;
let isMyDiary = this.state.isMyDiary;
let isMyComment = this.state.isMyComment;
let expired = this.state.expired;
return (
<View>
@ -39,7 +54,7 @@ export default class Comment extends Component {
</View>
{
editable
(isMyDiary || isMyComment) && !expired
? <TouchableOpacity onPress={this.props.onCommentAction} style={localStyle.moreIcon}>
<Ionicons name="ios-more" size={24} color={Color.inactiveText} />
</TouchableOpacity>

View file

@ -17,7 +17,7 @@ import Touchable from '../touchable';
import Color from '../../style/color';
import Api from '../../util/api';
import Event from '../../util/event';
import Msg from '../../util/msg'
import Msg from '../../util/msg';
import Comment from './comment';
@ -27,14 +27,27 @@ export default class CommentList extends Component {
constructor(props) {
super(props);
this.diaryId = props.diaryId;
this.editable = props.editable || false;
this.state = {
diaryId: props.diaryId,
selfInfo: props.selfInfo,
isMine: props.isMine || false,
expired: props.expired || false,
comments: []
};
}
componentWillReceiveProps(nextProps) {
this.setState({
diaryId: nextProps.diaryId,
selfInfo: nextProps.selfInfo,
isMine: nextProps.isMine || false,
expired: nextProps.expired || false
});
}
componentDidMount() {
InteractionManager.runAfterInteractions(() => {
this.refresh();
@ -42,7 +55,7 @@ export default class CommentList extends Component {
}
async refresh() {
let comments = await Api.getDiaryComments(this.diaryId);
let comments = await Api.getDiaryComments(this.state.diaryId);
if(comments && comments.length > 0) {
if (comments.length > 1) {
comments = comments.reverse();
@ -59,6 +72,10 @@ export default class CommentList extends Component {
}
_onUserIconPress(comment) {
if(this.state.selfInfo.id == comment.user_id) {
return;
}
Navigation.push(this.props.componentId, {
component: {
name: 'User',
@ -114,6 +131,8 @@ export default class CommentList extends Component {
}
render() {
let selfInfo = this.state.selfInfo;
return (
<View style={localStyle.container}>
<FlatList ref={(r) => this.list = r}
@ -127,7 +146,11 @@ export default class CommentList extends Component {
renderItem={({item}) => {
return (
<Touchable onPress={() => this._onCommentPress(item)}>
<Comment comment={item} editable={this.editable}
<Comment comment={item}
isMyDiary={this.state.isMine}
isMyComment={selfInfo.id == item.user_id}
expired={this.state.expired}
onUserIconPress={() => this._onUserIconPress(item)}
onCommentAction={() => this._onCommentAction(item)}>
</Comment>

View file

@ -19,11 +19,11 @@ export default class DiaryBrief extends Component {
super(props);
this.state = {
diary: props.diary
}
diary: props.diary,
this.expired = props.expired || false;
this.editable = props.editable || false;
isMine: props.isMine || false,
expired: props.expired || false
}
this.showField = ['userIcon', 'userName', 'subject', 'createdTime'];
if(props.showField && props.showField.length > 0) {
@ -51,6 +51,9 @@ export default class DiaryBrief extends Component {
return null;
}
let isMine = this.state.isMine;
let expired = this.state.expired;
let user = diary.user;
return (
@ -91,13 +94,13 @@ export default class DiaryBrief extends Component {
<DiaryIconOkB diaryId={diary.id}
count={diary.like_count}
active={diary.liked}
clickable={!this.expired}
clickable={!this.state.expired}
refreshBack={this.refreshDiary.bind(this)}
></DiaryIconOkB>
</View>
<View style={{flex: 1}} />
{
this.editable
isMine && !expired
? <TouchableOpacity onPress={this.onDiaryAction.bind(this)}>
<Ionicons name="ios-more" size={24}
color={Color.inactiveText}

View file

@ -18,15 +18,30 @@ export default class DiaryFull extends Component {
this.state = {
diary: props.diary,
editable: props.editable || false,
selfInfo: props.selfInfo,
isMine: props.isMine || false,
expired: props.expired || false
}
this.showField = ['userIcon', 'userName', 'subject', 'createdTime'];
if(props.showField && props.showField.length > 0) {
this.showField = props.showField
}
}
refreshDiaryContent(diary) {
if(diary) {
this.setState({diary})
show(field) {
return this.showField.indexOf(field) >= 0;
}
componentWillReceiveProps(nextProps) {
this.setState({
diary: nextProps.diary,
selfInfo: nextProps.selfInfo,
isMine: nextProps.isMine || false,
expired: nextProps.expired || false
});
}
async refreshComment() {
@ -76,12 +91,12 @@ export default class DiaryFull extends Component {
return (
<View>
<View style={localStyle.box}>
{user && user.iconUrl
{user && user.iconUrl && this.show('userIcon')
? <UserIcon iconUrl={user.iconUrl} onPress={this._onUserIconPress.bind(this)}></UserIcon> : null}
<View style={localStyle.body}>
<View style={localStyle.title}>
{user && user.name
{user && user.name && this.show('userName')
? ( <Text style={localStyle.titleName} numberOfLines={1}>
{user.name}
</Text>
@ -112,10 +127,10 @@ export default class DiaryFull extends Component {
</View>
<CommentList ref={(r) => this.commentList = r}
diaryId={diary.id}
editable={this.state.editable}
onUserIconPress={this._onUserIconPress.bind(this)}
{...this.props}
diaryId={diary.id}
isMine={this.state.isMine}
expired={this.state.expired}
></CommentList>
</View>

View file

@ -30,7 +30,7 @@ export default class DiaryList extends Component {
constructor(props) {
super(props);
this.editable = props.editable || false;
this.isMine = props.isMine || false;
this.dataSource = props.dataSource;
this.state = {
@ -95,7 +95,7 @@ export default class DiaryList extends Component {
diary: diary,
user: diary.user,
editable: this.editable,
showField: this.props.showField,
refreshBack: this.refreshOne.bind(this, index)
}
}
@ -199,7 +199,7 @@ export default class DiaryList extends Component {
render() {
if(!this.state.mounting && (!this.state.diaries || this.state.diaries.length == 0)) {
let message = this.editable
let message = this.isMine
? '今天还没有写日记,马上写一篇吧'
: '今天还没有人写日记';
return (
@ -227,7 +227,6 @@ export default class DiaryList extends Component {
<DiaryBrief {...this.props}
diary={item}
showField={this.props.showField}
editable={this.editable}
onDiaryPress={this._onDiaryPress.bind(this, index)}
onUserIconPress={() => this._onUserIconPress(item)}

View file

@ -9,7 +9,7 @@ import {
ActivityIndicator
} from 'react-native';
import {Navigation} from 'react-native-navigation';
import moment from 'moment'
import moment from 'moment';
import Api from '../../util/api';
import Color from '../../style/color';
@ -30,7 +30,7 @@ export default class NotebookDiaryList extends Component {
super(props);
this.notebook = props.notebook;
this.editable = props.editable || false;
this.isMine = props.isMine || false;
this.dataSource = new NotebookDiaryData();
this.state = {
@ -186,9 +186,9 @@ export default class NotebookDiaryList extends Component {
},
passProps: {
diary: diary,
editable: this.editable,
expired: this.notebook.isExpired,
showField: ['createdTime'],
expired: this.notebook.isExpired,
refreshBack: this.refreshOne.bind(this, index)
}
}
@ -201,6 +201,7 @@ export default class NotebookDiaryList extends Component {
}
let expired = this.notebook.isExpired;
let isMine = this.isMine;
return (
<View style={localStyle.container}>
@ -218,6 +219,7 @@ export default class NotebookDiaryList extends Component {
diary={rowData.item}
showField={['createdTime']}
expired={expired}
isMine={isMine}
onDiaryPress={this._onDiaryPress.bind(this, rowData.index)}

View file

@ -86,7 +86,7 @@ export default class NotebookList extends Component {
},
passProps: {
notebook: notebook,
editable: !this.state.user
isMine: !this.state.user
}
}
});

View file

@ -3,7 +3,7 @@ import {StyleSheet, Text, View} from 'react-native';
import Ionicons from 'react-native-vector-icons/Ionicons';
import Touchable from '../touchable';
import Color from '../../style/color'
import Color from '../../style/color';
function unique(array) {

View file

@ -51,8 +51,6 @@ export default class NotificationList extends Component {
}, []);
console.log('notifications:', reducedNoti);
this.setState({
notifications: reducedNoti
});
@ -77,8 +75,7 @@ export default class NotificationList extends Component {
}
},
passProps: {
diaryId: msg.link_id,
editable: true
diaryId: msg.link_id
}
}
});

View file

@ -10,11 +10,12 @@ import {
} 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 Api from '../util/api';
import Event from '../util/event';
import DiaryFull from '../component/diary/diaryFull';
@ -22,6 +23,16 @@ 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) {
@ -35,9 +46,13 @@ export default class DiaryDetailPage extends Component {
diary: props.diary,
user: props.user,
editable: props.editable || false,
isMine: false,
todayCreated: false,
expired: props.expired || false,
needScrollToBottom: false
needScrollToBottom: false,
error: null
}
}
@ -48,22 +63,17 @@ export default class DiaryDetailPage extends Component {
}
}
if(!passProps.expired) {
topBar.rightButtons = [{
id: 'navButtonMore',
icon: Icon.navButtonMore,
color: Color.primary // android
}]
}
return {
topBar
};
}
navigationButtonPressed({buttonId}) {
if(this.state.editable || this.state.diary.user_id == this.state.selfInfo.id) {
if(!this.state.selfInfo || !this.state.diary) {
return;
}
if(this.state.isMine) {
let componentId = this.props.componentId;
DiaryAction.action(componentId, this.state.diary, {
onDelete: () => {
@ -87,14 +97,13 @@ export default class DiaryDetailPage extends Component {
}
componentDidMount() {
if(!this.state.diary && this.state.diaryId) {
this.refreshDiary();
}
Api.getSelfInfoByStore()
.then(user => {
this.setState({
selfInfo: user
}, () =>{
this.refreshDiary(this.state.diary);
});
}).done();
@ -117,34 +126,67 @@ export default class DiaryDetailPage extends Component {
this.commentListener.remove();
}
refreshDiary() {
async refreshDiary(diary) {
if(!diary) {
let diaryId = this.state.diaryId;
if(!diaryId) {
diaryId = this.state.diary.id;
}
Api.getDiary(diaryId)
.then(result => {
if(!result) {
throw {
message: 'get diary no result'
try {
diary = await Api.getDiary(diaryId);
} catch(e) {
this.setState({
error: e
});
return;
}
}
this.props.refreshBack(result);
this.diaryFull.refreshDiaryContent(result);
})
.done();
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.selfInfo || !this.state.diary) {
return null;
}
if(this.state.error) {
return (
<View style={localStyle.container}>
<Text style={localStyle.text}>{'日记加载失败:('}</Text>
<Button fontSize={14} borderRadius={999} backgroundColor={Color.primary}
title={'刷新一下'}
onPress={() => this.refreshDiary()} />
</View>
);
let isMine = false;
if(this.state.selfInfo.id == this.state.diary.user_id) {
isMine = true;
} else if(!this.state.selfInfo || !this.state.diary) {
return null;
}
return (
@ -162,14 +204,15 @@ export default class DiaryDetailPage extends Component {
<DiaryFull ref={(r) => this.diaryFull = r}
{...this.props}
diary={this.state.diary}
editable={this.state.editable || isMine}
expired={this.state.expired}
selfInfo={this.state.selfInfo}
isMine={this.state.isMine}
expired={this.state.expired || !this.state.todayCreated}
></DiaryFull>
</ScrollView>
{
!this.state.expired ? (
this.state.todayCreated ? (
<CommentInput ref={(r) => this.commentInput = r}
diary={this.state.diary}
selfInfo={this.state.selfInfo}
@ -189,5 +232,14 @@ const localStyle = StyleSheet.create({
wrap: {
flex: 1,
flexDirection: 'column'
},
container: {
alignItems:'center',
justifyContent: 'center',
height: '100%'
},
text: {
paddingBottom: 15,
color: Color.text
}
});

View file

@ -22,7 +22,7 @@ export default class NotebookDetailPage extends Component {
}
}
if(passProps.editable) {
if(passProps.isMine) {
topBar.rightButtons = [{
id: 'navButtonNotebookSetting',
icon: Icon.navButtonNotebookSetting,

View file

@ -192,15 +192,17 @@ export default class UserPage extends Component {
/>,
diary: () => <DiaryList
ref={(r) => this.diaryList = r}
{...this.props}
dataSource={this.dataSource}
showField={['subject', 'createdTime']}
editable={!this.user}
{...this.props}
isMine={!this.user}
/>,
notebook: () => <NotebookList
ref={(r) => this.notebookList = r}
user={this.user}
{...this.props}
user={this.user}
/>
});