Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Created September 20, 2018 15:15
Show Gist options
  • Save ajcrites/43863a66a464c503aa51e9f26880ac0e to your computer and use it in GitHub Desktop.
Save ajcrites/43863a66a464c503aa51e9f26880ac0e to your computer and use it in GitHub Desktop.
// 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)),
),
);
// 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;
}
}
// 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