Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************/
- //actions/mensajes.js
- import firebaseApp from '../config';
- export const LOAD_MESSAGES = 'LOAD_MESSAGES';
- export const ADD_MESSAGE = 'ADD_MESSAGE';
- export const SET_ACTUAL_MESSAGE = 'SET_ACTUAL_MESSAGE';
- export const loadMessages = mensajes => ({
- type: LOAD_MESSAGES,
- mensajes
- });
- export const addMessage = mensaje => ({
- type: ADD_MESSAGE,
- mensaje
- });
- export const setActualMessage = texto =>({
- type:SET_ACTUAL_MESSAGE,
- texto
- });
- export const loadDataBaseMessages = () => dispatch => {
- let nodeRef = '/chat/mensajes/';
- firebaseApp.database().ref(nodeRef).once('value')
- .then(function(snapshot){
- console.log("LOAD ONCE MESSAGES");
- let mensajes = [];
- if (snapshot.val() !== null){
- const lista = snapshot.val();
- for (key in lista){
- let mensaje = {id:key, ...lista[key]};
- mensajes.push(mensaje);
- }
- }
- const actionLoadMessages = loadMessages(mensajes);
- dispatch(actionLoadMessages);
- },
- function(error){
- console.log(error);
- });
- };
- export const watchMessageAdded = () => dispatch => {
- let nodeRef = '/chat/mensajes/';
- firebaseApp.database().ref(nodeRef).limitToLast(1)
- .on('child_added',
- function(snapshot) {
- console.log("CHILD ADDED");
- if (snapshot.val() !== null){
- valor = snapshot.val();
- let nuevoMensaje = {id:snapshot.key, texto:valor.texto, color:valor.color};
- const actionAddMessage = addMessage(nuevoMensaje);
- dispatch(actionAddMessage);
- }
- },
- function(error) {
- console.log('ERROR WATCH',error);
- });
- };
- export const storeMessage = mensaje => dispatch => {
- const db = firebaseApp.database();
- let nodeRef = '/chat/mensajes/';
- let nuevoMensajeId = db.ref(nodeRef).push().key;
- db.ref(nodeRef + nuevoMensajeId)
- .set(mensaje, function(error) {
- if (error){
- console.log('ERROR SET',error);
- }
- else {
- const actionSetActualMessage = setActualMessage('');
- dispatch(actionSetActualMessage);
- }
- });
- };
- /******************************************************************/
- //reducers/mensajes.js
- import * as ActionType from '../actions/mensajes';
- import { combineReducers } from 'redux';
- import { mensajesMockeados } from '../helpers/mokedData';
- const MENSAJES_INITIAL_STATE = [];
- //const MENSAJES_INITIAL_STATE = mensajesMockeados;
- const MENSAJE_ACTUAL_INITIAL_STATE = {
- texto:''
- };
- const mensajes = (state = MENSAJES_INITIAL_STATE, action) => {
- switch (action.type) {
- case ActionType.LOAD_MESSAGES:
- return action.mensajes;
- case ActionType.ADD_MESSAGE:
- return [...state, action.mensaje];
- default:
- return state;
- }
- };
- const mensajeActual = (state= MENSAJE_ACTUAL_INITIAL_STATE, action) =>{
- switch (action.type) {
- case ActionType.SET_ACTUAL_MESSAGE:
- return {...state, texto:action.texto};
- default:
- return state;
- }
- };
- export default combineReducers({
- mensajes,
- mensajeActual
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement