1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
<script>
import { t } from "$lib/translations";
import { upload } from "$lib/upload";
import { fail } from "$lib/utils";
import { applyAction, deserialize } from "$app/forms";
import Spinner from "$comp/Spinner.svelte";
export let item;
export let user;
let fileInput, formElement, file, submitting, progress;
let select = () => fileInput.click();
let src;
let tooLarge = {};
let handleFile = async ({ target }, type) => {
tooLarge[type] = false;
file = target.files[0];
if (!file) return;
if (file.size > 10000000) return (tooLarge[type] = true);
var reader = new FileReader();
reader.onload = async (e) => {
src = e.target.result;
};
reader.readAsDataURL(file);
};
async function handleSubmit() {
try {
submitting = true;
let data = new FormData(formElement);
if (src) {
try {
let { hash } = JSON.parse(await upload(file, "item", progress));
data.set("image", hash);
await fetch(`/api/public/${hash}.webp`, {
cache: "reload",
mode: "no-cors",
});
} catch (e) {
console.log("problem upsubmitting avatar", e);
}
}
const response = await fetch(formElement.action, {
method: "POST",
body: data,
});
const result = deserialize(await response.text());
applyAction(result);
} catch (e) {
console.log(e);
fail("Something went wrong");
}
submitting = false;
}
</script>
<form
method="POST"
class="space-y-5"
on:submit|preventDefault={handleSubmit}
bind:this={formElement}
>
<input type="hidden" name="id" bind:value={item.id} />
<input type="hidden" name="image" bind:value={item.image} />
<div>
<label for="name" class="font-bold mb-1 block">{$t("items.name")}</label>
<input name="name" bind:value={item.name} />
</div>
<div>
<label for="price" class="font-bold mb-1 block">{$t("items.price")}</label>
<div class="flex">
<input
name="price"
bind:value={item.price}
class="border-r-none rounded-r-none"
/>
<div
class="text-gray-600 rounded-r-2xl p-4 my-auto rounded-l-none rounded border bg-gray-100"
>
{user.currency}
</div>
</div>
</div>
<div class="w-full">
<label for="img" class="font-bold mb-1 block">{$t("items.image")}</label>
<div class="grid sm:grid-cols-2 gap-4">
<div class="h-64 rounded-2xl overflow-hidden">
{#if src}
<img
{src}
alt={item.name}
class="object-cover w-full h-full"
on:click={select}
on:keydown={select}
/>
{:else if item.image}
<img
src={`/api/public/${item.image}.webp`}
alt={item.name}
class="object-cover w-full h-full"
on:click={select}
on:keydown={select}
/>
{:else}
<div
class="bg-gradient-to-r from-primary to-gradient mb-4 cursor-pointer hover:opacity-80 w-full h-full"
on:click={select}
on:keydown={select}
alt="Banner"
/>
{/if}
</div>
</div>
<input
type="file"
class="hidden"
bind:this={fileInput}
on:change={(e) => handleFile(e, "item")}
/>
</div>
<button
class="rounded-full py-5 px-6 font-bold hover:opacity-80 bg-black text-white text-2xl w-full text-center"
>
{#if submitting}
<Spinner />
{:else}
{$t("items.submit")}
{/if}
</button>
</form>
|