Skip to content

Commit

Permalink
feat: A new component: CategorySelection
Browse files Browse the repository at this point in the history
  • Loading branch information
danielschmitz committed May 23, 2019
1 parent 9ebffa9 commit c7b5814
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 14 deletions.
29 changes: 29 additions & 0 deletions src/components/CategorySelection.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script>
import { onMount } from "svelte";
let categories = [];
export let selected;
onMount(async () => {
categories = getCategories();
});
async function getCategories() {
const result = await fetch("https://northwind.now.sh/api/categories");
if (result.ok) return result.json();
}
</script>

<div class="select">
{#await categories}
<progress class="progress is-small is-primary" max="100">15%</progress>
{:then list}
<select bind:value={selected}>
{#each list as item}
<option value={item.id}>{item.name}</option>
{/each}
</select>
{:catch}
Error!
{/await}
</div>
70 changes: 56 additions & 14 deletions src/routes/products.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<script>
import Card from "../components/Card.svelte";
import CategorySelection from "../components/CategorySelection.svelte"
import { onMount } from "svelte";
import http from "../http";
Expand All @@ -21,7 +23,9 @@
async function getProducts() {
// a simple use of "fetch"
const result = await fetch("https://northwind.now.sh/api/products/?_expand=category&_expand=supplier");
const result = await fetch(
"https://northwind.now.sh/api/products/?_expand=category&_expand=supplier"
);
if (result.ok) return result.json();
Expand All @@ -37,8 +41,8 @@
async function onDeleteClick(item) {
product = item;
if (confirm(`Delete "${product.name}"?`)){
await http.delete(`/products/${product.id}`)
if (confirm(`Delete "${product.name}"?`)) {
await http.delete(`/products/${product.id}`);
products = getProducts();
}
}
Expand All @@ -53,10 +57,10 @@
function onNewClick() {
product = {
name: '',
description: ''
}
openModal()
name: "",
description: ""
};
openModal();
}
async function save() {
Expand Down Expand Up @@ -125,7 +129,13 @@
{/await}

</div>
<a href="javascript:;" slot="footer" class="card-footer-item" on:click={onNewClick}>New</a>
<a
href="javascript:;"
slot="footer"
class="card-footer-item"
on:click={onNewClick}>
New
</a>
</Card>

<div class={modalClass}>
Expand All @@ -147,14 +157,46 @@
</div>
</div>
<div class="field">
<label class="label">Description</label>
<label class="label">Category</label>
<div class="control">
<input
class="input"
type="text"
placeholder=""
bind:value={product.description} />
<CategorySelection bind:selected={product.categoryId}/>
</div>
</div>
<div class="columns is-desktop">
<div class="column field">
<label class="label">Quantity Per Unit</label>
<div class="control">
<input
class="input"
type="text"
placeholder=""
bind:value={product.quantityPerUnit} />
</div>
</div>
<div class="column field">
<label class="label">Unit Price</label>
<div class="control">
<input
class="input"
type="text"
placeholder=""
bind:value={product.unitPrice} />
</div>
</div>
<div class="column field">
<label class="label">Unit in Stock</label>
<div class="control">
<input
class="input"
type="text"
placeholder=""
bind:value={product.unitsInStock} />
</div>
</div>




</div>
</section>
<footer class="modal-card-foot">
Expand Down

0 comments on commit c7b5814

Please # to comment.