Advertisement
Radoslav_03

app.jsx s edit

May 10th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. import React, { useState } from 'react';
  2. import './App.css';
  3.  
  4. const booksData = [{
  5. id: 1,
  6. title: "The 48 Laws of Power",
  7. author: "Robert Greene",
  8. isbn: "978-0140280197",
  9. price: "$19.99",
  10. publicationDate: "1998-09-01"
  11. },
  12.  
  13. {
  14. id: 2,
  15. title: "The 80/20 Principle",
  16. author: "Richard Koch",
  17. isbn: "978-0385491747",
  18. price: "$17.99",
  19. publicationDate: "1949-06-08",
  20. },
  21.  
  22. {
  23. id: 3,
  24. title: "Are You Indispensable?",
  25. author: "Seth Godin",
  26. isbn: "978-1591844099",
  27. price: "$15.99",
  28. publicationDate: "2010-01-26",
  29. }]
  30.  
  31. function App() {
  32. const [books, setBooks] = useState(booksData);
  33. const [selectedBook, setSelectedBook] = useState({ id: '', title: '', author: '', isbn: '', price: '', publicationDate: '' });
  34.  
  35. const handleChange = (event) => {
  36. const { name, value } = event.target;
  37. setSelectedBook({ ...selectedBook, [name]: value });
  38. };
  39.  
  40. const formatDate = (dateString) => {
  41. const date = new Date(dateString);
  42. return date.toISOString().split('T')[0];
  43. };
  44.  
  45. const validateBook = () => {
  46. if (!selectedBook.title || !selectedBook.author || !selectedBook.isbn || !selectedBook.price || !selectedBook.publicationDate) {
  47. alert('All fields must be filled out');
  48. return false;
  49. }
  50. return true;
  51. };
  52.  
  53. const handleSave = () => {
  54. if (!validateBook()) return;
  55.  
  56. const formattedDate = formatDate(selectedBook.publicationDate);
  57.  
  58. setBooks((prevBooks) => {
  59. if (selectedBook.id) {
  60. return prevBooks.map(book => (book.id === selectedBook.id ? { ...selectedBook, publicationDate: formattedDate } : book));
  61. } else {
  62. const newBook = { ...selectedBook, id: prevBooks.length + 1, publicationDate: formattedDate };
  63. return [...prevBooks, newBook];
  64. }
  65. });
  66.  
  67. handleClear();
  68. };
  69.  
  70. const handleEdit = () => {
  71. if (!validateBook()) return;
  72.  
  73. const formattedDate = formatDate(selectedBook.publicationDate);
  74.  
  75. setBooks((prevBooks) => prevBooks.map(book => (book.id === selectedBook.id ? { ...selectedBook, publicationDate: formattedDate } : book)));
  76. handleClear();
  77. };
  78.  
  79. const handleDelete = (id) => {
  80. setBooks((prevBooks) => prevBooks.filter(book => book.id !== id).map((book, index) => ({ ...book, id: index + 1 })));
  81. handleClear();
  82. };
  83.  
  84. const handleSelectBook = (book) => {
  85. setSelectedBook(book);
  86. };
  87.  
  88. const handleClear = () => {
  89. setSelectedBook({ id: '', title: '', author: '', isbn: '', price: '', publicationDate: '' });
  90. };
  91.  
  92. return (
  93. <div className="app-container">
  94. <Navbar />
  95. <div className="library-container">
  96. <ul className="content-list">
  97. <h2>List of all books:</h2>
  98. {books.length > 0 ? (
  99. books.map(book => (
  100. <li key={book.id} onClick={() => handleSelectBook(book)}>
  101. <span className="id"><strong>ID: </strong> {book.id}</span>
  102. <p className="field1"><strong>Title: </strong> {book.title}</p>
  103. <p className="field2"><strong>Author: </strong> {book.author}</p>
  104. <p className="field3"><strong>ISBN: </strong> {book.isbn}</p>
  105. <p className="field4"><strong>Price: </strong> {book.price}</p>
  106. <p className="field5"><strong>Publication Date :</strong> {book.publicationDate}</p>
  107. <button className="deleteButton" onClick={(e) => { e.stopPropagation(); handleDelete(book.id); }}>Delete</button>
  108. </li>
  109. ))
  110. ) : (
  111. <p>Oops. No books have been found yet.</p>
  112. )}
  113. </ul>
  114. <div className="content-details">
  115. <h2>Enter book details:</h2>
  116. <form>
  117. <input id="field1" name="title" value={selectedBook.title} onChange={handleChange} placeholder="Title" required />
  118. <input id="field2" name="author" value={selectedBook.author} onChange={handleChange} placeholder="Author" required />
  119. <input id="field3" name="isbn" value={selectedBook.isbn} onChange={handleChange} placeholder="ISBN" required />
  120. <input id="field4" name="price" value={selectedBook.price} onChange={handleChange} placeholder="Price" required />
  121. <input id="field5" name="publicationDate" type="date" value={selectedBook.publicationDate} onChange={handleChange} required />
  122. {selectedBook.id ? (
  123. <button id="editButton" type="button" onClick={handleEdit}>Edit</button>
  124. ) : (
  125. <button id="saveButton" type="button" onClick={handleSave}>Save</button>
  126. )}
  127. <button id="clearButton" type="button" onClick={handleClear}>Clear</button>
  128. </form>
  129. </div>
  130. </div>
  131. <Footer />
  132. </div>
  133. );
  134. }
  135.  
  136. function Navbar() {
  137. return (
  138. <header className="navbar">
  139. <img src="https://i.pinimg.com/736x/e6/9d/67/e69d67edadb25b4a46e5afc583b9cf1d.jpg" alt="Logo" style={{ width: '50px'}}/>
  140.  
  141. <span style={{ marginLeft: '10px' }}><strong>Library Management System</strong></span>
  142. </header>
  143. );
  144. }
  145.  
  146. function Footer() {
  147. return <footer className="footer">DSS React Project | Radoslav Zahariev</footer>;
  148. }
  149.  
  150. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement