Created
August 6, 2017 22:56
-
-
Save chrismllr/c0523f8296256b16e1dcaf4001205b64 to your computer and use it in GitHub Desktop.
Model w/ factories
This file contains 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
export function compose (factories) { | |
return function ontoEntity (entity) { | |
const factoryAdditions = factories.reduce((composed, fn) => { | |
return { ...composed, ...fn(entity) }; | |
}, {}); | |
return { | |
...entity, | |
...factoryAdditions | |
}; | |
}; | |
} |
This file contains 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
// Factories | |
export function withCityState(mkt) { | |
return { | |
cityState: (mkt.city && mkt.state) | |
&& `${mkt.city}, ${mkt.state}` | |
}; | |
} |
This file contains 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
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 | |
) | |
}; | |
} |
This file contains 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
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