Commit 05b68916 authored by Kate's avatar Kate Committed by Tim Neutkens

Fixed server status message in with-redux-observable-example. (#4900)

**Changes:**
- Fixed "was character fetched on server" message by properly passing `isServer`.
- Stop fetching if there was an error (currently it keeps sending requests to the same endpoint every 3 sec)

**Related:**
- https://github.com/zeit/next.js/pull/4818
- https://github.com/zeit/next.js/issues/4724
parent 919b8850
import React from 'react' import React from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
const CharacterInfo = ({character, error, fetchCharacter, isFetchedOnServer = false}) => ( const CharacterInfo = ({ character, error, fetchCharacter, isFetchedOnServer = false }) => (
<div className='CharacterInfo'> <div className='CharacterInfo'>
{ {
error ? <p>We encountered and error.</p> error ? <p>We encountered and error.</p>
...@@ -15,7 +15,7 @@ const CharacterInfo = ({character, error, fetchCharacter, isFetchedOnServer = fa ...@@ -15,7 +15,7 @@ const CharacterInfo = ({character, error, fetchCharacter, isFetchedOnServer = fa
} }
<p> <p>
( was character fetched on server? - (was character fetched on server? -{' '}
<b>{isFetchedOnServer.toString()})</b> <b>{isFetchedOnServer.toString()})</b>
</p> </p>
<style jsx>{` <style jsx>{`
......
...@@ -6,7 +6,7 @@ export const startFetchingCharacters = () => ({ ...@@ -6,7 +6,7 @@ export const startFetchingCharacters = () => ({
export const stopFetchingCharacters = () => ({ export const stopFetchingCharacters = () => ({
type: types.STOP_FETCHING_CHARACTERS type: types.STOP_FETCHING_CHARACTERS
}) })
export const fetchCharacter = isServer => ({ export const fetchCharacter = (isServer = false) => ({
type: types.FETCH_CHARACTER, type: types.FETCH_CHARACTER,
payload: { isServer } payload: { isServer }
}) })
......
...@@ -11,12 +11,11 @@ export const fetchUserEpic = (action$, state$) => ...@@ -11,12 +11,11 @@ export const fetchUserEpic = (action$, state$) =>
ofType(types.START_FETCHING_CHARACTERS), ofType(types.START_FETCHING_CHARACTERS),
mergeMap(action => { mergeMap(action => {
return interval(3000).pipe( return interval(3000).pipe(
map(x => map(x => actions.fetchCharacter()),
actions.fetchCharacter({ takeUntil(action$.ofType(
isServer: state$.value.isServer types.STOP_FETCHING_CHARACTERS,
}) types.FETCH_CHARACTER_FAILURE
), ))
takeUntil(action$.ofType(types.STOP_FETCHING_CHARACTERS))
) )
}) })
) )
...@@ -31,14 +30,14 @@ export const fetchCharacterEpic = (action$, state$) => ...@@ -31,14 +30,14 @@ export const fetchCharacterEpic = (action$, state$) =>
map(response => map(response =>
actions.fetchCharacterSuccess( actions.fetchCharacterSuccess(
response.response, response.response,
state$.value.isServer action.payload.isServer
) )
), ),
catchError(error => catchError(error =>
of( of(
actions.fetchCharacterFailure( actions.fetchCharacterFailure(
error.xhr.response, error.xhr.response,
state$.value.isServer action.payload.isServer
) )
) )
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment