Advertisement
andresrm

actions y reducers 2 de 2

Mar 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /******************************************************************/
  2. //actions/mensajes.js
  3. import firebaseApp from '../config';
  4. export const LOAD_MESSAGES = 'LOAD_MESSAGES';
  5. export const ADD_MESSAGE = 'ADD_MESSAGE';
  6. export const SET_ACTUAL_MESSAGE = 'SET_ACTUAL_MESSAGE';
  7.  
  8. export const loadMessages = mensajes => ({
  9.     type: LOAD_MESSAGES,
  10.     mensajes
  11. });
  12.  
  13. export const addMessage = mensaje => ({
  14.     type: ADD_MESSAGE,
  15.     mensaje
  16. });
  17.  
  18. export const setActualMessage = texto =>({
  19.     type:SET_ACTUAL_MESSAGE,
  20.     texto
  21. });
  22.  
  23. export const loadDataBaseMessages = () => dispatch => {
  24.     let nodeRef = '/chat/mensajes/';
  25.     firebaseApp.database().ref(nodeRef).once('value')
  26.     .then(function(snapshot){
  27.         console.log("LOAD ONCE MESSAGES");
  28.         let mensajes = [];
  29.         if (snapshot.val() !== null){
  30.             const lista = snapshot.val();
  31.             for (key in lista){
  32.               let mensaje = {id:key, ...lista[key]};
  33.               mensajes.push(mensaje);
  34.             }
  35.         }
  36.         const actionLoadMessages = loadMessages(mensajes);
  37.         dispatch(actionLoadMessages);
  38.     },
  39.     function(error){
  40.         console.log(error);
  41.     });
  42. };
  43.  
  44. export const watchMessageAdded = () => dispatch => {
  45.     let nodeRef = '/chat/mensajes/';
  46.     firebaseApp.database().ref(nodeRef).limitToLast(1)
  47.     .on('child_added',
  48.     function(snapshot) {
  49.         console.log("CHILD ADDED");
  50.         if (snapshot.val() !== null){
  51.             valor = snapshot.val();
  52.             let nuevoMensaje = {id:snapshot.key, texto:valor.texto, color:valor.color};
  53.             const actionAddMessage = addMessage(nuevoMensaje);
  54.             dispatch(actionAddMessage);
  55.         }
  56.        
  57.     },
  58.     function(error) {
  59.         console.log('ERROR WATCH',error);
  60.     });
  61. };
  62.  
  63. export const storeMessage = mensaje => dispatch => {
  64.     const db = firebaseApp.database();
  65.     let nodeRef = '/chat/mensajes/';
  66.     let nuevoMensajeId = db.ref(nodeRef).push().key;
  67.     db.ref(nodeRef + nuevoMensajeId)
  68.     .set(mensaje, function(error) {
  69.         if (error){
  70.             console.log('ERROR SET',error);
  71.         }
  72.         else {
  73.             const actionSetActualMessage = setActualMessage('');
  74.             dispatch(actionSetActualMessage);
  75.         }
  76.     });
  77. };
  78.  
  79. /******************************************************************/
  80. //reducers/mensajes.js
  81. import * as ActionType from '../actions/mensajes';
  82. import { combineReducers } from 'redux';
  83. import { mensajesMockeados } from '../helpers/mokedData';
  84.  
  85. const MENSAJES_INITIAL_STATE = [];
  86. //const MENSAJES_INITIAL_STATE = mensajesMockeados;
  87. const MENSAJE_ACTUAL_INITIAL_STATE = {
  88.     texto:''
  89. };
  90.  
  91. const mensajes = (state = MENSAJES_INITIAL_STATE, action) => {
  92.     switch (action.type) {
  93.         case ActionType.LOAD_MESSAGES:
  94.             return action.mensajes;
  95.         case ActionType.ADD_MESSAGE:
  96.             return [...state, action.mensaje];
  97.         default:
  98.             return state;
  99.     }
  100. };
  101.  
  102. const mensajeActual = (state= MENSAJE_ACTUAL_INITIAL_STATE, action) =>{
  103.     switch (action.type) {
  104.         case ActionType.SET_ACTUAL_MESSAGE:
  105.             return {...state, texto:action.texto};
  106.         default:
  107.             return state;
  108.     }
  109. };
  110.  
  111. export default combineReducers({
  112.     mensajes,
  113.     mensajeActual
  114. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement