create-bdpa-react-scaffold 1.4.2 → 1.7.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/HOWTO.md ADDED
@@ -0,0 +1,166 @@
1
+ # How-To Guides
2
+
3
+ ## How to Add a New Page
4
+
5
+ 1. **Create the page component** in `src/pages/`:
6
+
7
+ ```jsx
8
+ // src/pages/Dashboard.jsx
9
+ import React from "react";
10
+ import Container from "../components/layout/Container";
11
+ import Card from "../components/ui/Card";
12
+
13
+ export default function Dashboard() {
14
+ return (
15
+ <Container>
16
+ <h1 className="text-3xl font-bold mb-6">Dashboard</h1>
17
+ <Card>
18
+ <p>Welcome to your dashboard!</p>
19
+ </Card>
20
+ </Container>
21
+ );
22
+ }
23
+ ```
24
+
25
+ 2. **Add the route** in `src/App.jsx`:
26
+
27
+ ```jsx
28
+ import { BrowserRouter, Routes, Route } from "react-router-dom";
29
+ import Dashboard from "./pages/Dashboard";
30
+ // ... other imports
31
+
32
+ function App() {
33
+ return (
34
+ <BrowserRouter>
35
+ <Routes>
36
+ <Route path="/" element={<Home />} />
37
+ <Route path="/dashboard" element={<Dashboard />} />
38
+ {/* Add your new route here */}
39
+ </Routes>
40
+ </BrowserRouter>
41
+ );
42
+ }
43
+ ```
44
+
45
+ 3. **Add navigation link** (optional) in `src/components/ui/Navbar.jsx` or `Sidebar.jsx`:
46
+
47
+ ```jsx
48
+ import { Link } from "react-router-dom";
49
+
50
+ <Link to="/dashboard" className="text-gray-700 hover:text-blue-600">
51
+ Dashboard
52
+ </Link>
53
+ ```
54
+
55
+ ## How to Make a REST API Call
56
+
57
+ The scaffold includes an Axios-based API client in `src/utils/api.js` with built-in CRUD methods.
58
+
59
+ ### Basic API Configuration
60
+
61
+ Set your API base URL in a `.env` file:
62
+
63
+ ```bash
64
+ VITE_API_BASE_URL=https://api.example.com
65
+ ```
66
+
67
+ ### Making API Calls
68
+
69
+ ```jsx
70
+ import { useState, useEffect } from "react";
71
+ import api from "../utils/api";
72
+
73
+ function MyComponent() {
74
+ const [data, setData] = useState([]);
75
+ const [loading, setLoading] = useState(false);
76
+ const [error, setError] = useState(null);
77
+
78
+ // GET request - Fetch all items
79
+ useEffect(() => {
80
+ const fetchData = async () => {
81
+ setLoading(true);
82
+ try {
83
+ const response = await api.getAll("/students");
84
+ setData(response.data);
85
+ } catch (err) {
86
+ setError(err.message);
87
+ } finally {
88
+ setLoading(false);
89
+ }
90
+ };
91
+ fetchData();
92
+ }, []);
93
+
94
+ // GET request - Fetch single item
95
+ const fetchStudent = async (id) => {
96
+ try {
97
+ const response = await api.getById("/students", id);
98
+ console.log(response.data);
99
+ } catch (err) {
100
+ console.error(err);
101
+ }
102
+ };
103
+
104
+ // POST request - Create new item
105
+ const createStudent = async () => {
106
+ try {
107
+ const newStudent = { name: "Ada Lovelace", grade: "A" };
108
+ const response = await api.create("/students", newStudent);
109
+ setData([...data, response.data]);
110
+ } catch (err) {
111
+ setError(err.message);
112
+ }
113
+ };
114
+
115
+ // PUT request - Update item
116
+ const updateStudent = async (id) => {
117
+ try {
118
+ const updates = { name: "Grace Hopper" };
119
+ const response = await api.update(`/students/${id}`, updates);
120
+ setData(data.map(item => item.id === id ? response.data : item));
121
+ } catch (err) {
122
+ setError(err.message);
123
+ }
124
+ };
125
+
126
+ // DELETE request - Remove item
127
+ const deleteStudent = async (id) => {
128
+ try {
129
+ await api.delete(`/students/${id}`);
130
+ setData(data.filter(item => item.id !== id));
131
+ } catch (err) {
132
+ setError(err.message);
133
+ }
134
+ };
135
+
136
+ return (
137
+ <div>
138
+ {loading && <p>Loading...</p>}
139
+ {error && <p className="text-red-500">Error: {error}</p>}
140
+ {/* Render your data here */}
141
+ </div>
142
+ );
143
+ }
144
+ ```
145
+
146
+ ### Available API Methods
147
+
148
+ - `api.getAll(endpoint)` - GET all items
149
+ - `api.getById(endpoint, id)` - GET single item
150
+ - `api.create(endpoint, data)` - POST new item
151
+ - `api.update(endpoint, data)` - PUT update item
152
+ - `api.delete(endpoint)` - DELETE item
153
+ - `api.setToken(token)` - Set authorization token
154
+
155
+ ### Custom API Client
156
+
157
+ Create a custom client for different base URLs:
158
+
159
+ ```js
160
+ import { ApiClient } from "./utils/api";
161
+
162
+ const adminApi = new ApiClient("https://admin.example.com");
163
+ adminApi.setToken("your-jwt-token");
164
+
165
+ const response = await adminApi.getAll("/users");
166
+ ```
package/README.md CHANGED
@@ -82,25 +82,9 @@ After publishing, users can run:
82
82
  npx create-bdpa-react-scaffold my-app
83
83
  ```
84
84
 
85
- ## API (Axios)
85
+ ## Documentation
86
86
 
87
- - Base URL: set `VITE_API_BASE_URL` in a `.env` file or your environment.
88
- - Default export `api` is a preconfigured instance; `ApiClient` class is also exported.
89
-
90
- Example:
91
-
92
- ```js
93
- import api, { ApiClient } from "./src/utils/api";
94
-
95
- // Using default instance
96
- await api.create("/students", { name: "Ada" });
97
- const { data } = await api.getAll("/students");
98
-
99
- // Or create your own client
100
- const client = new ApiClient("https://api.example.com");
101
- client.setToken("<jwt>");
102
- await client.update("/students/1", { name: "Grace" });
103
- ```
87
+ - [How-To Guides](HOWTO.md) - Learn how to add pages and make API calls
104
88
 
105
89
  ## License
106
90