mineGuangActions.js
4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
import ReactNative from 'react-native';
import {Platform} from 'react-native';
import MineGuangService from '../../services/MineGuangService';
import {Record, List, Map} from 'immutable';
const {
SET_TYPE,
FETCH_USER_FAV_ARTICLE_LIST_REQUEST,
FETCH_USER_FAV_ARTICLE_LIST_SUCCESS,
FETCH_USER_FAV_ARTICLE_LIST_FAILURE,
UPDATE_USER_FAV_ARTICLE_LIST,
} = require('../../constants/actionTypes').default;
export function fetchUserFavArticleListRequest() {
return{
type: FETCH_USER_FAV_ARTICLE_LIST_REQUEST,
}
}
export function fetchUserFavArticleListSuccess(json) {
return{
type: FETCH_USER_FAV_ARTICLE_LIST_SUCCESS,
payload: json,
}
}
export function fetchUserFavArticleListFailure(error) {
return{
type: FETCH_USER_FAV_ARTICLE_LIST_FAILURE,
payload: error,
}
}
export function updateUserFavArticleList(list) {
return{
type: UPDATE_USER_FAV_ARTICLE_LIST,
payload: list,
}
}
export function fetchUserFavArticleList() {
return (dispatch, getState) => {
let {app, mineGuang} = getState();
let {userFavArticleList} = mineGuang;
let {isFetching, favArticleList,curPage,totalPage} = userFavArticleList;
if (isFetching) {
return;
}
curPage++;
if (totalPage && curPage > totalPage) {
return;
}
let userFavArticleListFunction = (uid) => {
dispatch(fetchUserFavArticleListRequest());
let params = {
limit: 10,
uid: uid,
page: curPage,
}
return new MineGuangService(app.serviceHost).getUserFavArticleList(params)
.then(json => {
if (favArticleList && favArticleList.size) {
favArticleList = favArticleList.toJS();
}
let newList = [...favArticleList, ...json.data];
json.data = newList;
dispatch(fetchUserFavArticleListSuccess(json));
dispatch(fetchSimpleArticleList(json.data))
})
.catch(error => {
dispatch(fetchUserFavArticleListFailure(error));
})
}
ReactNative.NativeModules.YH_CommonHelper.uid()
.then(uid => {
userFavArticleListFunction(uid);
})
.catch(error => {
ReactNative.NativeModules.YH_CommonHelper.login()
.then(uid => {
userFavArticleListFunction(uid);
})
.catch(error => {
});
});
}
}
export function fetchSimpleArticleList(favArticleList) {
return (dispatch, getState) => {
let {app, mineGuang} = getState();
let simpleArticleListFunction = (uid) => {
let articleIds = '';
favArticleList.map((item,i) => {
articleIds = articleIds + item.id + ',';
})
let params = {
articleIds,
uid,
}
return new MineGuangService(app.serviceHost).getSimpleArticleList(params)
.then(json => {
//将请求到的文章相关数据合并到favArticleList
let newFavArticleList = mergeSimpleArticleToFavList(favArticleList,json.artList);
dispatch(updateUserFavArticleList(newFavArticleList));
})
.catch(error => {
})
}
ReactNative.NativeModules.YH_CommonHelper.uid()
.then(uid => {
simpleArticleListFunction(uid);
})
.catch(error => {
ReactNative.NativeModules.YH_CommonHelper.login()
.then(uid => {
simpleArticleListFunction(uid);
})
.catch(error => {
});
});
}
}
function mergeSimpleArticleToFavList(favArticleList,simpleArticleList) {
let newFavArticleList = favArticleList;
favArticleList.map((favArticleItem, i) => {
simpleArticleList.map((simpleArticleItem) => {
if (favArticleItem.id == simpleArticleItem.articleId) {
favArticleItem.isPraise = simpleArticleItem.isPraise;
favArticleItem.publish_time = simpleArticleItem.publish_time;
favArticleItem.isFavor = simpleArticleItem.isFavor;
favArticleItem.views_num = simpleArticleItem.views_num;
favArticleItem.praise_num = simpleArticleItem.praise_num;
}
})
})
return favArticleList;
}
export function onPressLike(article_id,rowID) {
return (dispatch, getState) => {
let {app, mineGuang} = getState();
let {userFavArticleList} = mineGuang;
let {favArticleList} = userFavArticleList;
let likeRequest = (article_id, uid) => {
if (!favArticleList || favArticleList.size == 0) {
return;
}
let newFavArticleList = favArticleList.delete(rowID);
dispatch(updateUserFavArticleList(newFavArticleList));
return new MineGuangService(app.serviceHost).cancelFavorite({article_id,uid});
}
ReactNative.NativeModules.YH_CommonHelper.uid()
.then(uid => {
likeRequest(article_id, uid);
})
.catch(error => {
ReactNative.NativeModules.YH_CommonHelper.login()
.then(uid => {
likeRequest(article_id, uid);
})
.catch(error => {
});
});
};
}