/
lostSun
/
project-client-server-apps
Обзор
Документация
Войти
/
lostSun
/
project-client-server-apps
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/pages/AddBookPage/AddBookPage.js
119 строк
3 KB
1lostsun
update project
25 май 2025, 13:12
25 май 2025, 13:12
78a0079
Код
Авторство
О чём код?
import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import styles from './AddBookPage.module.css'; const AddBookPage = () => { const navigate = useNavigate(); const [formData, setFormData] = useState({ title: '', author: '', description: '', year: '', isbn: '' }); const handleChange = (e) => { setFormData({ ...formData, [e.target.name]: e.target.value }); }; const handleSubmit = async (e) => { e.preventDefault(); try { const response = await fetch('http://localhost:3001/api/books', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(formData), }); if (response.ok) { navigate('/catalog'); } else { throw new Error('Failed to add book'); } } catch (error) { console.error('Error adding book:', error); } }; return ( <div className={styles.container}> <h1>Add New Book</h1> <form onSubmit={handleSubmit} className={styles.form}> <div className={styles.formGroup}> <label htmlFor="title">Title</label> <input type="text" id="title" name="title" value={formData.title} onChange={handleChange} required /> </div> <div className={styles.formGroup}> <label htmlFor="author">Author</label> <input type="text" id="author" name="author" value={formData.author} onChange={handleChange} required /> </div> <div className={styles.formGroup}> <label htmlFor="description">Description</label> <textarea id="description" name="description" value={formData.description} onChange={handleChange} rows="4" /> </div> <div className={styles.formGroup}> <label htmlFor="year">Year</label> <input type="number" id="year" name="year" value={formData.year} onChange={handleChange} /> </div> <div className={styles.formGroup}> <label htmlFor="isbn">ISBN</label> <input type="text" id="isbn" name="isbn" value={formData.isbn} onChange={handleChange} /> </div> <div className={styles.buttons}> <button type="button" onClick={() => navigate('/catalog')} className={styles.cancelButton}> Cancel </button> <button type="submit" className={styles.submitButton}> Add Book </button> </div> </form> </div> ); }; export default AddBookPage;