Skip to content

Commit

Permalink
fixed db keys and its data creation and similar products row fill
Browse files Browse the repository at this point in the history
  • Loading branch information
1-17 committed May 12, 2024
1 parent 6bca762 commit c3b7723
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
41 changes: 15 additions & 26 deletions src/js/utils/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import setPageTitle from "./setPageTitle.js"
import form from "./form.js"

const products = {
key: "products",
key: "alurageek_products",
container: document.querySelector("main"),
placeholderImage: "src/assets/img/placeholder.svg",
renderFallbackMessage: undefined,
Expand Down Expand Up @@ -49,21 +49,17 @@ products.get = () => {

if (!productsData) {
fetch("products.json")
.then(res => res.json())
.then(list => localStorage.setItem(products.key, JSON.stringify(list)))
.then(res => res.ok ? res.json() : products.renderFallbackMessage("Failed to get posts list. Please, try again later."))
.then(list => {
localStorage.setItem(products.key, JSON.stringify(list))
window.location.reload()
})
.catch(() => products.renderFallbackMessage("Failed to get posts list. Please, try again later."))
}

const list = JSON.parse(productsData)

if (!list) {
products.renderFallbackMessage("Failed to get posts list. Please, try again later.")
return
}

/* Newest products first */
list.sort((a, b) => Number(b.id) - Number(a.id))
products.list = list
products.list = JSON.parse(productsData).sort((a, b) => Number(b.id) - Number(a.id))
}

products.get()
Expand Down Expand Up @@ -245,26 +241,19 @@ products.renderProductDetailsAndSuggestions = () => {
`)

/* Getting similar products from same category as detailed product */
for (const product of products.byCategory[detailedProduct.category]) {
const i = products.byCategory[detailedProduct.category].indexOf(product)

if (product !== detailedProduct && i <= products.maxPerRow) {
for (const [i, product] of products.byCategory[detailedProduct.category].entries()) {
if (product !== detailedProduct && i < products.maxPerRow) {
similarProducts.push(product)
}
}
}

/* Adding more similar products from any categories until row is filled */
const isRowNotFilled = similarProducts.length < products.maxPerRow
const quantityToFillRow = products.maxPerRow - similarProducts.length

if (isRowNotFilled) {
for (const product of products.list) {
const i = products.list.indexOf(product)

if (product.category !== detailedProduct.category && i < quantityToFillRow) {
similarProducts.push(product)
}
for (let i = 0; i < products.list.length && similarProducts.length < products.maxPerRow; i++) {
const product = products.list[i]

if (product.category !== detailedProduct.category) {
similarProducts.push(product)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/js/utils/redirect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const isLoginPage = window.location.pathname.includes("login")
const isLogged = !!localStorage.getItem("logged")
const isLogged = !!localStorage.getItem("alurageek_logged")
const isPrivatePage = !isLoginPage && !isLogged

if (isPrivatePage) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/utils/session.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const session = {
key: "logged",
key: "alurageek_logged",
login: undefined,
logout: undefined,
isLogged: undefined
Expand Down

0 comments on commit c3b7723

Please sign in to comment.