File tree Expand file tree Collapse file tree 2 files changed +70
-13
lines changed
Expand file tree Collapse file tree 2 files changed +70
-13
lines changed Original file line number Diff line number Diff line change 1- const button = document . querySelector ( 'button ' )
1+ const button = document . querySelector ( '#load-btn ' )
22
33button . addEventListener ( 'click' , loadUsers )
44
55async function loadUsers ( ) {
6- const module = await import ( './data.json' , { with : { type : 'json' } } )
7- const {
8- default : { users } ,
9- } = module
10- console . log ( 'loading users...' , users )
11- listUsers ( users )
6+ try {
7+ // Show loading state
8+ button . disabled = true
9+ button . textContent = 'Loading...'
10+
11+ const module = await import ( './data.json' , { with : { type : 'json' } } )
12+ const {
13+ default : { users } ,
14+ } = module
15+ console . log ( 'loading users...' , users )
16+ listUsers ( users )
17+ } catch ( error ) {
18+ console . error ( 'Error loading users:' , error )
19+ showError ( 'Failed to load users. Please try again.' )
20+ } finally {
21+ // Reset button state
22+ button . disabled = false
23+ button . textContent = 'Load Users'
24+ }
1225}
1326
1427function listUsers ( users ) {
1528 const div = document . querySelector ( '#users' )
1629 const generatedHTML = `
17- <uL>
18- ${ users . map ( ( user ) => `<li>${ user } </li>` ) . join ( '' ) }
19- </ul>
30+ <div class="grid">
31+ ${ users
32+ . map (
33+ ( user , index ) => `
34+ <article class="card">
35+ <header>
36+ <h4>User ${ index + 1 } </h4>
37+ </header>
38+ <p><strong>Name:</strong> ${ user } </p>
39+ </article>
40+ ` ,
41+ )
42+ . join ( '' ) }
43+ </div>
2044 `
2145 div . innerHTML = generatedHTML
2246}
47+
48+ function showError ( message ) {
49+ const div = document . querySelector ( '#users' )
50+ div . innerHTML = `
51+ <article class="alert">
52+ <h4>Error</h4>
53+ <p>${ message } </p>
54+ </article>
55+ `
56+ }
Original file line number Diff line number Diff line change 44 < meta charset ="UTF-8 " />
55 < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
66 < title > Dynamic Import</ title >
7+ < link
8+ rel ="stylesheet "
9+ href ="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css "
10+ />
711 < script type ="module " src ="app.js "> </ script >
812 </ head >
913 < body >
10- < h1 > Dynamic Import</ h1 >
11- < button > Load Users</ button >
12- < div id ="users "> </ div >
14+ < main class ="container ">
15+ < header class ="container ">
16+ < h1 > Dynamic Import Demo</ h1 >
17+ < p > Demonstrating dynamic import of JSON data with responsive design</ p >
18+ </ header >
19+
20+ < section class ="container ">
21+ < button id ="load-btn "> Load Users</ button >
22+ </ section >
23+
24+ < section class ="container ">
25+ < div id ="users ">
26+ < article >
27+ < h3 > Users will appear here</ h3 >
28+ < p >
29+ Click the "Load Users" button above to fetch and display the user
30+ list.
31+ </ p >
32+ </ article >
33+ </ div >
34+ </ section >
35+ </ main >
1336 </ body >
1437</ html >
You can’t perform that action at this time.
0 commit comments