Skip to content

Commit

Permalink
fix: empty category and images bug
Browse files Browse the repository at this point in the history
  • Loading branch information
agladyshev committed Dec 6, 2019
1 parent 2435897 commit dd3b18f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 49 deletions.
70 changes: 36 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 => {
Expand All @@ -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))
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -200,32 +202,32 @@ 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
) {
res.send(res.order);
});

app.listen(port, () => console.log(`Listening`));
app.listen(port, () => console.log(`Listening`));
22 changes: 16 additions & 6 deletions src/CollectionsPanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
</script>

Expand All @@ -25,11 +33,13 @@
<nav>
<ul>
{#each col as { id, title, products, permalink }}
<CollectionIcon
highlight={permalink == params.permalink}
{title}
{permalink}
images={products[0].images[0]} />
{#if products[0].images}
<CollectionIcon
highlight={permalink == params.permalink}
{title}
{permalink}
images={products[0].images[0]} />
{/if}
{/each}
</ul>
</nav>
20 changes: 11 additions & 9 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit dd3b18f

Please sign in to comment.