Created
September 20, 2018 15:15
-
-
Save ajcrites/43863a66a464c503aa51e9f26880ac0e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Epic used by both examples. | |
// Error handling etc. omitted for simplicity | |
export const profileEpic = action => action.pipe( | |
ofType(SEARCH_PROFILE), | |
switchMap(term => getProfileByTerm(term).pipe( | |
map(({ body }) => new SearchProfileSuccessAction(body)), | |
), | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example | |
export function getProfileByTerm(term) { | |
return ajax(`${API_URL}/?term=${term}`).pipe( | |
map(({ body }) => { | |
let name = body.name; | |
if (name && name !== 'Andrew') { | |
name = name.toUpperCase(); | |
} else { | |
name = name.split('n').join(''); | |
} | |
return { body: name }; | |
}); | |
} | |
export function profileReducer(state = { name: '' }, action) { | |
switch (action.type) { | |
case SEARCH_PROFILE_SUCCESS: | |
return { ...state, name: action.payload }; | |
default: | |
return state; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Better Example | |
export function getProfileByTerm(term) { | |
return ajax(`${API_URL}/?term=${term}`); | |
} | |
export function profileReducer(state = { name: '' }, action) { | |
switch (action.type) { | |
case SEARCH_PROFILE_SUCCESS: | |
let name = action.payload.name; | |
if (name && name !== 'Andrew') { | |
name = name.toUpperCase(); | |
} else { | |
name = name.split('n').join(''); | |
} | |
return { ...state, name }; | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment