Skip to content

Instantly share code, notes, and snippets.

@chrismllr
Created August 6, 2017 22:56
Show Gist options
  • Save chrismllr/c0523f8296256b16e1dcaf4001205b64 to your computer and use it in GitHub Desktop.
Save chrismllr/c0523f8296256b16e1dcaf4001205b64 to your computer and use it in GitHub Desktop.
Model w/ factories
export function compose (factories) {
return function ontoEntity (entity) {
const factoryAdditions = factories.reduce((composed, fn) => {
return { ...composed, ...fn(entity) };
}, {});
return {
...entity,
...factoryAdditions
};
};
}
// Factories
export function withCityState(mkt) {
return {
cityState: (mkt.city && mkt.state)
&& `${mkt.city}, ${mkt.state}`
};
}
import get from 'lodash.get'
import { withCityState } from './market.model'
// Factories
export function withFullName (sub) {
return {
fullName: `${sub.firstName}, ${sub.middleName}, ${sub.lastName}`
};
}
export function withMarket (sub) {
return {
market: compose([withCityState])(sub.market)
};
}
export function currentStatus (sub) {
return {
currentStatus: statuses.find(
(status) => get(sub, 'status', '').toLowerCase() === status.value
)
};
}
export function currentIndicator (sub) {
return {
currentIndicator: indicators.find(
(indicator) => get(sub, 'indicator', '') === indicator.value
)
};
}
import axios from 'axios';
import { compose } from 'utils/compose'
import { withFullName, withMarket, currentStatus, currentIndicator } from 'models/subscriber.model';
function Subscriber(data) {
return compose([
withFullName,
withMarket,
currentStatus,
currentIndicator
])(data);
}
export function getAll() {
return axios.get('/subscribers').then(({ data }) => {
return data.map(Subscriber);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment