mineGuangActions.js 4.43 KB
'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 => {

			});
		});


	};
}