aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Tip.svelte
blob: 8509405cd43bd178cbcf52c965991186a8e4fed5 (plain) (blame)
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
210
211
212
213
<script>
	import { Icon } from '$comp';
	import { t } from '$lib/translations';
	import { f, post, s } from '$lib/utils';
	import { tick } from 'svelte';
	import { goto } from '$app/navigation';

	export let amountFiat,
		showCustomAmount,
		showMobileTip,
		tipPercent,
		tipAmount,
		amount,
		currency,
		tip,
		prompt,
		rate,
		text,
		username;

	const tipAmounts = ['None', '10%', '15%', '20%'];

	let customInput;
	let customTipAmount;

	let apply = async () => {
		if (customTipAmount > 0) {
			tipPercent = (customTipAmount / amountFiat) * 100;
			console.log('TIP PERCENT', tipPercent);
			update();
			showMobileTip = false;
			showCustomAmount = false;
		}
	};

	let update = async () => {
		await tick();
		let { uuid } = await post(`/invoice`, {
			amount: parseInt(amount + tip),
			prompt,
			rate,
			tip,
			text,
			user: { username }
		});

		goto(`/invoice/${uuid}`);
	};

	const handleCustomTipAmount = (e) => {
		tipPercent = 0;
		customTipAmount = e.target.value;
	};

	const handleMobileClose = () => {
		tipPercent = 0;
		if (customInput) {
			customInput.value = '';
		}
		customTipAmount = 0;
		tipAmount = 0;
		showMobileTip = false;
		showCustomAmount = false;
	};

	let timeout;
	const handleSlide = (e) => {
		customTipAmount = '';
		if (customInput) {
			customInput.value = '';
		}
		tipPercent = e.target.value;

		clearTimeout(timeout);
		timeout = setTimeout(update, 100);
	};

	const handleTipButtonClick = (amount) => {
		customTipAmount = '';
		if (customInput) {
			customInput.value = '';
		}

		if (amount === 'None') {
			tipPercent = 0;
		} else {
			tipPercent = parseInt(amount.slice(0, 2));
		}
		showMobileTip = false;
		update();
	};
</script>

<div
	class="w-[100%] md:w-auto absolute {showMobileTip
		? 'bottom-0'
		: '-top-full'} bg-white p-6 md:p-0 rounded-t-3xl md:rounded-none left-0 md:static text-center space-y-5"
>
	<div class="flex justify-end items-center">
		<button class="block md:hidden bg-primary rounded-full p-2" on:click={handleMobileClose}
			><Icon icon="close" /></button
		>
		<button
			class="hidden {showCustomAmount ? 'md:block' : ''}"
			on:click={() => (showCustomAmount = false)}
			><Icon icon="close" style="hover:opacity-80" /></button
		>
	</div>
	<h1 class="hidden md:block text-4xl font-semibold">{$t('invoice.addTipq')}</h1>
	{#if !showCustomAmount}
		<div>
			<span class="font-semibold mr-1">{f(tipAmount, currency)}</span><span
				class="text-secondary font-semibold text-sm">{`(${s(tip)} SAT)`}</span
			>
			<span class="block text-secondary text-sm">{Math.round(tipPercent)}%</span>
		</div>

		<input
			id="slider"
			type="range"
			min="0"
			max="100"
			value={tipPercent}
			class="px-2 py-0"
			on:input={handleSlide}
			on:change={() => (showMobileTip = false)}
		/>
	{/if}
	<div>
		{#if !showCustomAmount}
			{#each tipAmounts as amount}
				<button
					class="bg-black text-white md:text-black md:bg-primary md:hover:bg-black md:hover:text-white w-16 md:w-20 py-3 m-1 rounded-2xl font-semibold {customTipAmount
						? ''
						: Math.round(tipPercent) === parseInt(amount.slice(0, 2)) ||
						  (!tipPercent && amount === 'None')
						? '!bg-black !text-white'
						: ''}"
					on:click={() => handleTipButtonClick(amount)}>{amount}</button
				>
			{/each}
		{/if}
		{#if showCustomAmount}
			<div class="relative">
				<span
					class="absolute top-[18px] left-5 font-semibold {!customTipAmount
						? 'opacity-50'
						: 'opacity-100'}">$</span
				>
				<input
					bind:this={customInput}
					type="number"
					on:input={(e) => handleCustomTipAmount(e)}
					class="pl-10"
					placeholder="Other"
				/>
				<button
					class="w-full mt-2 bg-black text-white font-semibold py-3 px-7 rounded-2xl {!customTipAmount
						? 'opacity-50'
						: 'opacity-100 hover:opacity-80'}"
					disabled={!customTipAmount}
					on:click={apply}>Apply</button
				>
				<!-- found missing translation --->
			</div>
		{:else}
			<div>
				<button
					class="w-full md:w-auto mt-2 bg-black md:bg-primary text-white md:text-black md:hover:bg-black md:hover:text-white font-semibold py-3 px-7 rounded-2xl"
					on:click={() => (showCustomAmount = true)}>Custom amount</button
				>
			</div>
		{/if}
	</div>
</div>

<div
	class="hidden md:block h-[1px] lg:h-[600px] w-full lg:w-[1px] bg-lightgrey rounded-xl"
	id="section-divider"
/>

<style>
	#slider {
		-webkit-appearance: none;
		width: 100%;
		height: 6px;
		border-radius: 5px;
		background: #eaecef;
		outline: none;
	}

	#slider:active {
		background-color: black;
	}

	#slider::-webkit-slider-thumb {
		-webkit-appearance: none;
		appearance: none;
		width: 25px;
		height: 25px;
		border-radius: 50%;
		background: black;
		cursor: pointer;
	}

	#slider::-moz-range-thumb {
		width: 25px;
		height: 25px;
		border-radius: 50%;
		background: black;
		cursor: pointer;
	}
</style>