messageActions.js 2.89 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(brandId) {
    return {
        type: REQUEST_MSG_LIST,
        payload: brandId,
    }
}

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(brandId) {
    return dispatch => {
		dispatch(requestMsgList(brandId));
        return new MessageService().getMsgList(brandId)
            .then(json => {
                dispatch(requestMsgListSuccess(json));
            })
            .catch(error => {
                dispatch(requestMsgListFailure(error));
            });
	};
}

export function setMsgIsRead(brandId, messageId) {
    return dispatch => {
        dispatch(batchSetMsgRead());
        return new MessageService().setMsgIsRead(brandId, messageId)
            .then(json => {
                dispatch(requestUnreadMsgNum(brandId, '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(brandId, isRead) {
    return dispatch => {
		dispatch(requestMsgNum());
        return new MessageService().requestUnreadMsgNum(brandId, 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,
    }
}