mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 11:59:23 +00:00
Apply all biomejs fixes
This commit is contained in:
@ -14,6 +14,7 @@ const BASKET_ITEMS_COOKIE_NAME = "basket_items";
|
||||
* @returns {string|null|undefined} the value of the cookie or null if it does not exist, undefined if not found
|
||||
*/
|
||||
function getCookie(name) {
|
||||
// biome-ignore lint/style/useBlockStatements: <explanation>
|
||||
if (!document.cookie || document.cookie.length === 0) return null;
|
||||
|
||||
const found = document.cookie
|
||||
@ -28,7 +29,7 @@ function getCookie(name) {
|
||||
* Fetch the basket items from the associated cookie
|
||||
* @returns {BasketItem[]|[]} the items in the basket
|
||||
*/
|
||||
function get_starting_items() {
|
||||
function getStartingItems() {
|
||||
const cookie = getCookie(BASKET_ITEMS_COOKIE_NAME);
|
||||
if (!cookie) {
|
||||
return [];
|
||||
@ -45,13 +46,13 @@ function get_starting_items() {
|
||||
|
||||
document.addEventListener("alpine:init", () => {
|
||||
Alpine.data("basket", () => ({
|
||||
items: get_starting_items(),
|
||||
items: getStartingItems(),
|
||||
|
||||
/**
|
||||
* Get the total price of the basket
|
||||
* @returns {number} The total price of the basket
|
||||
*/
|
||||
get_total() {
|
||||
getTotal() {
|
||||
return this.items.reduce((acc, item) => acc + item.quantity * item.unit_price, 0);
|
||||
},
|
||||
|
||||
@ -61,38 +62,40 @@ document.addEventListener("alpine:init", () => {
|
||||
*/
|
||||
add(item) {
|
||||
item.quantity++;
|
||||
this.set_cookies();
|
||||
this.setCookies();
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove 1 to the quantity of an item in the basket
|
||||
* @param {BasketItem} item_id
|
||||
*/
|
||||
remove(item_id) {
|
||||
const index = this.items.findIndex((e) => e.id === item_id);
|
||||
remove(itemId) {
|
||||
const index = this.items.findIndex((e) => e.id === itemId);
|
||||
|
||||
if (index < 0) return;
|
||||
if (index < 0) {
|
||||
return;
|
||||
}
|
||||
this.items[index].quantity -= 1;
|
||||
|
||||
if (this.items[index].quantity === 0) {
|
||||
this.items = this.items.filter((e) => e.id !== this.items[index].id);
|
||||
}
|
||||
this.set_cookies();
|
||||
this.setCookies();
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove all the items from the basket & cleans the catalog CSS classes
|
||||
*/
|
||||
clear_basket() {
|
||||
clearBasket() {
|
||||
this.items = [];
|
||||
this.set_cookies();
|
||||
this.setCookies();
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the cookie in the browser with the basket items
|
||||
* ! the cookie survives an hour
|
||||
*/
|
||||
set_cookies() {
|
||||
setCookies() {
|
||||
if (this.items.length === 0) {
|
||||
document.cookie = `${BASKET_ITEMS_COOKIE_NAME}=;Max-Age=0`;
|
||||
} else {
|
||||
@ -107,18 +110,19 @@ document.addEventListener("alpine:init", () => {
|
||||
* @param {number} price The unit price of the product
|
||||
* @returns {BasketItem} The created item
|
||||
*/
|
||||
create_item(id, name, price) {
|
||||
const new_item = {
|
||||
createItem(id, name, price) {
|
||||
const newItem = {
|
||||
id,
|
||||
name,
|
||||
quantity: 0,
|
||||
// biome-ignore lint/style/useNamingConvention: used by django backend
|
||||
unit_price: price,
|
||||
};
|
||||
|
||||
this.items.push(new_item);
|
||||
this.add(new_item);
|
||||
this.items.push(newItem);
|
||||
this.add(newItem);
|
||||
|
||||
return new_item;
|
||||
return newItem;
|
||||
},
|
||||
|
||||
/**
|
||||
@ -128,15 +132,15 @@ document.addEventListener("alpine:init", () => {
|
||||
* @param {string} name The name of the product
|
||||
* @param {number} price The unit price of the product
|
||||
*/
|
||||
add_from_catalog(id, name, price) {
|
||||
addFromCatalog(id, name, price) {
|
||||
let item = this.items.find((e) => e.id === id);
|
||||
|
||||
// if the item is not in the basket, we create it
|
||||
// else we add + 1 to it
|
||||
if (!item) {
|
||||
item = this.create_item(id, name, price);
|
||||
} else {
|
||||
if (item) {
|
||||
this.add(item);
|
||||
} else {
|
||||
item = this.createItem(id, name, price);
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
@ -3,18 +3,23 @@
|
||||
* @enum {number}
|
||||
*/
|
||||
const BillingInfoReqState = {
|
||||
// biome-ignore lint/style/useNamingConvention: this feels more like an enum
|
||||
SUCCESS: 1,
|
||||
// biome-ignore lint/style/useNamingConvention: this feels more like an enum
|
||||
FAILURE: 2,
|
||||
// biome-ignore lint/style/useNamingConvention: this feels more like an enum
|
||||
SENDING: 3,
|
||||
};
|
||||
|
||||
document.addEventListener("alpine:init", () => {
|
||||
Alpine.store("billing_inputs", {
|
||||
data: et_data,
|
||||
// biome-ignore lint/correctness/noUndeclaredVariables: defined in eboutic_makecommand.jinja
|
||||
data: etData,
|
||||
|
||||
async fill() {
|
||||
document.getElementById("bank-submit-button").disabled = true;
|
||||
const res = await fetch(et_data_url);
|
||||
// biome-ignore lint/correctness/noUndeclaredVariables: defined in eboutic_makecommand.jinja
|
||||
const res = await fetch(etDataUrl);
|
||||
if (res.ok) {
|
||||
this.data = await res.json();
|
||||
document.getElementById("bank-submit-button").disabled = false;
|
||||
@ -24,10 +29,10 @@ document.addEventListener("alpine:init", () => {
|
||||
|
||||
Alpine.data("billing_infos", () => ({
|
||||
/** @type {BillingInfoReqState | null} */
|
||||
req_state: null,
|
||||
reqState: null,
|
||||
|
||||
async send_form() {
|
||||
this.req_state = BillingInfoReqState.SENDING;
|
||||
async sendForm() {
|
||||
this.reqState = BillingInfoReqState.SENDING;
|
||||
const form = document.getElementById("billing_info_form");
|
||||
document.getElementById("bank-submit-button").disabled = true;
|
||||
const payload = Object.fromEntries(
|
||||
@ -35,11 +40,12 @@ document.addEventListener("alpine:init", () => {
|
||||
.filter((elem) => elem.type !== "submit" && elem.value)
|
||||
.map((elem) => [elem.name, elem.value]),
|
||||
);
|
||||
const res = await fetch(billing_info_url, {
|
||||
// biome-ignore lint/correctness/noUndeclaredVariables: defined in eboutic_makecommand.jinja
|
||||
const res = await fetch(billingInfoUrl, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
this.req_state = res.ok
|
||||
this.reqState = res.ok
|
||||
? BillingInfoReqState.SUCCESS
|
||||
: BillingInfoReqState.FAILURE;
|
||||
if (res.status === 422) {
|
||||
@ -56,22 +62,24 @@ document.addEventListener("alpine:init", () => {
|
||||
}
|
||||
},
|
||||
|
||||
get_alert_color() {
|
||||
if (this.req_state === BillingInfoReqState.SUCCESS) {
|
||||
getAlertColor() {
|
||||
if (this.reqState === BillingInfoReqState.SUCCESS) {
|
||||
return "green";
|
||||
}
|
||||
if (this.req_state === BillingInfoReqState.FAILURE) {
|
||||
if (this.reqState === BillingInfoReqState.FAILURE) {
|
||||
return "red";
|
||||
}
|
||||
return "";
|
||||
},
|
||||
|
||||
get_alert_message() {
|
||||
if (this.req_state === BillingInfoReqState.SUCCESS) {
|
||||
return billing_info_success_message;
|
||||
getAlertMessage() {
|
||||
if (this.reqState === BillingInfoReqState.SUCCESS) {
|
||||
// biome-ignore lint/correctness/noUndeclaredVariables: defined in eboutic_makecommand.jinja
|
||||
return billingInfoSuccessMessage;
|
||||
}
|
||||
if (this.req_state === BillingInfoReqState.FAILURE) {
|
||||
return billing_info_failure_message;
|
||||
if (this.reqState === BillingInfoReqState.FAILURE) {
|
||||
// biome-ignore lint/correctness/noUndeclaredVariables: defined in eboutic_makecommand.jinja
|
||||
return billingInfoFailureMessage;
|
||||
}
|
||||
return "";
|
||||
},
|
||||
|
@ -56,11 +56,11 @@
|
||||
{# Total price #}
|
||||
<li style="margin-top: 20px">
|
||||
<span class="item-name"><strong>{% trans %}Basket amount: {% endtrans %}</strong></span>
|
||||
<span x-text="get_total().toFixed(2) + ' €'" class="item-price"></span>
|
||||
<span x-text="getTotal().toFixed(2) + ' €'" class="item-price"></span>
|
||||
</li>
|
||||
</ul>
|
||||
<div class="catalog-buttons">
|
||||
<button @click="clear_basket()" class="btn btn-grey">
|
||||
<button @click="clearBasket()" class="btn btn-grey">
|
||||
<i class="fa fa-trash"></i>
|
||||
{% trans %}Clear{% endtrans %}
|
||||
</button>
|
||||
@ -106,7 +106,7 @@
|
||||
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 }})'
|
||||
@click='addFromCatalog({{ p.id }}, {{ p.name|tojson }}, {{ p.selling_price }})'
|
||||
>
|
||||
{% if p.icon %}
|
||||
<img class="product-image" src="{{ p.icon.url }}"
|
||||
|
@ -56,7 +56,7 @@
|
||||
<div
|
||||
class="collapse"
|
||||
:class="{'shadow': collapsed}"
|
||||
x-data="{collapsed: !billing_info_exist}"
|
||||
x-data="{collapsed: !billingInfoExist}"
|
||||
x-cloak
|
||||
>
|
||||
<div class="collapse-header clickable" @click="collapsed = !collapsed">
|
||||
@ -73,27 +73,27 @@
|
||||
x-data="billing_infos"
|
||||
x-show="collapsed"
|
||||
x-transition.scale.origin.top
|
||||
@submit.prevent="await send_form()"
|
||||
@submit.prevent="await sendForm()"
|
||||
>
|
||||
{% csrf_token %}
|
||||
{{ billing_form }}
|
||||
<br>
|
||||
<br>
|
||||
<div
|
||||
x-show="[BillingInfoReqState.SUCCESS, BillingInfoReqState.FAILURE].includes(req_state)"
|
||||
x-show="[BillingInfoReqState.SUCCESS, BillingInfoReqState.FAILURE].includes(reqState)"
|
||||
class="alert"
|
||||
:class="'alert-' + get_alert_color()"
|
||||
:class="'alert-' + getAlertColor()"
|
||||
x-transition
|
||||
>
|
||||
<div class="alert-main" x-text="get_alert_message()"></div>
|
||||
<div class="clickable" @click="req_state = null">
|
||||
<div class="alert-main" x-text="getAlertMessage()"></div>
|
||||
<div class="clickable" @click="reqState = null">
|
||||
<i class="fa fa-close"></i>
|
||||
</div>
|
||||
</div>
|
||||
<input
|
||||
type="submit" class="btn btn-blue clickable"
|
||||
value="{% trans %}Validate{% endtrans %}"
|
||||
:disabled="req_state === BillingInfoReqState.SENDING"
|
||||
:disabled="reqState === BillingInfoReqState.SENDING"
|
||||
>
|
||||
</form>
|
||||
</div>
|
||||
@ -141,16 +141,16 @@
|
||||
|
||||
{% block script %}
|
||||
<script>
|
||||
const billing_info_url = '{{ url("api:put_billing_info", user_id=request.user.id) }}';
|
||||
const et_data_url = '{{ url("api:etransaction_data") }}';
|
||||
const billing_info_exist = {{ "true" if billing_infos else "false" }};
|
||||
const billing_info_success_message = "{% trans %}Billing info registration success{% endtrans %}";
|
||||
const billing_info_failure_message = "{% trans %}Billing info registration failure{% endtrans %}";
|
||||
const billingInfoUrl = '{{ url("api:put_billing_info", user_id=request.user.id) }}';
|
||||
const etDataUrl = '{{ url("api:etransaction_data") }}';
|
||||
const billingInfoExist = {{ "true" if billing_infos else "false" }};
|
||||
const billingInfoSuccessMessage = "{% trans %}Billing info registration success{% endtrans %}";
|
||||
const billingInfoFailureMessage = "{% trans %}Billing info registration failure{% endtrans %}";
|
||||
|
||||
{% if billing_infos %}
|
||||
const et_data = {{ billing_infos|safe }}
|
||||
const etData = {{ billing_infos|safe }}
|
||||
{% else %}
|
||||
const et_data = {}
|
||||
const etData = {}
|
||||
{% endif %}
|
||||
</script>
|
||||
{{ super() }}
|
||||
|
Reference in New Issue
Block a user