appActions.js 4.64 KB
/**
 * # guideActions.js
 *
 * App user guide
 *
 */
'use strict';

import ReactNative from 'react-native';
import {Actions} from 'react-native-router-flux';
import Immutable, {Map} from 'immutable';
import AppService from '../../services/AppService';
import RouterService from '../../services/RouterService';
import {loginThenSyncUserInfo} from '../user/userActions';

const {
	SET_PLATFORM,
	SET_CONTAINER,
	SET_CHANNEL,
	GO_ACTION,

	GO_TO_SECTION,

	GO_TO_POST,
	GO_TO_LIKE_LIST,

	GO_TO_USER,
	GO_TO_USER_THAT_NOT_ME,
	GO_TO_SETTING,

	GO_TO_MESSAGE_CENTER,
    GO_TO_SYS_MESSAGE,
    GO_TO_LIKE_MESSAGE,

	GO_TO_POSTING,
} = require('../../constants/actionTypes').default;

export function goToSection(section) {
	return (dispatch, getState) => {
		let {route} = getState();
		let tryToClean = route.lastPopScene == 'Section';
		Actions.Section();
		dispatch({
			type: GO_TO_SECTION,
	        payload: {...section, tryToClean},
		});
	}
}

export function goToPost(id) {
	return (dispatch, getState) => {
		let {route} = getState();
		let tryToClean = route.lastPopScene == 'SubjectPost';
		Actions.SubjectPost({postId:id});
		dispatch({
			type: GO_TO_POST,
	        payload: {id, tryToClean},
		});
	}
}

export function goToLikeList(postId, likeCount) {
	return (dispatch, getState) => {
		let {route} = getState();
		let tryToClean = route.lastPopScene == 'LikeList';
		Actions.LikeList({title: `共${likeCount}个赞`});
		dispatch({
			type: GO_TO_LIKE_LIST,
	        payload: {postId, tryToClean},
		});
	}
}

export function goToUserOrMe(uid) {
    return (dispatch, getState) => {
        let {user} = getState();
        if (uid	!= 0 && uid == user.profile.uid) {
            Actions.User();
			dispatch({
	            type: GO_TO_USER,
	        });
        } else {
            dispatch(goToUserThatNotMe(uid));
        }
	};
}

export function goToUser() {
	return (dispatch, getState) => {
		let operation = () => {
			Actions.User();
			return {
				type: GO_TO_USER,
			};
		};

        let {user} = getState();
        if (user.profile.uid == 0) {
			dispatch(loginThenSyncUserInfo(operation));
        } else {
			dispatch(operation());
        }
	};
}

export function goToUserThatNotMe(uid) {
    return (dispatch, getState) => {
        let {user, route} = getState();
        if (user.profile.uid == uid) {

        } else {
			let tryToClean = route.lastPopScene == 'UserThatNotMe';
			Actions.UserThatNotMe();
			dispatch({
	            type: GO_TO_USER_THAT_NOT_ME,
	            payload: {uid, tryToClean},
	        });
        }
	};
}

export function goToSetting() {
    Actions.Setting();
    return {
        type: GO_TO_SETTING,
    };
}

export function goToMessageCenter() {
    Actions.MessageCenter();
    return {
        type: GO_TO_MESSAGE_CENTER,
    };
}

export function goToLikeMessage() {
    Actions.LikeMessage();
    return {
        type: GO_TO_LIKE_MESSAGE,
    };
}

export function goToSystemMessage() {
    Actions.SystemMessage();
    return {
        type: GO_TO_SYS_MESSAGE,
    };
}

export function goToPosting() {
    Actions.Posting();
    return {
        type: GO_TO_POSTING,
    };
}

export function setPlatform(platform) {
    return {
        type: SET_PLATFORM,
        payload: platform
    };
}

export function setContianer(container) {
    return {
        type: SET_CONTAINER,
        payload: container
    };
}

export function setChannel(channel) {
    return {
        type: SET_CHANNEL,
        payload: channel
    };
}

export function goAction(inputUrl) {
	return (dispatch, getState) => {
		let json = new RouterService().parseUrl(inputUrl);
		
		if (json === null || !json.action || !json.params) {
			return;
		}

		let {action, params} = json;

		if (action == 'go.comm.forum') {
			let id = params && params.forumid ? params.forumid : 0;
			let name = '';
			if (id != 0) {
				dispatch(goToSection({id, name}));
			}

		} else if (action == 'go.comm.postdetail') {
			let id = params && params.postid ? params.postid : 0;
			if (id == 0) {
				id = params && params.postId ? params.postId : 0;
			}
			if (id != 0) {
				dispatch(goToPost(id));
			}

		} else if (action == 'go.comm.h5') {
			let title = params.title ? params.title : '';
			let url = params.url ? params.url : '';
			if (!url || url.length == 0) {
				return;
			}
			ReactNative.NativeModules.YH_CommunityHelper.displayH5({url, title});
		} else if (action == 'go.comm.productDetail') {
			let productSkn = params.product_skn ? params.product_skn : '';
			productSkn = productSkn + '';
			let tag = params.tag ? params.tag : '';
			if (!productSkn || productSkn.length == 0) {
				return;
			}
			ReactNative.NativeModules.YH_CommunityHelper.displayProductDetail({productSkn, tag});
		}
	};
}