2023-01-17 20:14:05 +01:00
|
|
|
<script lang="ts">
|
2023-01-24 15:09:20 +01:00
|
|
|
import { onMount } from "svelte";
|
2023-01-19 18:22:46 +01:00
|
|
|
import { inventory } from "../stores/inventory"
|
2023-01-31 22:35:58 +01:00
|
|
|
import inventoryApi from "../lib/api";
|
2023-01-17 20:14:05 +01:00
|
|
|
|
2023-01-19 18:22:46 +01:00
|
|
|
export let drinkName: string;
|
2023-01-19 00:17:51 +01:00
|
|
|
|
2023-01-19 18:22:46 +01:00
|
|
|
let amountContainers: number;
|
|
|
|
let amountBottles: number;
|
|
|
|
|
|
|
|
const saveInventory = () => {
|
2023-01-31 22:35:58 +01:00
|
|
|
// If there is not a timestamp entry yet, create one. This one is only used to show an info window for the user
|
|
|
|
// to indicate when the inventory was started. It won't get processed by the backend.
|
|
|
|
if ( !$inventory.some(item => "inventoryStartedAt" in item)) {
|
2023-01-25 01:30:27 +01:00
|
|
|
let _unixTime = Date.now();
|
|
|
|
let _date = new Date(_unixTime).toLocaleDateString();
|
|
|
|
let _time = new Date(_unixTime).toLocaleTimeString();
|
|
|
|
$inventory = [{
|
2023-01-31 22:35:58 +01:00
|
|
|
inventoryStartedAt: _date + " um " + _time
|
2023-01-25 01:30:27 +01:00
|
|
|
}, ...$inventory];
|
|
|
|
}
|
2023-01-19 18:22:46 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$inventory = [
|
|
|
|
{
|
|
|
|
name: drinkName,
|
|
|
|
amountContainers: amountContainers,
|
|
|
|
amountBottles: amountBottles,
|
|
|
|
},
|
|
|
|
...$inventory,
|
|
|
|
];
|
|
|
|
}
|
2023-01-31 22:35:58 +01:00
|
|
|
inventoryApi.save("drinks", $inventory);
|
2023-01-19 18:22:46 +01:00
|
|
|
};
|
2023-01-24 15:09:20 +01:00
|
|
|
|
|
|
|
const loadAmountsFromInventory = () => {
|
|
|
|
$inventory.forEach(item => {
|
|
|
|
if (item.name === drinkName) {
|
|
|
|
amountContainers = item.amountContainers;
|
|
|
|
amountBottles = item.amountBottles;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onMount(async () => {
|
|
|
|
loadAmountsFromInventory();
|
|
|
|
})
|
|
|
|
|
2023-01-19 18:22:46 +01:00
|
|
|
</script>
|
2023-01-19 00:17:51 +01:00
|
|
|
|
2023-01-19 18:22:46 +01:00
|
|
|
<div class="box">
|
|
|
|
<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}>
|
|
|
|
</div>
|
|
|
|
<div class="column">
|
|
|
|
<input class="input is-rounded" type="number" placeholder="Anzahl einzelne Flaschen" bind:value={amountBottles} on:input={saveInventory}>
|
|
|
|
</div>
|
2023-01-18 22:58:07 +01:00
|
|
|
</div>
|
2023-01-19 18:22:46 +01:00
|
|
|
</div>
|