postingActions.js 7.13 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,
} 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 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 = '';
                    for (let i = 0; i < assets.length; i++) {
                        imageStr += assets[i].webURL+',';
                    }
                    let param = {
                        postTitle: encodeURI(posting.title),
                        uid: ssouid,
                        forumCode: posting.currentBoardId,
                        contentData: encodeURI(posting.content),
                        images: imageStr,
                    }
                    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();

        let assetsArray = storedState.assets;
        let temp = [];
        for (var i = 0; i < assetsArray.length; i++) {
            let {
                    uri,
                    height,
                    width,
                    isStored,
                    url,
                    uti,
                    size,
                    finished,
                    name,
                    webURL,
                }=assetsArray[i];

            let newAsset = {
                uri,
                height,
                width,
                isStored,
                url,
                uti,
                size,
                finished,
                name,
                webURL,
            }
            temp.push(newAsset);
        }
        storedState.assets = temp;
        storedState.boards = [];

        store.save(POSTING_CACHE_KEY,storedState).then(()=>{
            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 => {
                        console.log(response);
                        dispatch(updateUploadState(response,asset.name));
                    }).catch(error => {

                        console.log(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 => {
                        console.log(json);
                        store.delete(POSTING_CACHE_KEY);
                        dispatch(postFinished(true));
                    }).catch(error => {
                        console.log('download error.....');
                        dispatch(postFinished(false));
                    });
                });
            }
        }); //Cache data before uploading....
    }
}

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

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