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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
<script>
import { btc, f, sat, post } from "$lib/utils";
import { browser } from "$app/environment";
import { goto, invalidate } from "$app/navigation";
import { fade } from "svelte/transition";
import { cubicIn } from "svelte/easing";
import { flip } from "svelte/animate";
import {
dndzone,
SHADOW_ITEM_MARKER_PROPERTY_NAME,
SOURCES,
TRIGGERS,
} from "svelte-dnd-action";
import DeleteItem from "$comp/DeleteItem.svelte";
import Icon from "$comp/Icon.svelte";
export let subject, user, items, total, rate, currency;
let flipDurationMs = 200;
let dropTargetStyle = {};
let dragDisabled =
subject?.id !== user?.id || (browser && window.innerWidth < 768);
let deleting;
let del = (item) => {
deleting = item;
};
function handleConsider(e) {
const {
items: newItems,
info: { source, trigger },
} = e.detail;
items = newItems;
if (source === SOURCES.KEYBOARD && trigger === TRIGGERS.DRAG_STOPPED)
reset();
}
async function handleFinalize(e) {
const {
items: newItems,
info: { source },
} = e.detail;
items = newItems;
if (source === SOURCES.POINTER) reset();
if (subject?.id === user?.id) {
await post("/api/items/sort", { items });
invalidate("app:items");
}
}
function startDrag(e) {
e.preventDefault();
dragDisabled = false;
}
function handleKeyDown(e) {
if ((e.key === "Enter" || e.key === " ") && dragDisabled)
dragDisabled = false;
}
$: if (total > 0 && browser) {
let payButton = document.querySelector("#payButton");
if (payButton) payButton.addEventListener("click", checkout);
}
let checkout = async () => {
let invoice = {
type: "lightning",
amount: btc(total, rate),
currency,
items: items.filter((i) => i.quantity > 0),
prompt: subject.prompt,
memoPrompt: subject.memoPrompt,
rate,
};
let { id } = await post(`/${subject.username}/invoice`, {
invoice,
user: { username: subject.username },
});
if (invoice.memoPrompt) {
return goto(`/${subject.username}/invoice/${id}/memo`);
}
goto(`/${subject.username}/invoice/${id}`);
};
let reset = () =>
(dragDisabled =
user?.username !== subject?.username || window.innerWidth < 768);
</script>
<svelte:window on:resize={reset} />
{#if deleting}
<DeleteItem bind:item={deleting} />
{/if}
{#if items.length}
<h2 class="text-2xl font-bold">Select items</h2>
{/if}
<div
class="grid sm:grid-cols-2 gap-4 pb-20"
use:dndzone={{ items, flipDurationMs, dropTargetStyle, dragDisabled }}
on:consider={handleConsider}
on:finalize={handleFinalize}
>
{#each items as i (i.id)}
<button
type="button"
class="w-full text-center cursor-pointer hover:opacity-80 mb-auto"
on:click={() => i.quantity++}
>
<div class="relative w-full h-64 overflow-hidden rounded-xl">
{#if i[SHADOW_ITEM_MARKER_PROPERTY_NAME]}
<div
in:fade={{ duration: 200, easing: cubicIn }}
class="custom-shadow-item"
/>
{:else if i.image}
<img
src={`/api/public/${i.image}.webp`}
alt={i.name}
class="mx-auto object-cover h-full w-full"
/>
{:else}
<div
class="bg-gradient-to-r from-primary to-gradient mb-4 cursor-pointer hover:opacity-80 w-full h-full"
alt="Banner"
/>
{/if}
{#if user?.username === subject.username}
<div class="flex gap-2 justify-center absolute top-2 right-2">
<button type="button" on:click|stopPropagation={() => {}}>
<a href={`/${user.username}/items/${i.id}`}>
<button
class="bg-black rounded-full w-12 h-12 bg-opacity-40 hover:bg-opacity-100"
>
<Icon icon="edit" style="w-8 mx-auto invert" />
</button>
</a>
</button>
<button
class="bg-black rounded-full w-12 h-12 bg-opacity-40 hover:bg-opacity-100"
on:click|stopPropagation={() => del(i)}
>
<Icon icon="trash" style="w-8 mx-auto invert" />
</button>
<button
type="button"
on:click|stopPropagation={() => {}}
class="sm:hidden"
on:mousedown={startDrag}
on:touchstart={startDrag}
on:keydown={handleKeyDown}
>
<a href={`/${user.username}/items/${i.id}`}>
<button
class="bg-black rounded-full w-12 h-12 bg-opacity-40 hover:bg-opacity-100"
>
<Icon icon="move" style="w-8 mx-auto invert" />
</button>
</a>
</button>
</div>
{/if}
</div>
<div class="bg-white rounded-xl py-2">
<div class="flex text-left gap-2">
<div class="overflow-hidden space-y-1">
<div class="text-2xl break-words">{i.name}</div>
<div class="flex gap-2">
<div class="font-semibold text-lg">
{f(i.price, currency)}
</div>
<div class="text-secondary my-auto">
{sat(btc(i.price, rate))}
</div>
</div>
</div>
<div class="flex gap-1 ml-auto mb-auto justify-around tabular-nums">
{#if i.quantity}
<button
type="button"
on:click|stopPropagation={() => i.quantity > 0 && i.quantity--}
>
<Icon icon="minus" style="w-10 min-w-10" />
</button>
{/if}
<div class="text-2xl my-auto text-center">
{#if i.quantity}{i.quantity}{:else}—{/if}
</div>
{#if i.quantity}
<button
type="button"
on:click|stopPropagation={() => i.quantity++}
>
<Icon icon="plus" style="w-10 min-w-10" />
</button>
{/if}
</div>
</div>
</div>
</button>
{/each}
</div>
|