Skip to content

Commit

Permalink
making the list dynamic when a product is purchased ($)
Browse files Browse the repository at this point in the history
  • Loading branch information
gean-ferreira committed Oct 16, 2021
1 parent 612a23f commit 9cffb8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
24 changes: 15 additions & 9 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@
import ShoppingList from "./ShoppingList.svelte";
import { Button, Table, Form, FormGroup, Label, Input } from "sveltestrap";
let totalPdt = 0;
let totalPurchases = 0;
let name,
qty = 1,
price;
let totalPdt = 0,
totalPurchases = 0;
let list = JSON.parse(localStorage.getItem("shopping-list")) ?? [];
let name;
let qty = 1;
let price;
$: {
localStorage.setItem("shopping-list", JSON.stringify(list));
}
$: outstandingProducts = list.filter((item) => !item.bought).length;
$: outstandingProducts = list.filter((pdt) => !pdt.bought).length;
$: totalPurchases = list.reduce((amount, pdt) => {
if (pdt.bought) {
return amount + parseFloat(pdt.total);
} else {
return amount;
}
}, 0);
function addPdt() {
console.log("nome: " + name);
totalPdt = qty * price;
totalPurchases += totalPdt;
list = [
...list,
Expand Down Expand Up @@ -102,7 +108,7 @@
{/if}

<div>Outstanding products: {outstandingProducts}</div>
<div>Total purchases:</div>
<span>Total purchases:</span>
<span>US$ {totalPurchases.toFixed(2)}</span>
</main>

Expand Down
10 changes: 2 additions & 8 deletions src/ShoppingList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
const dispatch = createEventDispatcher();
export let bought;
export let qty;
export let name;
export let price;
export let total;
export let bought, qty, name, price, total;
</script>

<tr>
Expand All @@ -17,7 +13,6 @@
<td><span>{name}</span></td>
<td><span>{price}</span></td>
<td><span>{total}</span></td>
<td><button class="editBtn"><Icon name="three-dots-vertical" /></button></td>
<td
><button class="trashBtn" on:click={() => dispatch("deletePdt")}
><Icon name="trash-fill" /></button
Expand All @@ -26,8 +21,7 @@
</tr>

<style>
.trashBtn,
.editBtn {
.trashBtn {
padding: 0;
border: none;
background-color: white;
Expand Down

1 comment on commit 9cffb8c

@vercel
Copy link

@vercel vercel bot commented on 9cffb8c Oct 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.