postingActions.js 7.75 KB
/**
 * # postingActions.js
 *
 * posting page....
 *
 */
'use strict';

import {Actions} from 'react-native-router-flux';
import PostingService from '../../services/PostingService';
import Immutable, {List, Record} from 'immutable';
import {
    NativeModules,
    Alert,
} from 'react-native'

let YH_CommunityAssetsPicker = NativeModules.YH_CommunityAssetsPicker;
import store from 'react-native-simple-store';

const {
    POSTING_BOARD_REQUEST,
    POSTING_BOARD_SUCCESS,
    POSTING_BOARD_FAILURE,
    POSTING_BOARD_SELECTED,
    POSTING_ASSETS_SELECTED,
    POSTING_TITLE_EDITED,
    POSTING_CONTENT_EDITED,
    POSTING_POST_START,
    POSTING_POST_FAILURE,
    POSTING_UPLOAD_UPDATE,
    POSTING_POST_FINISHED,
    POSTING_STATE_RESET,
    POSTING_STATE_MERGE,
} = require('../../constants/actionTypes').default;

let POSTING_CACHE_KEY = 'cachedPosting';

export function voidImplementationFunc() {
    return {
        type:'null',
    }
}

export function startEditPost(postingState) {

    return dispatch => {
        dispatch(voidImplementationFunc());
        dispatch(() => {
            if (postingState) {
                Alert.alert('抱歉','您有正在发送的帖子,请耐心等待~');
            } else {
                NativeModules.YH_CommunityHelper.uid()
                    .then(uid => {
                        drillToEditPostPage(uid);
                    })
                    .catch(error => {
                        NativeModules.YH_CommunityHelper.login()
                            .then(uid => {
                                drillToEditPostPage(uid);
                            });
                    });
            }
        })
    }


}

function drillToEditPostPage(uid,postingPageFunc) {
    new PostingService().getPostUser(uid).then(json => {
        if (json.forbidSpeaking === 'Y') {
            Alert.alert('抱歉','您暂时被禁言,请等待禁言解除哦~');
        } else {
            console.log('yyyyyyyyyyyyyyyyyyy');

            Actions.Posting();
        }
    }).catch(error => {
        console.log('eeeeeeeeeeeeeee');
        console.log(error);
        Actions.Posting();
    })
}

export function commitMerge(cachedObj) {
    return {
        type: POSTING_STATE_MERGE,
        payload: Immutable.fromJS(cachedObj),
    }
}

export function mergeCachedPosting() {
    return dispatch => {
        store.get(POSTING_CACHE_KEY)
        .then((cachedObj)=>{
            if (cachedObj) {
                dispatch(commitMerge(cachedObj));
                dispatch(startPosting(cachedObj.assets));
            }
        })
    }

}

export function titleEdited(title) {
    return {
        type:POSTING_TITLE_EDITED,
        payload: title,
    }
}

export function contentEdited(content) {
    return {
        type: POSTING_CONTENT_EDITED,
        payload: content,
    }
}

export function boardSelected(boardName,boardCode) {
    return {
        type: POSTING_BOARD_SELECTED,
        payload: {boardName, boardCode},
    }
}

export function getBoardRequest() {
    return {
        type: POSTING_BOARD_REQUEST,
    };
}

export function getBoardSuccess(json) {
    return {
        type: POSTING_BOARD_SUCCESS,
        payload: json,
    }
}

export function getBoardFailure(error) {
    return {
        type: POSTING_BOARD_FAILURE,
        payload: error,
    }
}

export function postStart() {
    return {
        type: POSTING_POST_START,
    }
}

export function uploadImageStateChanged(payloadData) {
    return {
        type: POSTING_UPLOAD_UPDATE,
        payload:payloadData,
    }
}

export function postFinished(success) {
    return {
        type:POSTING_POST_FINISHED,
        payload:success,
    }
}

export function updateUploadState(url,fileName) {

    return (dispatch, getState) => {

        let {posting} = getState();

        let assets = posting.assets.toJS();
        for (var i = 0; i < assets.length; i++) {
            if (assets[i].name == fileName) {
                assets[i].webURL = url;
                assets[i].finished = true;
            }
        }
        let finishCount = posting.finishedCount;
        finishCount++;
        let percent = parseFloat(finishCount)/assets.length;
        let payloadData = {assets,finishCount,percent};
        dispatch(uploadImageStateChanged(payloadData));

        dispatch(() => {
            NativeModules.YH_CommunityHelper.uid().then(ssouid => {
                let {posting} = getState();
                if (posting.postPercent == 1) {
                    let assets = posting.assets.toJS();
                    let imageStr = '';
                    let sizeStr = '';
                    for (let i = 0; i < assets.length; i++) {
                        imageStr += assets[i].webURL+',';
                        sizeStr += assets[i].width + 'x' + assets[i].height +',';
                    }
                    let param = {
                        postTitle: encodeURI(posting.title),
                        uid: ssouid,
                        forumCode: posting.currentBoardId,
                        contentData: encodeURI(posting.content),
                        images: imageStr,
                        sizes: sizeStr,
                    }
                    return new PostingService().commitPost(param).then((json) => {
                        store.delete(POSTING_CACHE_KEY);
                        dispatch(postFinished(true));
                    }).catch(error => {
                        dispatch(postFinished(false));
                    });
                }
            });
        });
    }
}

export function resetState() {
    YH_CommunityAssetsPicker.resetState();
    return {
        type: POSTING_STATE_RESET,
    }
}

export function postFail() {
    return {
        type: POSTING_POST_FAILURE,
    }
}

export function startPosting(assets) {

    return (dispatch, getState) => {
        dispatch(postStart());

        let storedState = getState().posting.toJS();
        storedState.boards = [];
        storedState.postingBannerShow = true;
        store.save(POSTING_CACHE_KEY,storedState).then(()=>{
            store.get(POSTING_CACHE_KEY).then((data)=>{
            })
            if (assets.length) {
                for (let i = 0; i < assets.length; i++) {
                    let asset = assets[i]
                    if (asset.finished) {
                        continue;
                    }
                    new PostingService().uploadImageAsset(asset)
                    .then(response => {
                        dispatch(updateUploadState(response,asset.name));
                    }).catch(error => {
                        dispatch(postFail());
                    });
                }
            } else {
                NativeModules.YH_CommunityHelper.uid().then(ssouid => {
                    let {posting} = getState();
                    let param = {
                        postTitle: posting.title,
                        uid: ssouid,
                        forumCode: posting.currentBoardId,
                        contentData: posting.content,
                    }
                    return new PostingService().commitPost(param).then(json => {
                        store.delete(POSTING_CACHE_KEY);
                        dispatch(postFinished(true));
                    }).catch(error => {
                        dispatch(postFinished(false));
                    });
                });
            }
        }); //Cache data before uploading....
    }
}

export function getBoardList() {
    return dispatch => {
        dispatch(getBoardRequest());
        new PostingService().getBoardList().then(json => {
            dispatch(getBoardSuccess(json));
        }).catch(error => {
            dispatch(getBoardFailure(error));
        });
    };
}

export function assetsSelected(assets) {
    return {
        type: POSTING_ASSETS_SELECTED,
        payload: assets,
    }
}