nuclo 0.1.36 → 0.1.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -203,17 +203,22 @@ render(app, document.body);
|
|
|
203
203
|
```ts
|
|
204
204
|
import 'nuclo';
|
|
205
205
|
|
|
206
|
-
type
|
|
206
|
+
type Product = { id: number; title: string; category: string };
|
|
207
|
+
type State = { status: 'idle' | 'loading' | 'error'; products: Product[]; error?: string };
|
|
207
208
|
|
|
208
|
-
let state: State = { status: 'idle',
|
|
209
|
+
let state: State = { status: 'idle', products: [] };
|
|
210
|
+
let searchQuery = 'phone';
|
|
211
|
+
|
|
212
|
+
async function fetchProducts() {
|
|
213
|
+
if (!searchQuery.trim()) return;
|
|
209
214
|
|
|
210
|
-
async function fetchData() {
|
|
211
215
|
state.status = 'loading';
|
|
212
216
|
update();
|
|
213
217
|
|
|
214
218
|
try {
|
|
215
|
-
const response = await fetch(
|
|
216
|
-
|
|
219
|
+
const response = await fetch(`https://dummyjson.com/products/search?q=${searchQuery}`);
|
|
220
|
+
const data = await response.json();
|
|
221
|
+
state.products = data.products;
|
|
217
222
|
state.status = 'idle';
|
|
218
223
|
} catch (err) {
|
|
219
224
|
state.status = 'error';
|
|
@@ -223,16 +228,36 @@ async function fetchData() {
|
|
|
223
228
|
}
|
|
224
229
|
|
|
225
230
|
const app = div(
|
|
226
|
-
|
|
231
|
+
div(
|
|
232
|
+
input(
|
|
233
|
+
{
|
|
234
|
+
type: 'search',
|
|
235
|
+
placeholder: 'Search products...',
|
|
236
|
+
value: () => searchQuery
|
|
237
|
+
},
|
|
238
|
+
on('input', e => {
|
|
239
|
+
searchQuery = e.target.value;
|
|
240
|
+
update();
|
|
241
|
+
}),
|
|
242
|
+
on('keydown', e => e.key === 'Enter' && fetchProducts())
|
|
243
|
+
),
|
|
244
|
+
button('Search', on('click', fetchProducts))
|
|
245
|
+
),
|
|
227
246
|
|
|
228
247
|
when(() => state.status === 'loading',
|
|
229
248
|
div('Loading...')
|
|
230
249
|
).when(() => state.status === 'error',
|
|
231
250
|
div({ className: 'error' }, () => `Error: ${state.error}`)
|
|
232
|
-
).when(() => state.
|
|
233
|
-
list(() => state.
|
|
251
|
+
).when(() => state.products.length > 0,
|
|
252
|
+
list(() => state.products, product =>
|
|
253
|
+
div(
|
|
254
|
+
{ className: 'product-card' },
|
|
255
|
+
h3(product.title),
|
|
256
|
+
p(() => `Category: ${product.category}`)
|
|
257
|
+
)
|
|
258
|
+
)
|
|
234
259
|
).else(
|
|
235
|
-
div('
|
|
260
|
+
div('Click search to load products')
|
|
236
261
|
)
|
|
237
262
|
);
|
|
238
263
|
|