diff --git a/index.js b/index.js index a7a94b4..f130b7d 100644 --- a/index.js +++ b/index.js @@ -33,7 +33,7 @@ let baseURL = `https://${process.env.INSALES_KEY}:${process.env.INSALES_PASSWORD // app.use(cors(corsOptions)); -app.use(function (req, res, next) { +app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Content-Type, X-Requested-With"); next(); @@ -47,19 +47,24 @@ app.use(function (req, res, next) { app.use("/", express.static("public")); -var filterEmptyProducts = function (product) { - return product.available && !product.is_hidden && product.images.length && product.variants[0].quantity; +var filterEmptyProducts = function(product) { + return ( + product.available && + !product.is_hidden && + product.images.length && + product.variants[0].quantity + ); }; -var addProductsArray = function (collection) { +var addProductsArray = function(collection) { // collection.products = []; - return getCollectionOrder(collection.id).then((res) => { + return getCollectionOrder(collection.id).then(res => { collection.products = res; return collection; - }) + }); }; -var getCollections = function (req, res, next) { +var getCollections = function(req, res, next) { fetch(new URL("/admin/collections.json?per_page=1000", baseURL)) .then(res => res.json()) .then(json => { @@ -71,24 +76,23 @@ var getCollections = function (req, res, next) { }) .then(collections => { // res.collections = collections.map(addProductsArray); - return Promise.all(collections.map(addProductsArray)) - + return Promise.all(collections.map(addProductsArray)); }) .then(collections => { - res.collections = collections + res.collections = collections; next(); }) .catch(error => console.log(error)); }; -var fetchProducts = function () { +var fetchProducts = function() { return fetch(new URL("/admin/products.json?per_page=1000", baseURL)) .then(res => res.json()) .then(json => json.filter(filterEmptyProducts)) .catch(error => console.log(error)); }; -var getProducts = function (req, res, next) { +var getProducts = function(req, res, next) { fetch(new URL("/admin/products.json?per_page=1000", baseURL)) .then(res => res.json()) .then(json => json.filter(filterEmptyProducts)) @@ -99,13 +103,13 @@ var getProducts = function (req, res, next) { .catch(error => console.log(error)); }; -var getCollectionOrder = function (id) { +var getCollectionOrder = function(id) { return fetch(new URL(`/admin/collects.json?collection_id=${id}`, baseURL)) .then(res => res.json()) .catch(error => console.log(error)); -} +}; -var checkAvailability = function (req, res, next) { +var checkAvailability = function(req, res, next) { let { ids } = req.body; fetchProducts() .then(available => { @@ -122,7 +126,7 @@ var checkAvailability = function (req, res, next) { }); }; -var getDelivery = function (req, res, next) { +var getDelivery = function(req, res, next) { fetch(new URL("/admin/delivery_variants.json", baseURL)) .then(res => res.json()) .then(json => { @@ -132,7 +136,7 @@ var getDelivery = function (req, res, next) { .catch(error => console.log(error)); }; -var getPayment = function (req, res, next) { +var getPayment = function(req, res, next) { fetch(new URL("/admin/payment_gateways.json", baseURL)) .then(res => res.json()) .then(json => { @@ -142,7 +146,7 @@ var getPayment = function (req, res, next) { .catch(error => console.log(error)); }; -var getPromo = function (req, res, next) { +var getPromo = function(req, res, next) { fetch(new URL("/admin/articles.json", baseURL)) .then(res => res.json()) .then(json => { @@ -152,7 +156,7 @@ var getPromo = function (req, res, next) { .catch(error => console.log(error)); }; -var addOrder = function (req, res, next) { +var addOrder = function(req, res, next) { let { products, name, @@ -178,15 +182,13 @@ var addOrder = function (req, res, next) { payment_gateway_id: paymentOption } }; - fetch(new URL("/admin/orders.json", baseURL), - { - method: "post", - body: JSON.stringify(body), - headers: { - "Content-Type": "application/json; charset=utf-8" - } + fetch(new URL("/admin/orders.json", baseURL), { + method: "post", + body: JSON.stringify(body), + headers: { + "Content-Type": "application/json; charset=utf-8" } - ) + }) .then(result => result.json()) .then(order => { res.order = order; @@ -200,27 +202,27 @@ var addOrder = function (req, res, next) { app.use("/", express.static("public")); -app.get("/getCollections", getCollections, function (req, res, next) { +app.get("/getCollections", getCollections, function(req, res, next) { res.send(res.collections); }); -app.get("/getProducts", getProducts, function (req, res, next) { +app.get("/getProducts", getProducts, function(req, res, next) { res.send(res.products); }); -app.get("/getDelivery", getDelivery, function (req, res, next) { +app.get("/getDelivery", getDelivery, function(req, res, next) { res.send(res.delivery); }); -app.get("/getPayment", getPayment, function (req, res, next) { +app.get("/getPayment", getPayment, function(req, res, next) { res.send(res.payment); }); -app.get("/getPromo", getPromo, function (req, res, next) { +app.get("/getPromo", getPromo, function(req, res, next) { res.send(res.promo); }); -app.post("/addOrder", express.json(), checkAvailability, addOrder, function ( +app.post("/addOrder", express.json(), checkAvailability, addOrder, function( req, res, next @@ -228,4 +230,4 @@ app.post("/addOrder", express.json(), checkAvailability, addOrder, function ( res.send(res.order); }); -app.listen(port, () => console.log(`Listening`)); \ No newline at end of file +app.listen(port, () => console.log(`Listening`)); diff --git a/src/CollectionsPanel.svelte b/src/CollectionsPanel.svelte index 11b2094..38c40f8 100644 --- a/src/CollectionsPanel.svelte +++ b/src/CollectionsPanel.svelte @@ -3,8 +3,16 @@ import CollectionIcon from "./CollectionIcon.svelte"; import { collections } from "./stores.js"; let col; + function isMainPage(value) { + if (value) { + return value.permalink != "frontpage"; + } + return false; + } collections.subscribe(values => { - col = values.filter(el => el.permalink != "frontpage"); + if (values) { + col = values.filter(isMainPage); + } }); @@ -25,11 +33,13 @@ diff --git a/src/helpers.js b/src/helpers.js index ffe2ee7..75d2bba 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -4,21 +4,23 @@ export const arrayToObject = (array, keyField) => return obj; }, {}); - -export const populateCollections = function (collections, products) { +export const populateCollections = function(collections, products) { if (!collections.length || !products.length) { return []; } return collections.map(c => { c.products = c.products.map(p => { - return Object.assign(p, products.find(prod => prod.id == p.product_id)) - }) - return c; - }) - + return Object.assign( + p, + products.find(prod => prod.id == p.product_id) + ); + }); + if (c.products.length) { + return c; + } + }); }; -export const filterEmptyCollections = function (col) { +export const filterEmptyCollections = function(col) { return col.products.length; }; -