diff --git a/02-reactivity-order/README.md b/02-reactivity-order/README.md index 345022e..5771fa0 100644 --- a/02-reactivity-order/README.md +++ b/02-reactivity-order/README.md @@ -6,7 +6,7 @@ We will simulate a simple order system, and we will calculate total, subtotal an # Step By Step Guide -- We will take as starting point: _00-scratch-typescript_, let's copy the content of that folder and +- We will take as starting point: _00-boiler-typescript_, let's copy the content of that folder and execute ```bash @@ -16,7 +16,7 @@ npm install - Let's create in `/src` a subfolder called _orders_ ```bash -md orders +md src/orders ``` - First we are going to define the order model: @@ -43,7 +43,7 @@ export const createNewItem = (): Item => ({ Let's define now the order component, we will start with something simple: -_./src/order.svelte_ +_./src/orders/order.svelte_ ```svelte

Order

@@ -60,7 +60,7 @@ export { default as OrderComponent } from "./order.svelte"; And let's consume it in our _app_ file just to ensure that we have wired up the basics, we will just replace the whole content -_./src/app.svelte_ +_./src/App.svelte_ ```svelte -

Order Component

+

Order Component

``` - Cool let's show the data..., we will use the _#each_ directive to iterate over the items and display them. -_./src/order.svelte_ +_./src/orders/order.svelte_ ```diff -

Order Component

-+ {#each order.itemCollection as item, index(index)} -+ {item.name} -+ {item.quantity} -+ {item.price} -+ here goes total -+ +

Orders

++ {#each order.itemCollection as item, index (index)} ++ {item.name} ++ {item.quantity} ++ {item.price} ++ here goes total ++ + {/each} ``` - If we run the example, we can check that the data is being shown although the layout looks a bit ugly, let's add some styling, we will add a grid layout and create a header section. -_./src/order.svelte_ +_./src/orders/order.svelte_ ```diff +
-+

Name

-+

Qty

-+

Price

-+

Total

-+

Commands

- - {#each order.itemCollection as item, index} - {item.name} - {item.quantity} - {item.price} - here goes total - - {/each} ++

Name

++

Qty

++

Price

++

Total

++

Commands

+ + {#each order.itemCollection as item, index} + {item.name} + {item.quantity} + {item.price} + here goes total + + {/each} +
+ + ``` We are just assigning a new array that contains the old array and the new item. @@ -251,12 +248,12 @@ what would happen? ```diff
- +- ++
``` @@ -264,12 +261,12 @@ It wont' work because there's no assignment and svelte does not notice about the update, but what if we try something like? ```diff - + ``` It works, there is an assignment... but what an ugly code :D @@ -279,47 +276,47 @@ total (subtotal + taxes). - First let's add some markup to display the subtotal, the vat and the total: -_./src/order.svelte_ +_./src/orders/order.svelte_ ```diff
-+
-+
Subtotal: {subtotal}
-+
VAT:{vat}
-+
Total: {total}
-+
++
++
Subtotal: {subtotal}
++
VAT: {vat}
++
Total: {total}
++
- - // (...) -+ .total-container { -+ display:flex; -+ justify-content: flex-end; -+ gap: 50px; -+ margin-top: 20px; -+ } - + + // (...) ++ .total-container { ++ display: flex; ++ justify-content: flex-end; ++ gap: 50px; ++ margin-top: 20px; ++ } + ``` - Now comes the magic, using the _$:_ directive we are just indicating the app to reevaluate this assignment whenever any the variables on the right side of the assignment are changed. -_./src/order.svelte_ +_./src/orders/order.svelte_ ```diff let subtotal = 0; let vat = 0; let total = 0; -+ $: subtotal = order.itemCollection.reduce((acc, item) => { -+ return acc + (item.price * item.quantity); -+ }, 0); ++ $: subtotal = order.itemCollection.reduce((acc, item) => { ++ return acc + (item.price * item.quantity); ++ }, 0); + -+ $: vat = subtotal * 0.21; ++ $: vat = subtotal * 0.21; + -+ $: total = subtotal + vat; ++ $: total = subtotal + vat; ``` - Now we can start adding items and check diff --git a/02-reactivity-order/package-lock.json b/02-reactivity-order/package-lock.json index c732276..039db75 100644 --- a/02-reactivity-order/package-lock.json +++ b/02-reactivity-order/package-lock.json @@ -11,7 +11,7 @@ "@sveltejs/vite-plugin-svelte": "^1.0.1", "@tsconfig/svelte": "^3.0.0", "prettier-plugin-svelte": "^2.7.0", - "svelte": "^3.49.0", + "svelte": "^3.50.1", "svelte-check": "^2.8.0", "svelte-preprocess": "^4.10.7", "tslib": "^2.4.0", @@ -1284,9 +1284,9 @@ } }, "node_modules/svelte": { - "version": "3.49.0", - "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.49.0.tgz", - "integrity": "sha512-+lmjic1pApJWDfPCpUUTc1m8azDqYCG1JN9YEngrx/hUyIcFJo6VZhj0A1Ai0wqoHcEIuQy+e9tk+4uDgdtsFA==", + "version": "3.50.1", + "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.50.1.tgz", + "integrity": "sha512-bS4odcsdj5D5jEg6riZuMg5NKelzPtmsCbD9RG+8umU03TeNkdWnP6pqbCm0s8UQNBkqk29w/Bdubn3C+HWSwA==", "dev": true, "engines": { "node": ">= 8" @@ -2295,9 +2295,9 @@ "dev": true }, "svelte": { - "version": "3.49.0", - "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.49.0.tgz", - "integrity": "sha512-+lmjic1pApJWDfPCpUUTc1m8azDqYCG1JN9YEngrx/hUyIcFJo6VZhj0A1Ai0wqoHcEIuQy+e9tk+4uDgdtsFA==", + "version": "3.50.1", + "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.50.1.tgz", + "integrity": "sha512-bS4odcsdj5D5jEg6riZuMg5NKelzPtmsCbD9RG+8umU03TeNkdWnP6pqbCm0s8UQNBkqk29w/Bdubn3C+HWSwA==", "dev": true }, "svelte-check": { diff --git a/02-reactivity-order/package.json b/02-reactivity-order/package.json index c7b1985..e12b601 100644 --- a/02-reactivity-order/package.json +++ b/02-reactivity-order/package.json @@ -13,7 +13,7 @@ "@sveltejs/vite-plugin-svelte": "^1.0.1", "@tsconfig/svelte": "^3.0.0", "prettier-plugin-svelte": "^2.7.0", - "svelte": "^3.49.0", + "svelte": "^3.50.1", "svelte-check": "^2.8.0", "svelte-preprocess": "^4.10.7", "tslib": "^2.4.0", diff --git a/02-reactivity-order/src/orders/order.svelte b/02-reactivity-order/src/orders/order.svelte index 3204eb4..d0d2af9 100644 --- a/02-reactivity-order/src/orders/order.svelte +++ b/02-reactivity-order/src/orders/order.svelte @@ -24,8 +24,8 @@ $: total = subtotal + vat; - export const removeItem = (item : Item) => { - // Svelte's reactivity is triggered by assignments. Therefore push, pop, slice etc do not work + export const removeItem = (item: Item) => { + // Svelte's reactivity is triggered by assignments. Therefore push, pop, splice etc do not work order.itemCollection = order.itemCollection.filter((e) => e !== item); }; @@ -39,7 +39,7 @@

Total

Commands

- {#each order.itemCollection as item, index(index)} + {#each order.itemCollection as item, index (index)} @@ -58,7 +58,7 @@
Subtotal: {subtotal}
-
VAT:{vat}
+
VAT: {vat}
Total: {total}