create-aziz-app 1.0.3 → 2.0.2
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/bin/create-aziz-app.js +39 -20
- package/package.json +3 -1
- package/template/README.md +189 -0
- package/template/api/hello.js +14 -0
- package/template/api/users.js +62 -0
- package/template/app.config.js +41 -0
- package/template/app.html +127 -24
- package/template/components/Counter.jsx +30 -12
- package/template/components/Footer.jsx +3 -3
- package/template/components/Navbar.jsx +16 -0
- package/template/docs/API-GUIDE.md +196 -0
- package/template/docs/AUTO-IMPORT.md +99 -0
- package/template/docs/GETTING-STARTED.md +205 -0
- package/template/docs/QUICK-START.md +111 -0
- package/template/modules/README.md +129 -0
- package/template/modules/api.js +148 -0
- package/template/modules/auto-import.js +130 -0
- package/template/modules/router.js +185 -0
- package/template/modules/state.js +54 -0
- package/template/package.json +18 -0
- package/template/pages/About.jsx +33 -0
- package/template/pages/ApiDemo.jsx +209 -0
- package/template/pages/Contact.jsx +27 -0
- package/template/pages/Home.jsx +39 -0
- package/template/pages/NewPage.jsx +39 -0
- package/template/pages/Test.jsx +74 -0
- package/template/scripts/build.js +81 -0
- package/template/scripts/dev-server.js +172 -0
- package/template/scripts/generate-imports.js +98 -0
- package/template/app.js +0 -83
- package/template/components/Header.jsx +0 -10
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# API Routes Guide
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Your app now has a Next.js-like API route system! Create API endpoints in the `api/` folder and call them from your components.
|
|
6
|
+
|
|
7
|
+
## Creating API Routes
|
|
8
|
+
|
|
9
|
+
### Basic Example
|
|
10
|
+
|
|
11
|
+
Create a file in `api/` folder:
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
// api/myroute.js
|
|
15
|
+
registerApiRoute('myroute', async (req, res) => {
|
|
16
|
+
return res.status(200).json({
|
|
17
|
+
message: 'Hello from API!'
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Request Object (req)
|
|
23
|
+
|
|
24
|
+
The request object contains:
|
|
25
|
+
- `req.method` - HTTP method (GET, POST, PUT, DELETE)
|
|
26
|
+
- `req.body` - Request body (parsed JSON)
|
|
27
|
+
- `req.query` - Query parameters from URL
|
|
28
|
+
- `req.params` - Route parameters
|
|
29
|
+
|
|
30
|
+
### Response Object (res)
|
|
31
|
+
|
|
32
|
+
The response object has methods:
|
|
33
|
+
- `res.status(code)` - Set HTTP status code
|
|
34
|
+
- `res.json(data)` - Send JSON response
|
|
35
|
+
- `res.send(data)` - Send plain response
|
|
36
|
+
|
|
37
|
+
### Full CRUD Example
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
// api/todos.js
|
|
41
|
+
const todos = [];
|
|
42
|
+
|
|
43
|
+
registerApiRoute('todos', async (req, res) => {
|
|
44
|
+
switch (req.method) {
|
|
45
|
+
case 'GET':
|
|
46
|
+
// Get all todos
|
|
47
|
+
return res.status(200).json(todos);
|
|
48
|
+
|
|
49
|
+
case 'POST':
|
|
50
|
+
// Create new todo
|
|
51
|
+
const newTodo = {
|
|
52
|
+
id: Date.now(),
|
|
53
|
+
title: req.body.title,
|
|
54
|
+
completed: false
|
|
55
|
+
};
|
|
56
|
+
todos.push(newTodo);
|
|
57
|
+
return res.status(201).json(newTodo);
|
|
58
|
+
|
|
59
|
+
case 'PUT':
|
|
60
|
+
// Update todo
|
|
61
|
+
const id = req.body.id;
|
|
62
|
+
const index = todos.findIndex(t => t.id === id);
|
|
63
|
+
if (index !== -1) {
|
|
64
|
+
todos[index] = { ...todos[index], ...req.body };
|
|
65
|
+
return res.status(200).json(todos[index]);
|
|
66
|
+
}
|
|
67
|
+
return res.status(404).json({ error: 'Not found' });
|
|
68
|
+
|
|
69
|
+
case 'DELETE':
|
|
70
|
+
// Delete todo
|
|
71
|
+
const deleteId = req.query.id;
|
|
72
|
+
const deleteIndex = todos.findIndex(t => t.id === parseInt(deleteId));
|
|
73
|
+
if (deleteIndex !== -1) {
|
|
74
|
+
todos.splice(deleteIndex, 1);
|
|
75
|
+
return res.status(200).json({ message: 'Deleted' });
|
|
76
|
+
}
|
|
77
|
+
return res.status(404).json({ error: 'Not found' });
|
|
78
|
+
|
|
79
|
+
default:
|
|
80
|
+
return res.status(405).json({ error: 'Method not allowed' });
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Using API Routes in Components
|
|
86
|
+
|
|
87
|
+
### Basic GET Request
|
|
88
|
+
|
|
89
|
+
```javascript
|
|
90
|
+
const MyComponent = () => {
|
|
91
|
+
const [data, setData] = useState(null);
|
|
92
|
+
|
|
93
|
+
const loadData = async () => {
|
|
94
|
+
const response = await fetchApi('/api/myroute');
|
|
95
|
+
const result = await response.json();
|
|
96
|
+
setData(result);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<div>
|
|
101
|
+
<button onClick={loadData}>Load Data</button>
|
|
102
|
+
{data && <p>{data.message}</p>}
|
|
103
|
+
</div>
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### POST Request
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
const addItem = async () => {
|
|
112
|
+
const response = await fetchApi('/api/todos', {
|
|
113
|
+
method: 'POST',
|
|
114
|
+
body: JSON.stringify({
|
|
115
|
+
title: 'New Todo'
|
|
116
|
+
})
|
|
117
|
+
});
|
|
118
|
+
const result = await response.json();
|
|
119
|
+
console.log('Created:', result);
|
|
120
|
+
};
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Query Parameters
|
|
124
|
+
|
|
125
|
+
```javascript
|
|
126
|
+
// Fetch with query params
|
|
127
|
+
const response = await fetchApi('/api/users?id=123&name=John');
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### DELETE Request
|
|
131
|
+
|
|
132
|
+
```javascript
|
|
133
|
+
const deleteItem = async (id) => {
|
|
134
|
+
const response = await fetchApi(`/api/todos?id=${id}`, {
|
|
135
|
+
method: 'DELETE'
|
|
136
|
+
});
|
|
137
|
+
const result = await response.json();
|
|
138
|
+
console.log(result.message);
|
|
139
|
+
};
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Available API Routes
|
|
143
|
+
|
|
144
|
+
### /api/hello
|
|
145
|
+
Simple hello world endpoint
|
|
146
|
+
- **GET** `/api/hello?name=YourName`
|
|
147
|
+
- Returns: `{ message: "Hello, YourName!", timestamp: "...", method: "GET" }`
|
|
148
|
+
|
|
149
|
+
### /api/users
|
|
150
|
+
User management endpoint
|
|
151
|
+
- **GET** `/api/users` - Get all users
|
|
152
|
+
- **GET** `/api/users?id=1` - Get specific user
|
|
153
|
+
- **POST** `/api/users` - Create user (body: `{ name, email }`)
|
|
154
|
+
- **PUT** `/api/users?id=1` - Update user (body: `{ name, email }`)
|
|
155
|
+
- **DELETE** `/api/users?id=1` - Delete user
|
|
156
|
+
|
|
157
|
+
## Tips
|
|
158
|
+
|
|
159
|
+
1. **Mock Data**: Currently, API routes use in-memory data. Replace with real backend calls when ready.
|
|
160
|
+
|
|
161
|
+
2. **Error Handling**: Always wrap API calls in try-catch:
|
|
162
|
+
```javascript
|
|
163
|
+
try {
|
|
164
|
+
const response = await fetchApi('/api/myroute');
|
|
165
|
+
const data = await response.json();
|
|
166
|
+
} catch (error) {
|
|
167
|
+
console.error('API Error:', error);
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
3. **Loading States**: Use loading state for better UX:
|
|
172
|
+
```javascript
|
|
173
|
+
const [loading, setLoading] = useState(false);
|
|
174
|
+
|
|
175
|
+
const loadData = async () => {
|
|
176
|
+
setLoading(true);
|
|
177
|
+
try {
|
|
178
|
+
const response = await fetchApi('/api/data');
|
|
179
|
+
const data = await response.json();
|
|
180
|
+
setData(data);
|
|
181
|
+
} finally {
|
|
182
|
+
setLoading(false);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
4. **Real Backend**: To connect to a real backend, modify `fetchApi` in `api.js` to use `fetch()` instead of the mock system.
|
|
188
|
+
|
|
189
|
+
## Demo
|
|
190
|
+
|
|
191
|
+
Visit `#/api-demo` in your app to see a live demo of the API system with:
|
|
192
|
+
- GET requests
|
|
193
|
+
- POST requests (create users)
|
|
194
|
+
- DELETE requests
|
|
195
|
+
- Loading states
|
|
196
|
+
- Error handling
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# 🚀 Auto-Import System
|
|
2
|
+
|
|
3
|
+
## How It Works
|
|
4
|
+
|
|
5
|
+
Instead of manually adding `<script>` tags, just run one command and all your files are automatically imported!
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
### 1. Create Your Files
|
|
10
|
+
|
|
11
|
+
Just create files in the appropriate folders:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
pages/MyNewPage.jsx ← Automatically becomes a route
|
|
15
|
+
components/Button.jsx ← Automatically available
|
|
16
|
+
api/todos.js ← Automatically registered
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### 2. Run the Build Command
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
cd template
|
|
23
|
+
node generate-imports.js
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Or from the root:
|
|
27
|
+
```bash
|
|
28
|
+
npm run build
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### 3. Refresh Your Browser
|
|
32
|
+
|
|
33
|
+
That's it! Your new files are now loaded.
|
|
34
|
+
|
|
35
|
+
## What Gets Auto-Imported
|
|
36
|
+
|
|
37
|
+
- **`api/*.js`** - All API route files
|
|
38
|
+
- **`components/*.jsx`** and **`components/*.js`** - All component files
|
|
39
|
+
- **`pages/*.jsx`** and **`pages/*.js`** - All page files
|
|
40
|
+
|
|
41
|
+
## Example Workflow
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
# 1. Create a new page
|
|
45
|
+
echo '/** @jsx h */
|
|
46
|
+
registerPage("profile", ProfilePage);
|
|
47
|
+
function ProfilePage() {
|
|
48
|
+
return <div><h1>Profile</h1></div>;
|
|
49
|
+
}' > pages/Profile.jsx
|
|
50
|
+
|
|
51
|
+
# 2. Run the generator
|
|
52
|
+
node generate-imports.js
|
|
53
|
+
|
|
54
|
+
# 3. Refresh browser and navigate to #/profile
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## How It Works
|
|
58
|
+
|
|
59
|
+
The `generate-imports.js` script:
|
|
60
|
+
1. Scans the `api/`, `components/`, and `pages/` folders
|
|
61
|
+
2. Finds all `.js` and `.jsx` files
|
|
62
|
+
3. Automatically updates `app.html` with the correct `<script>` tags
|
|
63
|
+
4. Maintains the correct loading order
|
|
64
|
+
|
|
65
|
+
## Benefits
|
|
66
|
+
|
|
67
|
+
✅ **No manual script tags** - Just create files and run the command
|
|
68
|
+
✅ **Automatic discovery** - Scans folders for you
|
|
69
|
+
✅ **Correct order** - Loads modules → API → components → pages
|
|
70
|
+
✅ **Simple** - One command does everything
|
|
71
|
+
|
|
72
|
+
## Tips
|
|
73
|
+
|
|
74
|
+
- Run `node generate-imports.js` after creating/deleting files
|
|
75
|
+
- The script is safe to run multiple times
|
|
76
|
+
- It preserves your HTML structure
|
|
77
|
+
- Works with both `.js` and `.jsx` files
|
|
78
|
+
|
|
79
|
+
## Troubleshooting
|
|
80
|
+
|
|
81
|
+
**Script not found?**
|
|
82
|
+
```bash
|
|
83
|
+
cd template
|
|
84
|
+
node generate-imports.js
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
**New page not showing?**
|
|
88
|
+
1. Make sure you ran `node generate-imports.js`
|
|
89
|
+
2. Check that your file is in the `pages/` folder
|
|
90
|
+
3. Refresh your browser (hard refresh: Ctrl+Shift+R)
|
|
91
|
+
|
|
92
|
+
**Want to watch for changes automatically?**
|
|
93
|
+
Install nodemon:
|
|
94
|
+
```bash
|
|
95
|
+
npm install -g nodemon
|
|
96
|
+
nodemon --watch pages --watch components --watch api --exec "node generate-imports.js"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Now every time you create/modify a file, it auto-regenerates!
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# Getting Started
|
|
2
|
+
|
|
3
|
+
## 🎉 Your App is Now Super Clean!
|
|
4
|
+
|
|
5
|
+
The app now uses an **auto-import system** - you only need to edit one file to add new pages, components, or API routes!
|
|
6
|
+
|
|
7
|
+
## 📁 Project Structure
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
template/
|
|
11
|
+
├── app.html # Main HTML (super clean - only 5 script tags!)
|
|
12
|
+
├── app.config.js # ⭐ Configure what files to load here
|
|
13
|
+
├── modules/ # Core system modules
|
|
14
|
+
│ ├── router.js # Routing & rendering
|
|
15
|
+
│ ├── state.js # State management
|
|
16
|
+
│ ├── api.js # API system
|
|
17
|
+
│ └── auto-import.js # Auto-import magic
|
|
18
|
+
├── api/ # API route handlers
|
|
19
|
+
│ ├── hello.js
|
|
20
|
+
│ └── users.js
|
|
21
|
+
├── components/ # Reusable components
|
|
22
|
+
│ ├── Navbar.jsx
|
|
23
|
+
│ ├── Footer.jsx
|
|
24
|
+
│ └── Counter.jsx
|
|
25
|
+
└── pages/ # Page components
|
|
26
|
+
├── Home.jsx
|
|
27
|
+
├── About.jsx
|
|
28
|
+
├── Contact.jsx
|
|
29
|
+
├── test.jsx
|
|
30
|
+
└── ApiDemo.jsx
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## 🚀 Adding New Files
|
|
34
|
+
|
|
35
|
+
### Add a New Page
|
|
36
|
+
|
|
37
|
+
1. **Create the page file:**
|
|
38
|
+
```javascript
|
|
39
|
+
// pages/MyNewPage.jsx
|
|
40
|
+
/** @jsx h */
|
|
41
|
+
|
|
42
|
+
registerPage("mynewpage", MyNewPage);
|
|
43
|
+
|
|
44
|
+
function MyNewPage() {
|
|
45
|
+
const [count, setCount] = useState(0);
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<div>
|
|
49
|
+
<h1>My New Page</h1>
|
|
50
|
+
<p>Count: {count}</p>
|
|
51
|
+
<button onClick={() => setCount(count + 1)}>
|
|
52
|
+
Increment
|
|
53
|
+
</button>
|
|
54
|
+
</div>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
2. **Add to config:**
|
|
60
|
+
```javascript
|
|
61
|
+
// app.config.js
|
|
62
|
+
window.APP_CONFIG = {
|
|
63
|
+
pages: [
|
|
64
|
+
'pages/Home.jsx',
|
|
65
|
+
'pages/About.jsx',
|
|
66
|
+
'pages/MyNewPage.jsx' // ← Add this line
|
|
67
|
+
]
|
|
68
|
+
};
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
3. **That's it!** Refresh the page and navigate to `#/mynewpage`
|
|
72
|
+
|
|
73
|
+
### Add a New Component
|
|
74
|
+
|
|
75
|
+
1. **Create the component:**
|
|
76
|
+
```javascript
|
|
77
|
+
// components/Button.jsx
|
|
78
|
+
/** @jsx h */
|
|
79
|
+
|
|
80
|
+
function Button({ label, onClick }) {
|
|
81
|
+
return (
|
|
82
|
+
<button class="btn btn-primary" onClick={onClick}>
|
|
83
|
+
{label}
|
|
84
|
+
</button>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
2. **Add to config:**
|
|
90
|
+
```javascript
|
|
91
|
+
// app.config.js
|
|
92
|
+
window.APP_CONFIG = {
|
|
93
|
+
components: [
|
|
94
|
+
'components/Navbar.jsx',
|
|
95
|
+
'components/Button.jsx' // ← Add this line
|
|
96
|
+
]
|
|
97
|
+
};
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
3. **Use it in pages:**
|
|
101
|
+
```javascript
|
|
102
|
+
<Button label="Click me" onClick={() => alert('Clicked!')} />
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Add a New API Route
|
|
106
|
+
|
|
107
|
+
1. **Create the API route:**
|
|
108
|
+
```javascript
|
|
109
|
+
// api/todos.js
|
|
110
|
+
/** @jsx h */
|
|
111
|
+
|
|
112
|
+
const todos = [];
|
|
113
|
+
|
|
114
|
+
registerApiRoute('todos', async (req, res) => {
|
|
115
|
+
if (req.method === 'GET') {
|
|
116
|
+
return res.status(200).json(todos);
|
|
117
|
+
}
|
|
118
|
+
if (req.method === 'POST') {
|
|
119
|
+
const newTodo = { id: Date.now(), ...req.body };
|
|
120
|
+
todos.push(newTodo);
|
|
121
|
+
return res.status(201).json(newTodo);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
2. **Add to config:**
|
|
127
|
+
```javascript
|
|
128
|
+
// app.config.js
|
|
129
|
+
window.APP_CONFIG = {
|
|
130
|
+
apiRoutes: [
|
|
131
|
+
'api/hello.js',
|
|
132
|
+
'api/todos.js' // ← Add this line
|
|
133
|
+
]
|
|
134
|
+
};
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
3. **Use it:**
|
|
138
|
+
```javascript
|
|
139
|
+
const response = await fetchApi('/api/todos');
|
|
140
|
+
const data = await response.json();
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## 🎨 Key Features
|
|
144
|
+
|
|
145
|
+
### State Management (React-like)
|
|
146
|
+
```javascript
|
|
147
|
+
const [count, setCount] = useState(0);
|
|
148
|
+
const [name, setName] = useState('');
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Routing
|
|
152
|
+
```javascript
|
|
153
|
+
// Register a page
|
|
154
|
+
registerPage("about", AboutPage);
|
|
155
|
+
|
|
156
|
+
// Navigate
|
|
157
|
+
navigate("about");
|
|
158
|
+
|
|
159
|
+
// Get current path
|
|
160
|
+
const path = getCurrentPath();
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### API Routes
|
|
164
|
+
```javascript
|
|
165
|
+
// Register
|
|
166
|
+
registerApiRoute('users', async (req, res) => {
|
|
167
|
+
return res.status(200).json({ users: [] });
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Call
|
|
171
|
+
const response = await fetchApi('/api/users');
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## 📝 Important Notes
|
|
175
|
+
|
|
176
|
+
1. **Always use `/** @jsx h */`** at the top of JSX files
|
|
177
|
+
2. **Load order matters** - config → modules → auto-import
|
|
178
|
+
3. **Check console** - auto-import logs what it's loading
|
|
179
|
+
4. **One config file** - `app.config.js` is the only place to add files
|
|
180
|
+
|
|
181
|
+
## 🔧 Troubleshooting
|
|
182
|
+
|
|
183
|
+
**Page not loading?**
|
|
184
|
+
- Check if it's added to `app.config.js`
|
|
185
|
+
- Check browser console for errors
|
|
186
|
+
- Make sure file path is correct
|
|
187
|
+
|
|
188
|
+
**State not working?**
|
|
189
|
+
- Make sure you're using `useState()` not `window.useState()`
|
|
190
|
+
- Check that `modules/state.js` is loaded
|
|
191
|
+
|
|
192
|
+
**API route not found?**
|
|
193
|
+
- Check if it's added to `app.config.js`
|
|
194
|
+
- Make sure you used `registerApiRoute()`
|
|
195
|
+
- Check the route path matches your `fetchApi()` call
|
|
196
|
+
|
|
197
|
+
## 🎯 Next Steps
|
|
198
|
+
|
|
199
|
+
1. Open `app.html` in your browser
|
|
200
|
+
2. Check the console to see auto-import in action
|
|
201
|
+
3. Navigate to different pages
|
|
202
|
+
4. Try the API Demo page at `#/api-demo`
|
|
203
|
+
5. Create your own pages and components!
|
|
204
|
+
|
|
205
|
+
Enjoy your clean, auto-importing app! 🚀
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Quick Start Guide
|
|
2
|
+
|
|
3
|
+
## Adding a New Page
|
|
4
|
+
|
|
5
|
+
1. Create `pages/NewPage.jsx`:
|
|
6
|
+
```javascript
|
|
7
|
+
/** @jsx h */
|
|
8
|
+
|
|
9
|
+
// Register this page
|
|
10
|
+
registerPage("newpage", NewPage);
|
|
11
|
+
|
|
12
|
+
function NewPage() {
|
|
13
|
+
// Use state management
|
|
14
|
+
const [counter, setCounter] = window.StateManager.useState(0);
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
<div>
|
|
18
|
+
<h1>New Page</h1>
|
|
19
|
+
<p>Counter: {counter}</p>
|
|
20
|
+
<button onClick={() => setCounter(counter + 1)}>
|
|
21
|
+
Increment
|
|
22
|
+
</button>
|
|
23
|
+
</div>
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
2. That's it! The page will be automatically loaded and available at `#/newpage`
|
|
29
|
+
|
|
30
|
+
## Adding a New Component
|
|
31
|
+
|
|
32
|
+
1. Create `components/Button.jsx`:
|
|
33
|
+
```javascript
|
|
34
|
+
/** @jsx h */
|
|
35
|
+
|
|
36
|
+
// Register this component
|
|
37
|
+
if (window.AutoImport) {
|
|
38
|
+
window.AutoImport.registerComponent('Button', Button);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function Button({ label, onClick, variant = 'primary' }) {
|
|
42
|
+
return (
|
|
43
|
+
<button
|
|
44
|
+
class={`btn btn-${variant}`}
|
|
45
|
+
onClick={onClick}
|
|
46
|
+
>
|
|
47
|
+
{label}
|
|
48
|
+
</button>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
2. Use it in any page:
|
|
54
|
+
```javascript
|
|
55
|
+
// In your page component
|
|
56
|
+
const Button = window.AutoImport.getComponent('Button');
|
|
57
|
+
return <Button label="Click me" onClick={() => alert('Clicked!')} />;
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## State Management Examples
|
|
61
|
+
|
|
62
|
+
### Basic Counter
|
|
63
|
+
```javascript
|
|
64
|
+
const [count, setCount] = window.StateManager.useState(0);
|
|
65
|
+
return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Form with Multiple Fields
|
|
69
|
+
```javascript
|
|
70
|
+
const [form, setForm] = window.StateManager.useState({
|
|
71
|
+
name: '',
|
|
72
|
+
email: '',
|
|
73
|
+
message: ''
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const handleChange = (field, value) => {
|
|
77
|
+
setForm({ ...form, [field]: value });
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<form>
|
|
82
|
+
<input
|
|
83
|
+
value={form.name}
|
|
84
|
+
onChange={(e) => handleChange('name', e.target.value)}
|
|
85
|
+
/>
|
|
86
|
+
{/* ... other fields */}
|
|
87
|
+
</form>
|
|
88
|
+
);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Global State
|
|
92
|
+
```javascript
|
|
93
|
+
// Set global state
|
|
94
|
+
window.StateManager.setState({ theme: 'dark', user: 'John' });
|
|
95
|
+
|
|
96
|
+
// Get global state
|
|
97
|
+
const { theme, user } = window.StateManager.getState();
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Tips
|
|
101
|
+
|
|
102
|
+
1. **State is reactive**: When state changes, components automatically re-render
|
|
103
|
+
2. **No imports needed**: Everything is available via `window.StateManager` and `window.AutoImport`
|
|
104
|
+
3. **Isolation**: Each component's `useState` is isolated to that component
|
|
105
|
+
4. **Global vs Local**: Use `window.StateManager.setState()` for global state, `useState()` for local component state
|
|
106
|
+
|
|
107
|
+
## Testing
|
|
108
|
+
|
|
109
|
+
Open `app.html` and navigate to:
|
|
110
|
+
- `#/` (Home) - Shows global state, local state, and reusable components
|
|
111
|
+
- `#/test` - Comprehensive form and state examples
|