messageActions.js 3.16 KB
/**
 * # guideActions.js
 *
 * App user guide
 *
 */
'use strict';

import {Actions} from 'react-native-router-flux';
import MessageService from '../../services/MessageService';
import {setDot} from '../device/deviceActions';

const {
    MESSAGE_DETAIL,
    REQUEST_MSG_LIST,
    REQUEST_MSG_LIST_SUCCESS,
    REQUEST_MSG_LIST_FAILURE,
    BATCH_SET_MSG_IS_READ,
    BATCH_SET_MSG_IS_READ_SUCCESS,
    BATCH_SET_MSG_IS_READ_FAILURE,
    REQUEST_MSG_NUM,
    REQUEST_MSG_NUM_SUCCESS,
    REQUEST_MSG_NUM_FAILURE,
    SHOW_DOT,
} = require('../../constants/actionTypes').default;

export function checkMessageDetail(index) {
    Actions.MessageDetail();
	return {
		type: MESSAGE_DETAIL,
		payload: index,
	};
}

export function requestMsgList(shopId) {
    return {
        type: REQUEST_MSG_LIST,
        payload: shopId,
    }
}

export function requestMsgListSuccess(json) {
    return {
        type: REQUEST_MSG_LIST_SUCCESS,
        payload: json,
    }
}

export function requestMsgListFailure(error) {
    return {
        type: REQUEST_MSG_LIST_FAILURE,
        payload: error,
    }
}

export function getMsgList(shopId) {
    return (dispatch, getState) => {
        const {message} = getState();
        if (!shouldRequestMsgList(message)) {
            return;
        }

		dispatch(requestMsgList(shopId));
        return new MessageService().getMsgList(shopId)
            .then(json => {
                dispatch(requestMsgListSuccess(json));
            })
            .catch(error => {
                dispatch(requestMsgListFailure(error));
            });
	};
}

export function shouldRequestMsgList(message) {
    if (message.isFetching || message.messageList.size != 0) {
        return false;
    }

    return true;
}

export function setMsgIsRead(shopId, messageId) {
    return dispatch => {
        dispatch(batchSetMsgRead());
        return new MessageService().setMsgIsRead(shopId, messageId)
            .then(json => {
                dispatch(requestUnreadMsgNum(shopId, 'N'));
            })
            .catch(error => {
                dispatch(batchSetMsgReadFailure(error));
            });
    };
}

export function batchSetMsgRead() {
    return {
        type: BATCH_SET_MSG_IS_READ,

    }
}

export function batchSetMsgReadSuccess(json) {
    return {
        type: BATCH_SET_MSG_IS_READ_SUCCESS,
        payload: json,
    }
}

export function batchSetMsgReadFailure(error) {
    return {
        type: BATCH_SET_MSG_IS_READ_FAILURE,
        payload: error,
    }
}

export function requestUnreadMsgNum(shopId, isRead) {
    return dispatch => {
		dispatch(requestMsgNum());
        return new MessageService().requestUnreadMsgNum(shopId, isRead)
            .then(json => {
                dispatch(setDot(json > 0));
            })
            .catch(error => {
                dispatch(requestMsgNumFailure(error));
            });
	};
}


export function requestMsgNum() {
    return {
        type: REQUEST_MSG_NUM,

    }
}

export function requestMsgNumSuccess(json) {
    return {
        type: REQUEST_MSG_NUM_SUCCESS,
        payload: json,
    }
}

export function requestMsgNumFailure(error) {
    return {
        type: REQUEST_MSG_NUM_FAILURE,
        payload: error,
    }
}