Skip to content

Commit 49152d7

Browse files
committed
update page finished
1 parent d5161eb commit 49152d7

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

src/app/book/[id]/edit/page.tsx

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use client';
22

3-
import { useParams } from 'next/navigation';
3+
import { redirect, useParams } from 'next/navigation';
44
import {
55
Button,
66
Card,
77
Container,
88
Grid,
9+
Select,
910
Space,
1011
Stack,
1112
Text,
@@ -53,16 +54,20 @@ export default function SearchBar() {
5354

5455
// получаем инфо о всех полках
5556

56-
const resBookshelves = await fetch(`${API_HOST}/bookshelves`, {
57+
const resBookshelves = await fetch(`${API_HOST}/bookshelves/`, {
5758
method: 'GET',
5859
headers: {
5960
'Content-Type': 'application/json',
6061
Authorization: `Bearer ${token}`,
6162
},
6263
});
6364

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+
}
6671

6772
// получаем инфо о полке
6873

@@ -103,12 +108,45 @@ export default function SearchBar() {
103108
cover_image: '',
104109
bookshelf_id: '',
105110
},
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-
// },
110111
});
111112

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+
112150
// console.log(bookshelves);
113151
if (loading) {
114152
return <div>Loading...</div>;
@@ -118,12 +156,6 @@ export default function SearchBar() {
118156
return <div>Book not foung</div>;
119157
}
120158

121-
// Handle form submission
122-
const handleSubmit = (values: BookUpdate) => {
123-
console.log('Form submitted:', values);
124-
// Handle form submission logic here
125-
};
126-
127159
return (
128160
<Container size="sm">
129161
<Card shadow="sm" padding="lg" radius="md" withBorder>
@@ -208,10 +240,20 @@ export default function SearchBar() {
208240
placeholder="Enter cover image URL"
209241
{...form.getInputProps('cover_image')}
210242
/>
211-
<TextInput
243+
{/* <TextInput
212244
label="Bookshelf ID"
213245
placeholder="Enter bookshelf ID"
214246
{...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
215257
/>
216258
<Button type="submit" mt="md">
217259
Update Book

0 commit comments

Comments
 (0)