1
1
'use client' ;
2
2
3
- import { useParams } from 'next/navigation' ;
3
+ import { redirect , useParams } from 'next/navigation' ;
4
4
import {
5
5
Button ,
6
6
Card ,
7
7
Container ,
8
8
Grid ,
9
+ Select ,
9
10
Space ,
10
11
Stack ,
11
12
Text ,
@@ -53,16 +54,20 @@ export default function SearchBar() {
53
54
54
55
// получаем инфо о всех полках
55
56
56
- const resBookshelves = await fetch ( `${ API_HOST } /bookshelves` , {
57
+ const resBookshelves = await fetch ( `${ API_HOST } /bookshelves/ ` , {
57
58
method : 'GET' ,
58
59
headers : {
59
60
'Content-Type' : 'application/json' ,
60
61
Authorization : `Bearer ${ token } ` ,
61
62
} ,
62
63
} ) ;
63
64
64
- const dataBookshelves : Bookshelf [ ] = await resBookshelves . json ( ) ;
65
- setBookshelves ( dataBookshelves ) ;
65
+ const dataBookshelves : Bookshelf [ ] | null = await resBookshelves . json ( ) ;
66
+ if ( dataBookshelves != null ) {
67
+ console . log ( 'resBookshelves.json() != null' ) ;
68
+ console . log ( resBookshelves . json ( ) ) ;
69
+ setBookshelves ( dataBookshelves ) ;
70
+ }
66
71
67
72
// получаем инфо о полке
68
73
@@ -103,12 +108,45 @@ export default function SearchBar() {
103
108
cover_image : '' ,
104
109
bookshelf_id : '' ,
105
110
} ,
106
- // validate: {
107
- // title: (value) => (value && value.length > 0 ? null : 'Title is required'),
108
- // author: (value) => (value && value.length > 0 ? null : 'Author is required'),
109
- // },
110
111
} ) ;
111
112
113
+ const handleSubmit = async ( values : BookUpdate ) => {
114
+ // Filter out empty fields
115
+ const nonEmptyFields = Object . fromEntries (
116
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
117
+ Object . entries ( values ) . filter ( ( [ _ , v ] ) => v !== '' && v !== undefined )
118
+ ) ;
119
+
120
+ // If no non-empty fields, do not make the request
121
+ if ( Object . keys ( nonEmptyFields ) . length === 0 ) {
122
+ console . log ( 'No non-empty fields to submit' ) ;
123
+ return ;
124
+ }
125
+
126
+ try {
127
+ const token = await user ?. getIdToken ( ) ;
128
+
129
+ // Make the request
130
+ const response = await fetch ( `${ API_HOST } /books/${ book ?. id } ` , {
131
+ method : 'PUT' ,
132
+ headers : {
133
+ 'Content-Type' : 'application/json' ,
134
+ Authorization : `Bearer ${ token } ` ,
135
+ } ,
136
+ body : JSON . stringify ( nonEmptyFields ) ,
137
+ } ) ;
138
+
139
+ if ( response . ok ) {
140
+ // Redirect to the homepage after successful submission
141
+ redirect ( '/' ) ;
142
+ } else {
143
+ console . error ( 'Failed to submit form:' , response . statusText ) ;
144
+ }
145
+ } catch ( error ) {
146
+ console . error ( 'Error submitting form:' , error ) ;
147
+ }
148
+ } ;
149
+
112
150
// console.log(bookshelves);
113
151
if ( loading ) {
114
152
return < div > Loading...</ div > ;
@@ -118,12 +156,6 @@ export default function SearchBar() {
118
156
return < div > Book not foung</ div > ;
119
157
}
120
158
121
- // Handle form submission
122
- const handleSubmit = ( values : BookUpdate ) => {
123
- console . log ( 'Form submitted:' , values ) ;
124
- // Handle form submission logic here
125
- } ;
126
-
127
159
return (
128
160
< Container size = "sm" >
129
161
< Card shadow = "sm" padding = "lg" radius = "md" withBorder >
@@ -208,10 +240,20 @@ export default function SearchBar() {
208
240
placeholder = "Enter cover image URL"
209
241
{ ...form . getInputProps ( 'cover_image' ) }
210
242
/>
211
- < TextInput
243
+ { /* <TextInput
212
244
label="Bookshelf ID"
213
245
placeholder="Enter bookshelf ID"
214
246
{...form.getInputProps('bookshelf_id')}
247
+ /> */ }
248
+ < Select
249
+ label = "Bookshelf"
250
+ placeholder = "Pick a bookshelf"
251
+ data = { bookshelves . map ( ( shelf ) => ( {
252
+ value : shelf . id ,
253
+ label : shelf . name ,
254
+ } ) ) }
255
+ { ...form . getInputProps ( 'bookshelf_id' ) }
256
+ clearable
215
257
/>
216
258
< Button type = "submit" mt = "md" >
217
259
Update Book
0 commit comments