Skip to content

Instantly share code, notes, and snippets.

@vjeux

vjeux/test.js Secret

Created April 5, 2017 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vjeux/83488a218252b4030daf6b0d01527fb6 to your computer and use it in GitHub Desktop.
Save vjeux/83488a218252b4030daf6b0d01527fb6 to your computer and use it in GitHub Desktop.
const a = Observable.fromPromise(axiosInstance.post("/carts/mine")).map(
response => response.data,
);
const b = Observable.fromPromise(axiosInstance.get(url)).map(
response => response.data,
);
const CategoryLoader = axiosInstance => {
const load = id =>
Observable.fromPromise(axiosInstance.get(`/categories/${id}`))
.map(response => response.data)
.map(category => ({
...convertCategoryMainAttributesForFront(category),
children: category.children.split(",").filter(id => id !== ""),
}));
};
const loadPricesBatch = skus => {
const params = {};
return Observable.fromPromise(
axiosInstance.get("/frontcommerce/price", { params }),
)
.map(response => response.data)
.map(reorderForSkus(skus));
};
const changePassword = (currentPassword, newPassword) => {
return Observable.fromPromise(
axiosInstance.put("/customers/me/password", {
currentPassword,
newPassword,
}),
).map(response => ({ success: response.data }));
};
const optionIsPartOf = configuration => option =>
configuration.some(
({ id, value }) => id === option.id && value === option.value,
);
// splitting the callback params on a newline as below
// seems much readable imo than the whole `new Dataloader(...)`
const makeUrlLoader = type =>
new DataLoader(ids => loadUrlsBatch(type, ids).toPromise());
const reorderForSkus = skus => data =>
skus.map(sku => data.find(result => result.sku === sku) || null);
const reorderForIds = (ids, idKey) => data => {
return ids.map(
id =>
data.find(result => result[idKey].toString() === id.toString()) || null,
);
};
const apolloMutationToRxObservable = apolloPromise => {
return Observable.create(observer => {
// the then callback content, when pretified as idented
// would be better wrapped with parenthesis imo
apolloPromise
.then(response =>
observer.next({ status: "success", response: response }),
)
.catch(error => {
let response = error;
observer.next({ status: "error", response });
});
});
};
withHandlers({
onImageClick: props => image => {
// foo
},
});
const EnhanceDelivery = fragments =>
compose(
makeCommandDispatcherWithMessageFeedback({
onSubmit: props => e => {
e.preventDefault();
// ...
},
}),
);
const hasPrice = config =>
config && config.product && typeof config.product.price !== "undefined";
const priceOf = config => config.product.price;
const cheapestFirst = (a, b) => priceOf(b) - priceOf(a);
const logFailedActions = store => next => action => {
// ...
};
const cart = {
type: CartType,
resolve: (_, args, { loaders }) =>
loaders.Cart.cart().map(cart => cart || { items_qty: 0 }).toPromise(),
};
const EnhanceFooter = () =>
compose(
withRouter,
withProps({
isCartActive: props => props.router.isActive("/cart", true),
}),
);
const EnhanceLoadable = (isLoaded, Spinner) =>
branch(isLoaded, BaseComponent => BaseComponent, () => Spinner);
export default (Apollo, Router) => () => (
<Apollo.Provider>
<Router />
</Apollo.Provider>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment