Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------- In component to use stream ---------------------------------------
- state = {
- message: ''
- }
- url= `http://localhost:5000`
- //url= `https://quiet-sands-12836.herokuapp.com`
- //connect to server 'eventStream'
- source = new EventSource(`${this.url}/stream`)
- //connect source to handler
- componentDidMount() {
- this.source.onmessage = this.props.onEvent
- }
- onChange = (event) => {
- const {value } = event.target
- this.setState({
- message: value
- })
- }
- onSubmit = (event) => {
- event.preventDefault()
- const {message} = this.state
- //call post Action here
- }
- //DONT FORGET!!!
- // state of main reducer:
- function mapStateToProps (state) {
- data: state.data //reducer name
- }
- // so this.props.data NOW CONTAINS THE FOLLOWING (from stream)
- data: [
- {"id":1,"name":"Retro Spectacle","description":"This is a test","active":true,
- "users":[
- {"id":2,"username":"Marten","retroId":1},
- {"id":1,"username":"Andrew","retroId":1}
- ],
- "cards":[
- {"id":2,"text":"Its alive!!","type":"glad","userId":2,"retroId":1},
- {"id":1,"text":"We got this working!!","type":"glad","userId":1,"retroId":1}
- ]
- },
- {"id":2,"name":"Retro Number 2","description":"This is the second test","active":true,
- "users":[
- {"id":3,"username":"Brigitte","retroId":2},
- {"id":4,"username":"Giulia","retroId":2}
- ],
- "cards":[
- {"id":4,"text":"Its good when your code works!","type":"glad","userId":4,"retroId":2},
- {"id":3,"text":"I love ith when things are working!","type":"glad","userId":3,"retroId":2}
- ]
- }
- ]
- ----------------- In action to FETCH ---------------------------------------
- export const EVENT = 'EVENT'
- export function onEvent (event) {
- const {data} = event
- //deserialize array
- const totalData = JSON.parse(data)
- //data = the array of ALL the data sent
- return {
- type: EVENT,
- payload: totalData
- }
- }
- ----------------- In main reducer ---------------------------------------
- import { EVENT } from "../actions/event";
- const initialState = []
- export default function messages (state = initialState, { type, payload}) {
- switch(type) {
- case EVENT:
- return payload
- default:
- return state
- }
- }
- //this should always update the state to show ALL data in stream
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement