Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ----------------- In component to use stream ---------------------------------------
  2. state = {
  3.     message: ''
  4.   }
  5.   url= `http://localhost:5000`
  6.  
  7.   //url= `https://quiet-sands-12836.herokuapp.com`
  8.   //connect to server 'eventStream'
  9.   source = new EventSource(`${this.url}/stream`)
  10.  
  11.   //connect source to handler
  12.   componentDidMount() {
  13.     this.source.onmessage = this.props.onEvent
  14.   }
  15.  
  16.   onChange = (event) => {
  17.     const {value } = event.target
  18.  
  19.     this.setState({
  20.       message: value
  21.     })
  22.   }
  23.  
  24.   onSubmit = (event) => {
  25.     event.preventDefault()
  26.  
  27.     const {message} = this.state
  28.    
  29.     //call post Action here
  30. }
  31.  
  32. //DONT FORGET!!!
  33. // state of main reducer:
  34.  
  35. function mapStateToProps (state) {
  36.   data: state.data //reducer name
  37. }
  38.  
  39. // so this.props.data   NOW CONTAINS THE FOLLOWING (from stream)
  40. data: [
  41.     {"id":1,"name":"Retro Spectacle","description":"This is a test","active":true,
  42.     "users":[
  43.         {"id":2,"username":"Marten","retroId":1},
  44.         {"id":1,"username":"Andrew","retroId":1}
  45.         ],
  46.     "cards":[
  47.         {"id":2,"text":"Its alive!!","type":"glad","userId":2,"retroId":1},
  48.         {"id":1,"text":"We got this working!!","type":"glad","userId":1,"retroId":1}
  49.         ]
  50.     },
  51.     {"id":2,"name":"Retro Number 2","description":"This is the second test","active":true,
  52.     "users":[
  53.         {"id":3,"username":"Brigitte","retroId":2},
  54.         {"id":4,"username":"Giulia","retroId":2}
  55.         ],
  56.     "cards":[
  57.         {"id":4,"text":"Its good when your code works!","type":"glad","userId":4,"retroId":2},
  58.         {"id":3,"text":"I love ith when things are working!","type":"glad","userId":3,"retroId":2}
  59.         ]
  60.     }
  61. ]
  62.  
  63.  
  64. ----------------- In action to FETCH ---------------------------------------
  65. export const EVENT = 'EVENT'
  66.  
  67. export function onEvent (event) {
  68.     const {data} = event
  69.     //deserialize array
  70.     const totalData = JSON.parse(data)
  71.     //data = the array of ALL the data sent
  72.  
  73.     return {
  74.         type: EVENT,
  75.         payload: totalData
  76.     }
  77. }
  78.  
  79. ----------------- In main reducer ---------------------------------------
  80.  
  81. import { EVENT } from "../actions/event";
  82.  
  83. const initialState = []
  84.  
  85. export default function messages (state = initialState, { type, payload}) {
  86.     switch(type) {
  87.         case EVENT:
  88.             return payload
  89.         default:
  90.             return state
  91.     }
  92. }
  93.  
  94. //this should always update the state to show ALL data in stream
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement