summaryrefslogtreecommitdiff
path: root/ping/frontend/src/lib/components/input/StockSelector.svelte
blob: 123712812276f6db7385d38dd9399a3f08a40a1b (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<script lang="ts">
	import { onMount } from 'svelte';
	import Button from '../Button.svelte';

	let { selectedStock = $bindable('AAPL') } = $props();

	let stocks: {
		symbol: string;
		shortname: string;
		quoteType: string;
		isYahooFinance: boolean;
	}[] = $state([]);
	let news: {
		link: string;
		title: string;
		thumbnail: { resolutions: { url: string; width: number; height: number }[] };
	}[] = $state([]);

	function openDialog() {
		const dialog = document.querySelector('dialog');
		const backdrop = document.getElementById('backdrop');
		// @ts-ignore
		dialog.open = true;
		// @ts-ignore
		backdrop.style.display = 'block';
	}

	function closeDialog() {
		const dialog = document.querySelector('dialog');
		const backdrop = document.getElementById('backdrop');
		// @ts-ignore
		dialog.open = false;
		// @ts-ignore
		backdrop.style.display = 'none';
	}

	function onSearch(event: SubmitEvent) {
		event.preventDefault();
		// @ts-ignore
		const fd = new FormData(event.target);
		const searchQuery = fd.get('search');

		fetch(`/stocksapi/search?query=${searchQuery}`, {
			method: 'GET',
			headers: {
				'Content-Type': 'application/json',
				'Cache-Control': 'no-cache'
			}
		})
			.then((response) => response.json())
			.then((data) => {
				stocks = data.quotes.filter((q: any) => q.isYahooFinance);
				news = data.news;
			})
			.catch((error) => {
				console.error('Error fetching stock data:', error);
			});
	}

	onMount(() => {
		const handleKeyDown = (event: KeyboardEvent) => {
			if (event.key === 'Escape') {
				closeDialog();
			}
		};
		window.addEventListener('keydown', handleKeyDown);
		return () => {
			window.removeEventListener('keydown', handleKeyDown);
		};
	});
</script>

<Button onclick={openDialog}>{selectedStock}</Button>

<dialog>
	<form onsubmit={onSearch}>
		<input
			type="search"
			name="search"
			id="search"
			placeholder="Recherche d'actions, ETFs, entreprises"
			value={selectedStock || ''}
		/>
		<button type="submit" class="btn">Rechercher</button>
	</form>
	<div class="results">
		<div class="stocks">
			{#if stocks.length === 0}
				<p>Aucune action, ETFs trouvés.</p>
			{:else}
				<p>{stocks.length} actions, ETFs trouvés</p>
				{#each stocks as stock}
					<button
						class="btn stock"
						onclick={() => {
							selectedStock = stock.symbol;
							closeDialog();
						}}
						disabled={!stock.isYahooFinance}
					>
						<pre>{stock.symbol}</pre>
						<span>{stock.shortname}</span>
						<i>{stock.quoteType}</i>
					</button>
				{/each}
			{/if}
		</div>
		<div class="news">
			{#if news.length === 0}
				<p>Pas de news trouvés.</p>
			{:else}
				<p>{news.length} news trouvées</p>
				{#each news as newsItem}
					<a class="newsItem" href={newsItem.link}>
						{#if newsItem.thumbnail?.resolutions?.length > 0}
							<img
								src={newsItem.thumbnail.resolutions[0].url}
								alt="News Thumbnail"
								width={newsItem.thumbnail.resolutions[0].width}
								height={newsItem.thumbnail.resolutions[0].height}
							/>
						{/if}
						<h1>{newsItem.title}</h1></a
					>
				{/each}
			{/if}
		</div>
	</div>
</dialog>
<!-- svelte-ignore a11y_click_events_have_key_events -->
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div id="backdrop" onclick={closeDialog}></div>

<style>
	dialog {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		width: 300px;
		padding: 20px;
		background-color: var(--bg-primary);
		border: 4px solid #ccc;
		border-radius: 16px;
		box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
		height: 90vh;
		width: 90vw;
		z-index: 1000;
		color: white;
	}

	#backdrop {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 999;
		display: none;
	}

	form {
		display: flex;
		gap: 10px;
	}

	input[type='search'] {
		width: 300px;
	}

	.results {
		display: flex;
		flex-direction: row;
		gap: 20px;
		justify-content: space-evenly;
	}

	.stocks,
	.news {
		display: flex;
		flex-direction: column;
		gap: 10px;
		overflow-y: auto;
		max-height: 70vh;
		padding: 16px;
		flex: 1;
	}

	.stock {
		display: flex;
		align-items: center;
		gap: 8px;
	}

	.stock pre {
		font-weight: bold;
		padding: 8px;
		background: var(--bg-primary);
		border-radius: 8px;
	}

	.stock span {
		font-weight: bold;
		color: var(--text-lime);
	}

	.newsItem {
		display: flex;
		flex-direction: column;
		background-color: var(--bg-secondary);
		padding-bottom: 4px;
		border-radius: 8px;
		transition-duration: 0.2s;
	}

	.newsItem h1 {
		margin: 16px;
	}

	.newsItem img {
		border-radius: 8px;
		width: 100%;
		height: auto;
	}

	.newsItem:hover {
		transform: scale(1.025);
		background-color: #333;
	}
</style>