1
1
import { useEffect , useState , useRef } from "react" ;
2
+ import { CiTrash } from "react-icons/ci" ;
2
3
import Head from "next/head" ;
3
4
import Image from "next/image" ;
4
5
import styles from "@/styles/Home.module.scss" ;
@@ -16,7 +17,7 @@ export default function Home() {
16
17
const [ deletedError , setDeletedError ] = useState ( false ) ;
17
18
18
19
async function addProduct ( ) {
19
- const productName = productNameRef . current . value ;
20
+ const productName = productNameRef . current . value . trim ( ) ;
20
21
if ( productName . length < 3 ) return ;
21
22
const postData = {
22
23
method : "POST" ,
@@ -62,16 +63,15 @@ export default function Home() {
62
63
console . log ( response ) ;
63
64
}
64
65
65
- async function deleteProduct ( ) {
66
- const productIDToDelete = productIDToDeleteRef . current . value ;
67
- if ( ! productIDToDelete . length ) return ;
66
+ async function deleteProduct ( id ) {
67
+ if ( ! id ) return ;
68
68
const postData = {
69
69
method : "DELETE" ,
70
70
headers : {
71
71
"Content-Type" : "application/json" ,
72
72
} ,
73
73
body : JSON . stringify ( {
74
- product_id : productIDToDelete ,
74
+ product_id : id ,
75
75
} ) ,
76
76
} ;
77
77
const res = await fetch (
@@ -87,8 +87,8 @@ export default function Home() {
87
87
}
88
88
89
89
async function updateProduct ( ) {
90
- const productIDToUpdate = productIDToUpdateRef . current . value ;
91
- const productNameToUpdate = productNameToUpdateRef . current . value ;
90
+ const productIDToUpdate = productIDToUpdateRef . current . value . trim ( ) ;
91
+ const productNameToUpdate = productNameToUpdateRef . current . value . trim ( ) ;
92
92
if ( ! productIDToUpdate . length ) return ;
93
93
const postData = {
94
94
method : "PATCH" ,
@@ -138,23 +138,45 @@ export default function Home() {
138
138
< title > CRUD NEXT.JS Tutorial with Next JS API endpoints</ title >
139
139
</ Head >
140
140
< div className = { styles . container } >
141
- < h1 > CRUD Next.Js API Demo</ h1 >
142
- < p >
143
- Create, Read, Update, Delete database data in React, Node and Next.JS
144
- by Omar Elbaga{ " " }
145
- < a
146
- href = "https://github.com/oelbaga/nextjs-mysql"
147
- target = "_blank"
148
- rel = "noreferrer"
149
- >
150
- GitHub
151
- </ a >
152
- </ p >
153
- < div className = { styles . heading } >
154
- < a href = "/api/products" target = "_blank" rel = "noreferrer" >
155
- Database API data
156
- </ a >
157
- </ div >
141
+ < section className = { styles . main } >
142
+ < h1 > CRUD Next.Js API Demo</ h1 >
143
+ < p >
144
+ Create, Read, Update, Delete database data in React, Node and
145
+ Next.JS by Omar Elbaga{ " " }
146
+ < a
147
+ href = "https://github.com/oelbaga/nextjs-mysql"
148
+ target = "_blank"
149
+ rel = "noreferrer"
150
+ >
151
+ GitHub
152
+ </ a >
153
+ </ p >
154
+ < div className = { styles . heading } >
155
+ < a href = "/api/products" target = "_blank" rel = "noreferrer" >
156
+ Database API data
157
+ </ a >
158
+ </ div >
159
+ </ section >
160
+ < section >
161
+ < div className = { styles . read } >
162
+ < h2 > Read</ h2 >
163
+ < div className = { styles . products } >
164
+ { products . map ( ( item , index ) => {
165
+ return (
166
+ < div key = { item . product_id } className = { styles . product } >
167
+ < span > product_id</ span > : { item . product_id } < br /> { " " }
168
+ < span > product_name</ span > : { item . product_name } { " " }
169
+ < CiTrash
170
+ className = { styles . icons }
171
+ onClick = { ( ) => deleteProduct ( item . product_id ) }
172
+ />
173
+ </ div >
174
+ ) ;
175
+ } ) }
176
+ { ! products . length ? < > No products found</ > : null }
177
+ </ div >
178
+ </ div >
179
+ </ section >
158
180
< section >
159
181
< div className = { styles . create } >
160
182
< h2 > Create</ h2 >
@@ -173,21 +195,6 @@ export default function Home() {
173
195
</ div >
174
196
</ div >
175
197
</ section >
176
- < section >
177
- < div className = { styles . read } >
178
- < h2 > Read</ h2 >
179
- < div className = { styles . products } >
180
- { products . map ( ( item , index ) => {
181
- return (
182
- < div key = { item . product_id } className = { styles . product } >
183
- < span > Id</ span > :{ item . product_id } < br /> < span > Name</ span > :
184
- { item . product_name }
185
- </ div >
186
- ) ;
187
- } ) }
188
- </ div >
189
- </ div >
190
- </ section >
191
198
< section >
192
199
< div className = { styles . update } >
193
200
< h2 > Update</ h2 >
@@ -225,22 +232,26 @@ export default function Home() {
225
232
className = { `${ styles . button } ${ styles . warning } ` }
226
233
value = "Delete"
227
234
type = "button"
228
- onClick = { deleteProduct }
235
+ onClick = { ( ) =>
236
+ deleteProduct ( productIDToDeleteRef . current . value )
237
+ }
229
238
/>
230
239
</ div >
231
240
</ div >
232
241
</ section >
233
- < p >
234
- Create, Read, Update, Delete database data in React, Node and Next.JS
235
- by Omar Elbaga{ " " }
236
- < a
237
- href = "https://github.com/oelbaga/nextjs-mysql"
238
- target = "_blank"
239
- rel = "noreferrer"
240
- >
241
- GitHub
242
- </ a >
243
- </ p >
242
+ < footer >
243
+ < p >
244
+ Create, Read, Update, Delete database data in React, Node and
245
+ Next.JS by Omar Elbaga{ " " }
246
+ < a
247
+ href = "https://github.com/oelbaga/nextjs-mysql"
248
+ target = "_blank"
249
+ rel = "noreferrer"
250
+ >
251
+ GitHub
252
+ </ a >
253
+ </ p >
254
+ </ footer >
244
255
</ div >
245
256
</ >
246
257
) ;
0 commit comments