nuclo 0.1.36 → 0.1.37
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 +17 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -203,17 +203,19 @@ 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: [] };
|
|
209
210
|
|
|
210
|
-
async function
|
|
211
|
+
async function fetchProducts() {
|
|
211
212
|
state.status = 'loading';
|
|
212
213
|
update();
|
|
213
214
|
|
|
214
215
|
try {
|
|
215
|
-
const response = await fetch('/
|
|
216
|
-
|
|
216
|
+
const response = await fetch('https://dummyjson.com/products/search?q=phone');
|
|
217
|
+
const data = await response.json();
|
|
218
|
+
state.products = data.products;
|
|
217
219
|
state.status = 'idle';
|
|
218
220
|
} catch (err) {
|
|
219
221
|
state.status = 'error';
|
|
@@ -223,16 +225,22 @@ async function fetchData() {
|
|
|
223
225
|
}
|
|
224
226
|
|
|
225
227
|
const app = div(
|
|
226
|
-
button('Load
|
|
228
|
+
button('Load Products', on('click', fetchProducts)),
|
|
227
229
|
|
|
228
230
|
when(() => state.status === 'loading',
|
|
229
231
|
div('Loading...')
|
|
230
232
|
).when(() => state.status === 'error',
|
|
231
233
|
div({ className: 'error' }, () => `Error: ${state.error}`)
|
|
232
|
-
).when(() => state.
|
|
233
|
-
list(() => state.
|
|
234
|
+
).when(() => state.products.length > 0,
|
|
235
|
+
list(() => state.products, product =>
|
|
236
|
+
div(
|
|
237
|
+
{ className: 'product-card' },
|
|
238
|
+
h3(product.title),
|
|
239
|
+
p(() => `Category: ${product.category}`)
|
|
240
|
+
)
|
|
241
|
+
)
|
|
234
242
|
).else(
|
|
235
|
-
div('No
|
|
243
|
+
div('No products loaded')
|
|
236
244
|
)
|
|
237
245
|
);
|
|
238
246
|
|