Skip to content

Instantly share code, notes, and snippets.

@emilong
Created May 29, 2017 05:31
Show Gist options
  • Save emilong/be80b97f55ce9380c9ae715dbe2db2be to your computer and use it in GitHub Desktop.
Save emilong/be80b97f55ce9380c9ae715dbe2db2be to your computer and use it in GitHub Desktop.
MobX store for brand data
import autobind from "autobind-decorator";
import { extendObservable, action, observable } from "mobx";
const brandUrl = brand =>
`https://makeup-api.herokuapp.com/api/v1/products.json?brand=${brand.toLowerCase()}`;
export default class BrandStore {
constructor(preloadedState) {
const { brandProducts = {} } = preloadedState || {};
extendObservable(this, {
brandProducts: observable.map(brandProducts),
// ignore any brandsBeingFetched passed in on initialization.
// they definitely are _not_ being fetched.
brandsBeingFetched: new Set()
});
}
@action.bound fetchBrand(brand) {
this.brandsBeingFetched.add(brand);
fetch(brandUrl(brand))
.then(response => response.json())
.then(products => this.storeBrandProducts(brand, products));
}
@action.bound storeBrandProducts(brand, products) {
this.brandsBeingFetched.delete(brand);
this.brandProducts.set(brand, products);
}
@autobind isBeingFetched(brand) {
return this.brandsBeingFetched.has(brand);
}
@autobind hasFetched(brand) {
return !this.isBeingFetched(brand) && !!this.getProducts(brand);
}
@autobind getProducts(brand) {
return this.brandProducts.get(brand);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment