actions/StateNoticesAction.js

/**
 * Функции для изменения текущего состояния notices
 * @module StateConfigAction
 * @author Ihor Bielchenko
 */

import Base from '../Base.js';

/**
 * Обновление текущего состояния
 * @param {Object} notices
 * @param {Function} callback
 * @return {Function}
 */
export function update(notices, callback = () => {}) {
	return (dispatch) => {
		dispatch({
			type: 'UPDATE_NOTICES',
			payload: notices
		});
		callback();
	}
}

/**
 * Обновление текущего состояния
 * @param {Object} notices
 * @param {Function} callback
 * @return {Function}
 */
export function clear(notices, callback = () => {}) {
	return (dispatch) => {
		notices.items = [];
		Base.serverSEND(JSON.stringify({
			action: 'teacher/notices/clear'
		}), (r) => {
			dispatch({
				type: 'CLEAR_NOTICES',
				payload: notices
			});
			callback();
		});
	}
}

/**
 * Отправить сообщение
 * @param {Object} notices
 * @param {String} message Текст сообщения
 * @param {Number} myID id екущего пользователя
 * @param {Function} callback
 * @return {Function}
 */
export function sendMessage(notices, message, myID, callback = () => {}) {
	return (dispatch) => {
		if(notices.current_dialog !== 0) {
			Base.serverSEND(JSON.stringify({
				action: 'user/message/send',
				properties: {
					dialog_id: notices.current_dialog,
					content: message
				}
			}), (r) => {
				var i,
					current = '';

				for(i = 0; i < notices.items.length; i++) {
					if(notices.items[i].id === notices.current_dialog) {
						current = notices.items[i];
					}
				}

				if(current !== '') {
					current.messages.push({
						id: Date.now(),
						user_id: myID,
						introtext: message,
						content: message,
						time: '21:43',
						readed: 1
					});
				}

				dispatch({
					type: 'SEND_MESSAGE',
					payload: notices
				});
				callback();
			}, 0);
		}
	}
}