|
| 1 | +import { useState, useEffect } from 'react'; |
| 2 | +import styles from './BookForm.module.css'; |
| 3 | + |
| 4 | +const BookForm = ({ book, onSubmit, onCancel }) => { |
| 5 | + const [formData, setFormData] = useState({ |
| 6 | + title: '', |
| 7 | + author: '', |
| 8 | + year: new Date().getFullYear(), |
| 9 | + status: 'pending', |
| 10 | + }); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + if (book) { |
| 14 | + setFormData({ |
| 15 | + title: book.title, |
| 16 | + author: book.author, |
| 17 | + year: book.year, |
| 18 | + status: book.status, |
| 19 | + }); |
| 20 | + } |
| 21 | + }, [book]); |
| 22 | + |
| 23 | + const handleSubmit = (e) => { |
| 24 | + e.preventDefault(); |
| 25 | + if (formData.title.trim() && formData.author.trim()) { |
| 26 | + onSubmit(formData); |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + const handleChange = (e) => { |
| 31 | + const { name, value } = e.target; |
| 32 | + setFormData((prev) => ({ |
| 33 | + ...prev, |
| 34 | + [name]: value, |
| 35 | + })); |
| 36 | + }; |
| 37 | + |
| 38 | + return ( |
| 39 | + <div className={styles['book-form']}> |
| 40 | + <h2 className={styles['book-form__title']}> |
| 41 | + {book ? 'Edit Book' : 'Add New Book'} |
| 42 | + </h2> |
| 43 | + |
| 44 | + <form onSubmit={handleSubmit} className={styles['book-form__form']}> |
| 45 | + <div className={styles['book-form__group']}> |
| 46 | + <label htmlFor="title" className={styles['book-form__label']}> |
| 47 | + Title |
| 48 | + </label> |
| 49 | + <input |
| 50 | + type="text" |
| 51 | + id="title" |
| 52 | + name="title" |
| 53 | + value={formData.title} |
| 54 | + onChange={handleChange} |
| 55 | + placeholder="Enter book title" |
| 56 | + className={styles['book-form__input']} |
| 57 | + required |
| 58 | + /> |
| 59 | + </div> |
| 60 | + |
| 61 | + <div className={styles['book-form__group']}> |
| 62 | + <label htmlFor="author" className={styles['book-form__label']}> |
| 63 | + Autor |
| 64 | + </label> |
| 65 | + <input |
| 66 | + type="text" |
| 67 | + id="author" |
| 68 | + name="author" |
| 69 | + value={formData.author} |
| 70 | + onChange={handleChange} |
| 71 | + placeholder="Enter author name" |
| 72 | + className={styles['book-form__input']} |
| 73 | + required |
| 74 | + /> |
| 75 | + </div> |
| 76 | + |
| 77 | + <div className={styles['book-form__row']}> |
| 78 | + <div className={styles['book-form__group']}> |
| 79 | + <label htmlFor="year" className={styles['book-form__label']}> |
| 80 | + Publication Year |
| 81 | + </label> |
| 82 | + <input |
| 83 | + type="number" |
| 84 | + id="year" |
| 85 | + name="year" |
| 86 | + value={formData.year} |
| 87 | + onChange={handleChange} |
| 88 | + className={styles['book-form__input']} |
| 89 | + min="1000" |
| 90 | + max={new Date().getFullYear() + 10} |
| 91 | + required |
| 92 | + /> |
| 93 | + </div> |
| 94 | + |
| 95 | + <div className={styles['book-form__group']}> |
| 96 | + <label htmlFor="status" className={styles['book-form__label']}> |
| 97 | + Reading Status |
| 98 | + </label> |
| 99 | + <select |
| 100 | + id="status" |
| 101 | + name="status" |
| 102 | + value={formData.status} |
| 103 | + onChange={handleChange} |
| 104 | + className={styles['book-form__select']} |
| 105 | + > |
| 106 | + <option value="pending">Pending</option> |
| 107 | + <option value="in progress">In Progress</option> |
| 108 | + <option value="read">Read</option> |
| 109 | + </select> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + |
| 113 | + <button type="submit" className={styles['book-form__button']}> |
| 114 | + {book ? 'Update Book' : 'Add Book'} |
| 115 | + </button> |
| 116 | + </form> |
| 117 | + </div> |
| 118 | + ); |
| 119 | +}; |
| 120 | + |
| 121 | +export default BookForm; |
0 commit comments