personalInfoActions.js 6.15 KB
'use strict';

import ReactNative from 'react-native';
import {Platform} from 'react-native';
import PersonalInfoService from '../../services/PersonalInfoService';
import {Record, List, Map} from 'immutable';

const {
	SET_TYPE,

	FETCH_PROFILE_REQUEST,
	FETCH_PROFILE_SUCCESS,
	FETCH_PROFILE_FAILURE,

	UPDATE_PAGE_CELL_LIST,

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

export function fetchProfileRequest() {
	return{
		type: FETCH_PROFILE_REQUEST,
	}
}

export function fetchProfileSuccess(json) {
	return{
		type: FETCH_PROFILE_SUCCESS,
		payload: json,
	}
}

export function fetchProfileFailure(error) {
	return{
		type: FETCH_PROFILE_FAILURE,
		payload: error,
	}
}

export function updatePageCellList(list) {
	return{
		type: UPDATE_PAGE_CELL_LIST,
		payload: list,
	}
}

export function fetchPersonalInfo() {
	return (dispatch, getState) => {
		let {app, personalInfo} = getState();
		let getProfileFunction = (uid) => {
			dispatch(fetchProfileRequest());
			return new PersonalInfoService(app.host).getProfile(uid)
			.then(json => {
				dispatch(fetchProfileSuccess(json));
				dispatch(processProfile(json));
			})
			.catch(error => {
				dispatch(fetchProfileFailure());
			})
		}

		ReactNative.NativeModules.YH_CommonHelper.uid()
		.then(uid => {
			getProfileFunction(uid);
		})
		.catch(error => {
			ReactNative.NativeModules.YH_CommonHelper.login()
			.then(uid => {
				getProfileFunction(uid);
			})
			.catch(error => {

			});
		});
	}
}

function processProfile(profile){
	return (dispatch, getState) => {
		let {app, personalInfo} = getState();
		let {pageCellList} = personalInfo;
		pageCellList = pageCellList.toJS();
		pageCellList.map((cellItem, i) => {
			switch (cellItem.id) {
				case 'portrait':
					cellItem.url = profile.head_ico;
					break;
				case 'nick_name':
					cellItem.content = profile.nickname;
					break;
				case 'gender':{
					if (profile.gender == 1) {
						cellItem.content = 'BOY';
					} else if (profile.gender == 2) {
						cellItem.content = 'GIRL';
					}
				}
					break;
				case 'birthday':
					cellItem.content = profile.birthday;
					break;
				case 'VIPLevel':{
					if (profile.vip_info.cur_level != '0') {
						cellItem.content = profile.vip_info.cur_level;
					}
				}
					break;
				case 'height':
					cellItem.content = profile.height;
					break;
				case 'weight':
					cellItem.content = profile.weight;
					break;
				default:

			}
		})
		dispatch(updatePageCellList(pageCellList));
	}
}

export function modifyUserBaseInfo(cellList) {
	return (dispatch, getState) => {
		let {app, personalInfo} = getState();
		let {pageCellList} = personalInfo;
		if (!cellList) {
			cellList = pageCellList.toJS();
		}
		let params = {};
		cellList.map((cellItem, i) => {
			if (cellItem.id == 'gender') {
				let gender = 0;
				if (cellItem.content == 'BOY') {
					gender = 1;
				} else if (cellItem.content == 'GIRL') {
					gender = 2;
				}
				params.gender = gender;
			}else {
				if (cellItem.baseUpload) {
					params[cellItem.id] = cellItem.content;
				}
			}
		})
		return new PersonalInfoService(app.host).modifyUserBaseInfo(params)
		.then(json => {

				console.log('-----更新信息');
				console.log(json);
		})
		.catch(error => {
			console.log('-----更新信息');
			console.log(error);
		})
	}
}

export function onPressInfoCell(cellInfo) {
	return (dispatch, getState) => {
		let {app, personalInfo} = getState();
		let {pageCellList} = personalInfo;
		pageCellList = pageCellList.toJS();
		switch (cellInfo.get('id')) {
			case 'portrait':{
				ReactNative.NativeModules.YH_PersonalInfoHelper.selectAvatarAction()
				.then(data => {
					if (data && data != '') {
						pageCellList[0].url = data;
					}
					dispatch(updatePageCellList(pageCellList));
				})
				.catch(error => {
					console.log('-----imageUrl error');
					console.log(error);
				});
			}
				break;
			case 'nick_name': {
				ReactNative.NativeModules.YH_PersonalInfoHelper.gotoEditNickName(cellInfo.get('content'))
				.then(data => {
					pageCellList[cellInfo.get('index')].content = data;
					dispatch(updatePageCellList(pageCellList));
					dispatch(modifyUserBaseInfo(pageCellList));
				})
				.catch(error => {
					console.log('-----nickname error');
					console.log(error);
				});
			}
				break;
			case 'gender':{
				ReactNative.NativeModules.YH_PersonalInfoHelper.selectGenderAction()
				.then(data => {
					console.log('-----gender');
					console.log(data);
					pageCellList[cellInfo.get('index')].content = data;
					dispatch(updatePageCellList(pageCellList));
				})
				.catch(error => {
					console.log('-----nickname error');
					console.log(error);
				});
			}
				break;
			case 'birthday':{
				ReactNative.NativeModules.YH_PersonalInfoHelper.selectBirthdayAction()
				.then(data => {
					console.log('-----gender');
					console.log(data);
					pageCellList[cellInfo.get('index')].content = data;
					dispatch(updatePageCellList(pageCellList));
				})
				.catch(error => {
					console.log('-----nickname error');
					console.log(error);
				});
			}
				break;
			case 'VIPLevel':{
				ReactNative.NativeModules.YH_PersonalInfoHelper.gotoVIPLevelVC();
			}
				break;
			case 'mineQRCode':{
				ReactNative.NativeModules.YH_PersonalInfoHelper.gotoMineQRCode();
			}
				break;
			case 'height':
			case 'weight':{
				ReactNative.NativeModules.YH_PersonalInfoHelper.selectHeightWeightAction()
				.then(data => {
					console.log('-----height & weight');
					console.log(data);
					if (cellInfo.get('id') == 'height') {
						pageCellList[cellInfo.get('index')].content = data.height;
						pageCellList[cellInfo.get('index') + 1].content = data.weight;
					} else if (cellInfo.get('id') == 'weight') {
						pageCellList[cellInfo.get('index')].content = data.weight;
						pageCellList[cellInfo.get('index') - 1].content = data.height;
					}
					dispatch(updatePageCellList(pageCellList));
				})
				.catch(error => {
					console.log('-----nickname error');
					console.log(error);
				});
			}
				break;
			case 'addressManage':{
				ReactNative.NativeModules.YH_PersonalInfoHelper.gotoManagerAddressVC();
			}
				break;
			case 'accountBind':{
				ReactNative.NativeModules.YH_PersonalInfoHelper.gotoAccountBindVC();
			}
				break;
			default:

		}
	}
}