mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-10 00:03:24 +00:00
fix crash when basket contains not existing product
This commit is contained in:
parent
417f328206
commit
0a2ed6dd94
@ -30,25 +30,17 @@ function getCookie(name) {
|
|||||||
*/
|
*/
|
||||||
function get_starting_items() {
|
function get_starting_items() {
|
||||||
const cookie = getCookie(BASKET_ITEMS_COOKIE_NAME);
|
const cookie = getCookie(BASKET_ITEMS_COOKIE_NAME);
|
||||||
let output = [];
|
if (!cookie) {
|
||||||
|
return []
|
||||||
try {
|
}
|
||||||
// Django cookie backend converts `,` to `\054`
|
// Django cookie backend converts `,` to `\054`
|
||||||
let parsed = JSON.parse(cookie.replace(/\\054/g, ','));
|
let parsed = JSON.parse(cookie.replace(/\\054/g, ','));
|
||||||
if (typeof parsed === "string") {
|
if (typeof parsed === "string") {
|
||||||
// In some conditions, a second parsing is needed
|
// In some conditions, a second parsing is needed
|
||||||
parsed = JSON.parse(parsed);
|
parsed = JSON.parse(parsed);
|
||||||
}
|
}
|
||||||
output = Array.isArray(parsed) ? parsed : [];
|
const res = Array.isArray(parsed) ? parsed : [];
|
||||||
} catch (e) {
|
return res.filter((i) => !!document.getElementById(i.id))
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
output.forEach(item => {
|
|
||||||
let el = document.getElementById(item.id);
|
|
||||||
el.classList.add("selected");
|
|
||||||
});
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener('alpine:init', () => {
|
document.addEventListener('alpine:init', () => {
|
||||||
@ -84,9 +76,6 @@ document.addEventListener('alpine:init', () => {
|
|||||||
this.items[index].quantity -= 1;
|
this.items[index].quantity -= 1;
|
||||||
|
|
||||||
if (this.items[index].quantity === 0) {
|
if (this.items[index].quantity === 0) {
|
||||||
let el = document.getElementById(this.items[index].id);
|
|
||||||
el.classList.remove("selected");
|
|
||||||
|
|
||||||
this.items = this.items.filter((e) => e.id !== this.items[index].id);
|
this.items = this.items.filter((e) => e.id !== this.items[index].id);
|
||||||
}
|
}
|
||||||
this.set_cookies();
|
this.set_cookies();
|
||||||
@ -96,12 +85,6 @@ document.addEventListener('alpine:init', () => {
|
|||||||
* Remove all the items from the basket & cleans the catalog CSS classes
|
* Remove all the items from the basket & cleans the catalog CSS classes
|
||||||
*/
|
*/
|
||||||
clear_basket() {
|
clear_basket() {
|
||||||
// We remove the class "selected" from all the items in the catalog
|
|
||||||
this.items.forEach(item => {
|
|
||||||
let el = document.getElementById(item.id);
|
|
||||||
el.classList.remove("selected");
|
|
||||||
})
|
|
||||||
|
|
||||||
this.items = [];
|
this.items = [];
|
||||||
this.set_cookies();
|
this.set_cookies();
|
||||||
},
|
},
|
||||||
@ -111,8 +94,11 @@ document.addEventListener('alpine:init', () => {
|
|||||||
* ! the cookie survives an hour
|
* ! the cookie survives an hour
|
||||||
*/
|
*/
|
||||||
set_cookies() {
|
set_cookies() {
|
||||||
if (this.items.length === 0) document.cookie = `${BASKET_ITEMS_COOKIE_NAME}=;Max-Age=0`;
|
if (this.items.length === 0) {
|
||||||
else document.cookie = `${BASKET_ITEMS_COOKIE_NAME}=${encodeURIComponent(JSON.stringify(this.items))};Max-Age=3600`;
|
document.cookie = `${BASKET_ITEMS_COOKIE_NAME}=;Max-Age=0`;
|
||||||
|
} else {
|
||||||
|
document.cookie = `${BASKET_ITEMS_COOKIE_NAME}=${encodeURIComponent(JSON.stringify(this.items))};Max-Age=3600`;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -148,12 +134,10 @@ document.addEventListener('alpine:init', () => {
|
|||||||
|
|
||||||
// if the item is not in the basket, we create it
|
// if the item is not in the basket, we create it
|
||||||
// else we add + 1 to it
|
// else we add + 1 to it
|
||||||
if (item === undefined) item = this.create_item(id, name, price);
|
if (!item) {
|
||||||
else this.add(item);
|
item = this.create_item(id, name, price);
|
||||||
|
} else {
|
||||||
if (item.quantity > 0) {
|
this.add(item);
|
||||||
let el = document.getElementById(item.id);
|
|
||||||
el.classList.add("selected");
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
|
@ -102,8 +102,12 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="product-group">
|
<div class="product-group">
|
||||||
{% for p in items %}
|
{% for p in items %}
|
||||||
<button id="{{ p.id }}" class="product-button"
|
<button
|
||||||
@click='add_from_catalog({{ p.id }}, {{ p.name|tojson }}, {{ p.selling_price }})'>
|
id="{{ p.id }}"
|
||||||
|
class="product-button"
|
||||||
|
:class="{selected: items.some((i) => i.id === {{ p.id }})}"
|
||||||
|
@click='add_from_catalog({{ p.id }}, {{ p.name|tojson }}, {{ p.selling_price }})'
|
||||||
|
>
|
||||||
{% if p.icon %}
|
{% if p.icon %}
|
||||||
<img class="product-image" src="{{ p.icon.url }}"
|
<img class="product-image" src="{{ p.icon.url }}"
|
||||||
alt="image de {{ p.name }}">
|
alt="image de {{ p.name }}">
|
||||||
|
Loading…
Reference in New Issue
Block a user