spartacus-inventur-frontend/src/components/Drink.svelte

89 lines
2.3 KiB
Svelte
Raw Normal View History

2023-01-17 20:14:05 +01:00
<script lang="ts">
2023-03-27 19:49:02 +02:00
import { onMount } from "svelte";
import { inventory, inventoryStartedAt } from "../stores/inventory";
import inventoryApi from "../lib/api";
2023-01-17 20:14:05 +01:00
2023-03-27 19:49:02 +02:00
export let drinkName: string;
export let drinkType: string;
2023-01-19 00:17:51 +01:00
2023-03-27 19:49:02 +02:00
let amountContainers: number;
let amountBottles: number;
2023-03-27 19:49:02 +02:00
const setStartOfInventory = () => {
if ($inventoryStartedAt.length < 1) {
$inventoryStartedAt = inventoryApi.createTimestamp();
inventoryApi.save("inventoryStartedAt", $inventoryStartedAt);
}
2023-03-27 19:49:02 +02:00
};
2023-03-27 19:49:02 +02:00
const saveInventory = () => {
// Check if a drink is already in the inventory. If yes, then update the amounts. If not, add it.
if ($inventory.some((item) => item.name === drinkName)) {
$inventory.forEach((item) => {
if (drinkName === item.name) {
item.amountContainers = amountContainers;
item.amountBottles = amountBottles;
}
2023-03-27 19:49:02 +02:00
});
} else {
$inventory = [
{
name: drinkName,
type: drinkType,
amountContainers: amountContainers,
amountBottles: amountBottles,
},
...$inventory,
];
}
2023-03-27 19:49:02 +02:00
inventoryApi.save("drinks", $inventory);
};
2023-03-27 19:49:02 +02:00
const loadAmountsFromInventory = () => {
$inventory.forEach((item) => {
if (item.name === drinkName) {
amountContainers = item.amountContainers;
amountBottles = item.amountBottles;
}
});
};
2023-03-27 19:49:02 +02:00
onMount(async () => {
loadAmountsFromInventory();
// Check if an inventory has been ongoing on the current device
$inventoryStartedAt = await inventoryApi.load("inventoryStartedAt");
});
</script>
2023-01-19 00:17:51 +01:00
<div class="box">
2023-03-27 19:49:02 +02:00
<div class="columns">
<div class="column is-one-third">
{drinkName}
</div>
<div class="column">
<input
class="input is-primary"
type="number"
placeholder="Anzahl volle Gebinde/Kästen"
bind:value={amountContainers}
on:input={() => {
saveInventory();
setStartOfInventory();
}}
/>
</div>
<div class="column">
<input
class="input is-rounded"
type="number"
placeholder="Anzahl einzelne Flaschen"
bind:value={amountBottles}
on:input={() => {
saveInventory();
setStartOfInventory();
}}
/>
2023-01-18 22:58:07 +01:00
</div>
2023-03-27 19:49:02 +02:00
</div>
</div>