/**
* 熱門發(fā)現(xiàn)
*/
'use strict';
import React, { Component } from 'react';
import {
Modal,
TouchableOpacity,
ListView,
StyleSheet,
Text,
View,
ActivityIndicator,
RefreshControl,
InteractionManager
} from 'react-native';
import Login from '../user/login';
import Icon from '../svg/icon';
import PostShow from '../post/PostShow';
import StatusBar from '../components/StatusBar';
import ListItem from '../components/ListItem';
import styles from '../styles/common';
import routes from '../../routes';
import * as Storage from '../common/Storage';
import { hotListArticles } from '../actions/hotActions';
import { userFromSync, userToken } from '../actions/userActions';
let cacheResults = {
dataBlob : [],
skip : 0,
limit : 4,
}
class Hot extends Component {
constructor(props) {
super(props);
this.state = {
checkItem : '',
checkIndex : '',
isFavorite : false,
animationType: 'none',
modalVisible : false,
transparent : true,
dataSource : new ListView.DataSource({
rowHasChanged : (row1, row2) => row1 !== row2
}),
loaded : true,
isNull : false,
isMore : false,
isRefreshing : false,
}
this._favorite = this._favorite.bind(this);
this._postToFriend = this._postToFriend.bind(this);
}
getArticles(skip, limit, isNew) {
var url = routes.hot
Storage.getToken()
.then((token) => {
if (token != '') {
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `JWT ${token}`,
},
body: JSON.stringify({
skip: skip,
limit: limit,
})
})
.then((response) => response.json())
.then((responseJson) => {
if (responseJson.articles.length == 0 && skip == 0) {
this.setState({
isNull : true
})
} else if (responseJson.articles.length == 0 && skip != 0){
this.setState({
isMore : true
})
} else {
if (isNew) {
let datas = [...responseJson.articles];
cacheResults.dataBlob = datas;
var skilNext = skip + 5;
this.setState({
dataSource: this.state.dataSource.cloneWithRows(datas),
loaded : false,
skip : skilNext,
isRefreshing: false
})
} else {
let datas = [...cacheResults.dataBlob];
datas.push(...responseJson.articles);
cacheResults.dataBlob = datas;
var skilNext = this.state.skip + 5;
this.setState({
dataSource: this.state.dataSource.cloneWithRows(datas),
loaded : false,
skip : skilNext,
isRefreshing: false
})
}
}
})
.catch((error) => {
console.error(error);
});
} else {
this._loginPush();
}
})
}
componentWillMount() {
const { navigator, route } = this.props;
route.title = "發(fā)現(xiàn)";
}
componentDidMount() {
//從緩存加載數(shù)據(jù)到對應(yīng)的state
const { dispatch } = this.props;
// Storage.getUser()
// .then((user) => {
// dispatch(userFromSync(user));
// });
//獲取發(fā)現(xiàn)數(shù)據(jù)
InteractionManager.runAfterInteractions(() => {
const { dispatch, userReducer } = this.props;
Storage.getToken()
.then((token) => {
dispatch(userToken(token));
dispatch(hotListArticles(token, cacheResults.skip, cacheResults.limit));
});
});
//this.getArticles(this.state.skip, this.state.limit)
}
_renderEndReached() {
const { hotReducer, userReducer, dispatch } = this.props;
if (hotReducer.loaded) {
return;
}
dispatch(hotListArticles(userReducer.token, hotReducer.skip + 5, hotReducer.limit))
}
// 下拉刷新
_onRefresh() {
this.setState({isRefreshing: true});
//服務(wù)器獲取新的數(shù)據(jù)
this.getArticles(0, 4, true)
}
_renderFooter(isNull, isMore) {
if (isNull) {
return (
<View style={styles.nullBoxForLine}>
<Text style={styles.nullBoxTextForLine}>~ 快去發(fā)現(xiàn)吧 ~</Text>
</View>
);
} else if(isMore){
return (
<View style={styles.nullBoxForLine}>
<Text style={styles.nullBoxTextForLine}>~ 沒有更多了喔 ~</Text>
</View>
);
} else {
return (
<View style={styles.loadingMore}>
<ActivityIndicator style={styles.loadIcon}/>
<Text>正在加載...</Text>
</View>
);
}
}
// 跳轉(zhuǎn)到登錄頁面
_loginPush() {
const { navigator } = this.props;
if (navigator) {
navigator.push({
name : 'login',
component: Login,
})
}
}
// 跳轉(zhuǎn)到 postshow
_showPost(rowData) {
const { navigator } = this.props;
if (navigator) {
navigator.push({
name : 'postshow',
component: PostShow,
params : {
data: rowData,
user: this.props.user
}
})
}
}
// 顯示/隱藏modal
_setModalVisible(visible) {
this.setState({modalVisible: visible});
};
// 選中item
_checkItem(item) {
this.setState({checkItem : item});
this.setState({isFavorite : item.isfavorite});
};
_checkIndex(index) {
this.setState({checkIndex : index});
}
// 列表單元格
_renderItem(rowData, sectionID, rowID) {
return (
<ListItem item={rowData} index={rowID} showPost= {this._showPost.bind(this)}
modal= {this._setModalVisible.bind(this)} checkitem={this._checkItem.bind(this)}
checkindex = {this._checkIndex.bind(this)} navigator={this.props.navigator}
/>
);
}
_renderSectionHeader(sectionData, sectionID) {
return (
<View style={styles.spaceBox}>
</View>
);
}
// 收藏功能
_favorite(item) {
if (item) {
if (item.isfavorite) {
this._favoriteRemove(item.favorite);
} else {
this._favoriteCreate(item.target_id, item.author_id._id);
}
}
}
// 創(chuàng)建收藏
_favoriteCreate(postid, authorid){
var createurl = routes.favoriteCreate;
AsyncStorage.getItem('jwt', (err, token) => {
fetch(createurl, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `JWT ${token}`,
},
body: JSON.stringify({
postid: postid,
authorid: authorid,
})
})
.then((response) => response.json())
.then((responseJson) => {
// 修改本地緩存數(shù)據(jù)
cacheResults.dataBlob[this.state.checkIndex].isfavorite = true;
cacheResults.dataBlob[this.state.checkIndex].favorite = responseJson;
this.setState({
checkIndex: '',
isFavorite: true,
dataSource: this.state.dataSource.cloneWithRows(cacheResults.dataBlob),
})
this._setModalVisible(false)
})
.catch((error) => {
console.error(error);
});
});
}
// 取消收藏
_favoriteRemove(favorite){
var removeurl = routes.favoriteRemove + favorite._id;
AsyncStorage.getItem('jwt', (err, token) => {
fetch(removeurl,{
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': `JWT ${token}`,
}
})
.then((response) => response.json())
.then((responseJson) => {
if (responseJson == 1) {
// 修改本地緩存數(shù)據(jù)
cacheResults.dataBlob[this.state.checkIndex].isfavorite = false;
cacheResults.dataBlob[this.state.checkIndex].favorite = '';
this.setState({
checkIndex: '',
isFavorite: false,
dataSource: this.state.dataSource.cloneWithRows(cacheResults.dataBlob),
})
this._setModalVisible(false)
}
})
.catch((error) => {
console.error(error);
})
.done();
});
}
// 轉(zhuǎn)發(fā)到動態(tài)
_postToFriend(item) {
var url = routes.postToFriend + item.target_id + '/user/' + this.state.user._id;
AsyncStorage.getItem('jwt', (err, token) => {
fetch(url,{
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': `JWT ${token}`,
}
})
.then((response) => response.json())
.then((responseJson) => {
if (responseJson) {
this._setModalVisible(false)
setTimeout(function(){
alert('轉(zhuǎn)發(fā)成功爆班!')
}, 1000)
}
})
.catch((error) => {
console.error(error);
})
.done();
});
}
render() {
let modalBackgroundStyle = {
backgroundColor: this.state.transparent ? 'rgba(0, 0, 0, 0.5)' : '#f5fcff',
};
let innerContainerTransparentStyle = this.state.transparent
? {backgroundColor: '#fff'}
: null;
const { hotReducer } = this.props;
return (
<View style={styles.container}>
<StatusBar/>
<View style={styles.header}>
<Text style={styles.headerTitle} numberOfLines={1}>發(fā)現(xiàn)</Text>
</View>
<ListView
initialListSize = {1}
enableEmptySections = {true}
pageSize = {5}
onEndReached = {this._renderEndReached.bind(this)}
onEndReachedThreshold = {20}
dataSource = {this.state.dataSource.cloneWithRows(hotReducer.dataBlob)}
refreshControl = {
<RefreshControl
refreshing = {this.state.isRefreshing}
onRefresh = {this._onRefresh.bind(this)}
/>
}
renderFooter = {this._renderFooter.bind(this, this.state.isNull, this.state.isMore)}
renderRow = {this._renderItem.bind(this)}
style = {styles.listview}
/>
<Modal
animationType={this.state.animationType}
transparent={this.state.transparent}
visible={this.state.modalVisible}
onRequestClose={() => this._setModalVisible(false)}
>
<View style={[styles.modalContainer, modalBackgroundStyle]}>
<View style={[styles.modalInnerContainer, innerContainerTransparentStyle]}>
<TouchableOpacity onPress={ () => this._postToFriend(this.state.checkItem)}>
<View style={styles.itemBox}>
<Text style={styles.itemText}>轉(zhuǎn)發(fā)</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress={ () => this._favorite(this.state.checkItem)}>
<View style={styles.itemBox}>
<Text style={styles.itemText}>{this.state.isFavorite ? '取消收藏' : '收藏'}</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress={this._setModalVisible.bind(this,false) }>
<View style={[styles.itemBox,{height:60,borderTopWidth:10,borderTopColor:'#F8F8F8'}]}>
<Text style={[styles.itemText]}>取消</Text>
</View>
</TouchableOpacity>
</View>
</View>
</Modal>
</View>
);
}
}
module.exports = Hot;
hot記錄
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門默伍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人衰琐,你說我怎么就攤上這事也糊。” “怎么了羡宙?”我有些...
- 文/不壞的土叔 我叫張陵狸剃,是天一觀的道長。 經(jīng)常有香客問我辛辨,道長捕捂,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任斗搞,我火速辦了婚禮指攒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘僻焚。我一直安慰自己允悦,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布虑啤。 她就那樣靜靜地躺著隙弛,像睡著了一般。 火紅的嫁衣襯著肌膚如雪狞山。 梳的紋絲不亂的頭發(fā)上全闷,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼淫奔!你這毒婦竟也來了山涡?” 一聲冷哼從身側(cè)響起,我...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡系吩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 年R本政府宣布贞绵,位于F島的核電站,受9級特大地震影響恍飘,放射性物質(zhì)發(fā)生泄漏榨崩。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一章母、第九天 我趴在偏房一處隱蔽的房頂上張望母蛛。 院中可真熱鬧,春花似錦乳怎、人聲如沸彩郊。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽秫逝。三九已至,卻和暖如春询枚,著一層夾襖步出監(jiān)牢的瞬間违帆,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 相信很多人和我一樣班巩,知道柳比歇夫的“事件-時間”記錄方法之后,躍躍欲試地記錄著自己的時間開銷嘶炭,期待可以突破普通專業(yè)...
- 前日兰迫,釘釘Tower“任務(wù)應(yīng)用”首次開放信殊,1000家企業(yè)試用名額,6分鐘被搶光汁果! 昨日涡拘,釘釘藍(lán)凌“考勤應(yīng)用”首次開...
- 2017.11.8橱野。無為。焦點解決分享第93天赡译。設(shè)立一個明確且堅定的目標(biāo)仲吏,做出持續(xù)的,小跨步的有效努力蝌焚,你就會發(fā)現(xiàn)...
- 沒有人不喜歡正面的東西裹唆,除非心理有缺陷。 現(xiàn)在很多人只洒,特別是老板都常常把“正能量”掛在嘴邊许帐,不允許有抱怨或者...