mj-lab10v2 1.0.0
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/index.js +191 -0
- package/package.json +8 -0
package/index.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
|
|
6
|
+
const files = {
|
|
7
|
+
"App.jsx": `
|
|
8
|
+
import React,{useState,useEffect} from "react";
|
|
9
|
+
import "./App.css";
|
|
10
|
+
|
|
11
|
+
const DataFetcher=()=>{
|
|
12
|
+
const [data,setData]=useState([]);
|
|
13
|
+
const [q,setQ]=useState("");
|
|
14
|
+
const [err,setErr]=useState(null);
|
|
15
|
+
const [load,setLoad]=useState(false);
|
|
16
|
+
|
|
17
|
+
const fetchData=async()=>{
|
|
18
|
+
setLoad(true);
|
|
19
|
+
|
|
20
|
+
try{
|
|
21
|
+
const res=await fetch("https://jsonplaceholder.typicode.com/users");
|
|
22
|
+
|
|
23
|
+
if(!res.ok){
|
|
24
|
+
throw new Error("Fetch failed");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
setData(await res.json());
|
|
28
|
+
setErr(null);
|
|
29
|
+
}
|
|
30
|
+
catch(e){
|
|
31
|
+
setErr(e.message);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
setLoad(false);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
useEffect(()=>{
|
|
38
|
+
fetchData();
|
|
39
|
+
},[]);
|
|
40
|
+
|
|
41
|
+
const filtered=q
|
|
42
|
+
? data.filter(d =>
|
|
43
|
+
d.name.toLowerCase().includes(q.toLowerCase())
|
|
44
|
+
)
|
|
45
|
+
: data;
|
|
46
|
+
|
|
47
|
+
return(
|
|
48
|
+
<div>
|
|
49
|
+
<h1>User Data</h1>
|
|
50
|
+
|
|
51
|
+
{err && <div>Error: {err}</div>}
|
|
52
|
+
|
|
53
|
+
<input
|
|
54
|
+
value={q}
|
|
55
|
+
onChange={e=>setQ(e.target.value)}
|
|
56
|
+
placeholder="Search..."
|
|
57
|
+
/>
|
|
58
|
+
|
|
59
|
+
{load ? (
|
|
60
|
+
<div>Loading...</div>
|
|
61
|
+
) : (
|
|
62
|
+
<table>
|
|
63
|
+
<thead>
|
|
64
|
+
<tr>
|
|
65
|
+
<th>Name</th>
|
|
66
|
+
<th>Email</th>
|
|
67
|
+
<th>City</th>
|
|
68
|
+
</tr>
|
|
69
|
+
</thead>
|
|
70
|
+
|
|
71
|
+
<tbody>
|
|
72
|
+
{filtered.length ? (
|
|
73
|
+
filtered.map(({id,name,email,address})=>(
|
|
74
|
+
<tr key={id}>
|
|
75
|
+
<td>{name}</td>
|
|
76
|
+
<td>{email}</td>
|
|
77
|
+
<td>{address.city}</td>
|
|
78
|
+
</tr>
|
|
79
|
+
))
|
|
80
|
+
) : (
|
|
81
|
+
<tr>
|
|
82
|
+
<td colSpan="3">No results</td>
|
|
83
|
+
</tr>
|
|
84
|
+
)}
|
|
85
|
+
</tbody>
|
|
86
|
+
</table>
|
|
87
|
+
)}
|
|
88
|
+
|
|
89
|
+
<button onClick={fetchData}>
|
|
90
|
+
Refresh
|
|
91
|
+
</button>
|
|
92
|
+
</div>
|
|
93
|
+
);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export default DataFetcher;
|
|
97
|
+
`,
|
|
98
|
+
|
|
99
|
+
"App.css": `
|
|
100
|
+
*{
|
|
101
|
+
padding:0;
|
|
102
|
+
margin:0;
|
|
103
|
+
box-sizing:border-box;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
body{
|
|
107
|
+
font-family:Arial,sans-serif;
|
|
108
|
+
margin:0;
|
|
109
|
+
padding:0;
|
|
110
|
+
background-color:#f4f4f4;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
button{
|
|
114
|
+
border-radius:5px;
|
|
115
|
+
border:none;
|
|
116
|
+
cursor:pointer;
|
|
117
|
+
color:#fff;
|
|
118
|
+
font-weight:bold;
|
|
119
|
+
background:red;
|
|
120
|
+
margin-top:20px;
|
|
121
|
+
padding:10px;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.data-fetcher{
|
|
125
|
+
width:80%;
|
|
126
|
+
margin:0 auto;
|
|
127
|
+
padding:20px;
|
|
128
|
+
background-color:#fff;
|
|
129
|
+
border-radius:8px;
|
|
130
|
+
box-shadow:0 4px 8px rgba(0,0,0,0.1);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
h1{
|
|
134
|
+
text-align:center;
|
|
135
|
+
color:#333;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
.search-bar{
|
|
139
|
+
margin:20px 0;
|
|
140
|
+
text-align:center;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
.search-bar input{
|
|
144
|
+
padding:8px;
|
|
145
|
+
width:60%;
|
|
146
|
+
font-size:16px;
|
|
147
|
+
border:1px solid #000;
|
|
148
|
+
border-radius:4px;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
table{
|
|
152
|
+
width:100%;
|
|
153
|
+
margin-top:20px;
|
|
154
|
+
border-collapse:collapse;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
table th,
|
|
158
|
+
table td{
|
|
159
|
+
padding:10px;
|
|
160
|
+
text-align:left;
|
|
161
|
+
border-bottom:1px solid #ddd;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
.error{
|
|
165
|
+
color:red;
|
|
166
|
+
text-align:center;
|
|
167
|
+
}
|
|
168
|
+
`,
|
|
169
|
+
|
|
170
|
+
"main.jsx": `
|
|
171
|
+
import { StrictMode } from "react";
|
|
172
|
+
import { createRoot } from "react-dom/client";
|
|
173
|
+
import "./index.css";
|
|
174
|
+
import App from "./App.jsx";
|
|
175
|
+
|
|
176
|
+
createRoot(document.getElementById("root")).render(
|
|
177
|
+
<StrictMode>
|
|
178
|
+
<App />
|
|
179
|
+
</StrictMode>
|
|
180
|
+
);
|
|
181
|
+
`
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
for (const [fileName, content] of Object.entries(files)) {
|
|
185
|
+
fs.writeFileSync(
|
|
186
|
+
path.join(process.cwd(), fileName),
|
|
187
|
+
content.trim()
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
console.log(".");
|