|
| 1 | +# DOM Bookstore Project - Walkthrough |
| 2 | + |
| 3 | +## 📘 What You’re Building |
| 4 | + |
| 5 | +You’ll create a **Bookstore Web Application** using basic front-end tools: |
| 6 | + |
| 7 | +* 🧱 HTML for structure |
| 8 | +* 🎨 Tailwind CSS for styling |
| 9 | +* ⚙️ JavaScript for logic |
| 10 | +* 🔌 `fetch()` to interact with a real Bookstore API (GET, POST, DELETE) |
| 11 | + |
| 12 | +This guide is written like a class tutorial with simple language, perfect for beginners. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## 📁 1. Folder & File Setup |
| 17 | + |
| 18 | +### 🛠 Step-by-Step Setup |
| 19 | + |
| 20 | +1. Create a folder named `bookstore-project` |
| 21 | +2. Inside that folder, create these files: |
| 22 | + |
| 23 | + * `index.html` → the web page |
| 24 | + * `style.css` → styling rules |
| 25 | + * `index.js` → your logic and interactivity |
| 26 | +3. (Optional) Create `.env` for API secrets (advanced) |
| 27 | + |
| 28 | +### ✅ Connect Everything |
| 29 | + |
| 30 | +In your `index.html` file, connect your CSS and JS like this: |
| 31 | + |
| 32 | +```html |
| 33 | +<head> |
| 34 | + <title>Bookstore</title> |
| 35 | + <link rel="stylesheet" href="style.css"> |
| 36 | + <script defer src="index.js"></script> |
| 37 | +</head> |
| 38 | +``` |
| 39 | + |
| 40 | +Use the **Live Server** extension in VS Code to run your site in the browser. |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## 🏗️ 2. Build Your HTML (Structure) |
| 45 | + |
| 46 | +This code goes in `index.html`. It sets up a form for adding books and a container to display them: |
| 47 | + |
| 48 | +```html |
| 49 | +<body> |
| 50 | + <h1>📚 My Bookstore</h1> |
| 51 | + <form id="book-form"> |
| 52 | + <input type="text" id="title" placeholder="Book Title" required> |
| 53 | + <input type="text" id="author" placeholder="Author" required> |
| 54 | + <button type="submit">Add Book</button> |
| 55 | + </form> |
| 56 | + |
| 57 | + <div id="books-container"></div> |
| 58 | +</body> |
| 59 | +``` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## 🎨 3. Tailwind Styling (style.css) |
| 64 | + |
| 65 | +### Step 1: Add Tailwind CSS to your HTML |
| 66 | + |
| 67 | +Put this in the `<head>`: |
| 68 | + |
| 69 | +```html |
| 70 | +<script src="https://cdn.tailwindcss.com"></script> |
| 71 | +``` |
| 72 | + |
| 73 | +### Step 2: Add Optional Custom Styles |
| 74 | + |
| 75 | +```css |
| 76 | +/* style.css */ |
| 77 | +body { |
| 78 | + font-family: sans-serif; |
| 79 | + margin: 2rem; |
| 80 | + background-color: #f9fafb; |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +You’ll style your buttons, inputs, and divs using Tailwind utility classes in HTML. |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## 🧠 4. JavaScript Logic (index.js) |
| 89 | + |
| 90 | +### 4.1 Access the DOM |
| 91 | + |
| 92 | +```js |
| 93 | +const form = document.getElementById('book-form'); |
| 94 | +const booksContainer = document.getElementById('books-container'); |
| 95 | +``` |
| 96 | + |
| 97 | +### 4.2 Add a Book (POST Request) |
| 98 | + |
| 99 | +```js |
| 100 | +form.addEventListener('submit', function(e) { |
| 101 | + e.preventDefault(); |
| 102 | + const title = document.getElementById('title').value; |
| 103 | + const author = document.getElementById('author').value; |
| 104 | + |
| 105 | + fetch('https://bookstore-api-six.vercel.app/books', { |
| 106 | + method: 'POST', |
| 107 | + headers: { 'Content-Type': 'application/json' }, |
| 108 | + body: JSON.stringify({ title, author }) |
| 109 | + }) |
| 110 | + .then(res => res.json()) |
| 111 | + .then(() => { |
| 112 | + renderBooks(); |
| 113 | + form.reset(); |
| 114 | + }); |
| 115 | +}); |
| 116 | +``` |
| 117 | + |
| 118 | +### 4.3 Load Books (GET Request) |
| 119 | + |
| 120 | +```js |
| 121 | +function renderBooks() { |
| 122 | + booksContainer.innerHTML = ''; |
| 123 | + fetch('https://bookstore-api-six.vercel.app/books') |
| 124 | + .then(res => res.json()) |
| 125 | + .then(books => { |
| 126 | + books.forEach(book => { |
| 127 | + const div = document.createElement('div'); |
| 128 | + div.classList.add('book-item'); |
| 129 | + div.innerHTML = ` |
| 130 | + <p><strong>${book.title}</strong> by ${book.author}</p> |
| 131 | + <button onclick="deleteBook('${book.id}')">Delete</button> |
| 132 | + `; |
| 133 | + booksContainer.appendChild(div); |
| 134 | + }); |
| 135 | + }); |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +### 4.4 Delete a Book (DELETE Request) |
| 140 | + |
| 141 | +```js |
| 142 | +function deleteBook(id) { |
| 143 | + fetch(`https://bookstore-api-six.vercel.app/books/${id}`, { |
| 144 | + method: 'DELETE' |
| 145 | + }) |
| 146 | + .then(res => res.json()) |
| 147 | + .then(() => renderBooks()); |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### 4.5 Start Everything on Load |
| 152 | + |
| 153 | +```js |
| 154 | +renderBooks(); |
| 155 | +``` |
| 156 | + |
| 157 | +--- |
| 158 | + |
| 159 | +## 🔁 5. How the App Flows |
| 160 | + |
| 161 | +| Step | Action | Code | |
| 162 | +| ---- | ----------- | ------------------------------------------- | |
| 163 | +| 1️⃣ | Load page | `renderBooks()` fetches books | |
| 164 | +| 2️⃣ | Add book | Form sends a `POST` → re-fetches books | |
| 165 | +| 3️⃣ | Delete book | Button triggers `DELETE` → re-fetches books | |
| 166 | + |
| 167 | +All actions reflect live updates using the DOM. |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## 💾 6. Bonus: Save to Local Storage |
| 172 | + |
| 173 | +```js |
| 174 | +function renderBooks() { |
| 175 | + fetch('https://bookstore-api-six.vercel.app/books') |
| 176 | + .then(res => res.json()) |
| 177 | + .then(books => { |
| 178 | + localStorage.setItem('books', JSON.stringify(books)); |
| 179 | + displayBooks(books); |
| 180 | + }); |
| 181 | +} |
| 182 | + |
| 183 | +function displayBooks(books) { |
| 184 | + booksContainer.innerHTML = ''; |
| 185 | + books.forEach(book => { |
| 186 | + const div = document.createElement('div'); |
| 187 | + div.innerHTML = `<p>${book.title} by ${book.author}</p><button onclick="deleteBook('${book.id}')">Delete</button>`; |
| 188 | + booksContainer.appendChild(div); |
| 189 | + }); |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +--- |
| 194 | + |
| 195 | +## 🧪 7. GitHub Workflow (Team Practice) |
| 196 | + |
| 197 | +1. Create a feature branch: |
| 198 | + |
| 199 | + ```bash |
| 200 | + git checkout -b add-form |
| 201 | + ``` |
| 202 | +2. Stage and commit: |
| 203 | + |
| 204 | + ```bash |
| 205 | + git add . |
| 206 | + git commit -m "Add book form feature" |
| 207 | + ``` |
| 208 | +3. Push and make a Pull Request (PR): |
| 209 | + |
| 210 | + ```bash |
| 211 | + git push origin add-form |
| 212 | + ``` |
| 213 | +4. Get it reviewed → then merge to `main` |
| 214 | + |
| 215 | +--- |
| 216 | + |
| 217 | +## 🚀 8. Deployment (Vercel or Netlify) |
| 218 | + |
| 219 | +1. Push your repo to GitHub |
| 220 | +2. Go to [vercel.com](https://vercel.com) or [netlify.com](https://netlify.com) |
| 221 | +3. Sign in, connect your repo, and deploy |
| 222 | +4. Share the live link! |
| 223 | + |
| 224 | +--- |
| 225 | + |
| 226 | +## 📄 9. README Template |
| 227 | + |
| 228 | +````markdown |
| 229 | +# Bookstore Project |
| 230 | + |
| 231 | +## Features |
| 232 | +- Add / view / delete books |
| 233 | +- Real API with fetch |
| 234 | +- Styled using Tailwind CSS |
| 235 | +- Bonus: localStorage cache |
| 236 | + |
| 237 | +## Setup |
| 238 | +```bash |
| 239 | +git clone https://github.com/yourname/bookstore-project.git |
| 240 | +cd bookstore-project |
| 241 | +open index.html (Live Server) |
| 242 | +```` |
| 243 | + |
| 244 | +## To Do |
| 245 | + |
| 246 | +* Add book editing |
| 247 | +* Add loading indicator |
| 248 | +* Improve error handling |
| 249 | + |
| 250 | +``` |
| 251 | +
|
| 252 | +--- |
| 253 | +
|
| 254 | +## ✅ 10. Final Checklist Before Submission |
| 255 | +- [ ] HTML is valid and clean |
| 256 | +- [ ] API works: GET, POST, DELETE |
| 257 | +- [ ] Tailwind used throughout |
| 258 | +- [ ] GitHub used with branches + PRs |
| 259 | +- [ ] README included |
| 260 | +- [ ] App deployed and link shared |
| 261 | +
|
| 262 | +--- |
| 263 | +
|
| 264 | +You're now ready to build a real-world app like a pro! 🎓💻 |
| 265 | +
|
| 266 | +``` |
1 | 267 |
|
0 commit comments