Merge pull request #919 from ae-utbm/ts-eboutic

Migrate eboutic to Typescript
This commit is contained in:
thomas girod 2024-11-14 11:07:37 +01:00 committed by GitHub
commit db9f86c41e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 36 deletions

View File

@ -19,7 +19,7 @@ runs:
uses: actions/cache@v3 uses: actions/cache@v3
with: with:
path: ~/.local path: ~/.local
key: poetry-1 # increment to reset cache key: poetry-2 # increment to reset cache
- name: Install Poetry - name: Install Poetry
if: steps.cached-poetry.outputs.cache-hit != 'true' if: steps.cached-poetry.outputs.cache-hit != 'true'

View File

@ -1,21 +1,24 @@
/** export {};
* @typedef {Object} BasketItem An item in the basket
* @property {number} id The id of the product
* @property {string} name The name of the product
* @property {number} quantity The quantity of the product
* @property {number} unit_price The unit price of the product
*/
const BASKET_ITEMS_COOKIE_NAME = "basket_items"; interface BasketItem {
id: number;
name: string;
quantity: number;
// biome-ignore lint/style/useNamingConvention: the python code is snake_case
unit_price: number;
}
const BASKET_ITEMS_COOKIE_NAME: string = "basket_items";
/** /**
* Search for a cookie by name * Search for a cookie by name
* @param {string} name Name of the cookie to get * @param name Name of the cookie to get
* @returns {string|null|undefined} the value of the cookie or null if it does not exist, undefined if not found * @returns the value of the cookie or null if it does not exist, undefined if not found
*/ */
function getCookie(name) { function getCookie(name: string): string | null | undefined {
// biome-ignore lint/style/useBlockStatements: <explanation> if (!document.cookie || document.cookie.length === 0) {
if (!document.cookie || document.cookie.length === 0) return null; return null;
}
const found = document.cookie const found = document.cookie
.split(";") .split(";")
@ -27,9 +30,9 @@ function getCookie(name) {
/** /**
* Fetch the basket items from the associated cookie * Fetch the basket items from the associated cookie
* @returns {BasketItem[]|[]} the items in the basket * @returns the items in the basket
*/ */
function getStartingItems() { function getStartingItems(): BasketItem[] {
const cookie = getCookie(BASKET_ITEMS_COOKIE_NAME); const cookie = getCookie(BASKET_ITEMS_COOKIE_NAME);
if (!cookie) { if (!cookie) {
return []; return [];
@ -46,31 +49,34 @@ function getStartingItems() {
document.addEventListener("alpine:init", () => { document.addEventListener("alpine:init", () => {
Alpine.data("basket", () => ({ Alpine.data("basket", () => ({
items: getStartingItems(), items: getStartingItems() as BasketItem[],
/** /**
* Get the total price of the basket * Get the total price of the basket
* @returns {number} The total price of the basket * @returns {number} The total price of the basket
*/ */
getTotal() { getTotal() {
return this.items.reduce((acc, item) => acc + item.quantity * item.unit_price, 0); return this.items.reduce(
(acc: number, item: BasketItem) => acc + item.quantity * item.unit_price,
0,
);
}, },
/** /**
* Add 1 to the quantity of an item in the basket * Add 1 to the quantity of an item in the basket
* @param {BasketItem} item * @param {BasketItem} item
*/ */
add(item) { add(item: BasketItem) {
item.quantity++; item.quantity++;
this.setCookies(); this.setCookies();
}, },
/** /**
* Remove 1 to the quantity of an item in the basket * Remove 1 to the quantity of an item in the basket
* @param {BasketItem} item_id * @param itemId the id of the item to remove
*/ */
remove(itemId) { remove(itemId: number) {
const index = this.items.findIndex((e) => e.id === itemId); const index = this.items.findIndex((e: BasketItem) => e.id === itemId);
if (index < 0) { if (index < 0) {
return; return;
@ -78,7 +84,9 @@ 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) {
this.items = this.items.filter((e) => e.id !== this.items[index].id); this.items = this.items.filter(
(e: BasketItem) => e.id !== this.items[index].id,
);
} }
this.setCookies(); this.setCookies();
}, },
@ -105,19 +113,19 @@ document.addEventListener("alpine:init", () => {
/** /**
* Create an item in the basket if it was not already in * Create an item in the basket if it was not already in
* @param {number} id The id of the product to add * @param id The id of the product to add
* @param {string} name The name of the product * @param name The name of the product
* @param {number} price The unit price of the product * @param price The unit price of the product
* @returns {BasketItem} The created item * @returns The created item
*/ */
createItem(id, name, price) { createItem(id: number, name: string, price: number): BasketItem {
const newItem = { const newItem = {
id, id,
name, name,
quantity: 0, quantity: 0,
// biome-ignore lint/style/useNamingConvention: used by django backend // biome-ignore lint/style/useNamingConvention: the python code is snake_case
unit_price: price, unit_price: price,
}; } as BasketItem;
this.items.push(newItem); this.items.push(newItem);
this.add(newItem); this.add(newItem);
@ -128,12 +136,12 @@ document.addEventListener("alpine:init", () => {
/** /**
* Add an item to the basket. * Add an item to the basket.
* This is called when the user click on a button in the catalog * This is called when the user click on a button in the catalog
* @param {number} id The id of the product to add * @param id The id of the product to add
* @param {string} name The name of the product * @param name The name of the product
* @param {number} price The unit price of the product * @param price The unit price of the product
*/ */
addFromCatalog(id, name, price) { addFromCatalog(id: number, name: string, price: number) {
let item = this.items.find((e) => e.id === id); let item = this.items.find((e: BasketItem) => e.id === id);
// 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

View File

@ -11,7 +11,7 @@
{% block additional_js %} {% block additional_js %}
{# This script contains the code to perform requests to manipulate the {# This script contains the code to perform requests to manipulate the
user basket without having to reload the page #} user basket without having to reload the page #}
<script src="{{ static('eboutic/js/eboutic.js') }}"></script> <script src="{{ static('webpack/eboutic/eboutic-index.ts') }}"></script>
{% endblock %} {% endblock %}
{% block additional_css %} {% block additional_css %}