Advertisement
Swaster

Webówka 16.01

Jan 17th, 2025
22
0
7 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'bootstrap/dist/css/bootstrap.css'
  2. import {useState } from 'react';
  3.  
  4. function App() {
  5.  
  6.   const [flowerTag, setFlowers] = useState(true)
  7.   const [animalTag, setAnimals] = useState(true)
  8.   const [carTag, setCars] = useState(true)
  9.  
  10.   const switchArr = [flowerTag, animalTag, carTag];
  11.  
  12.   const [stateData, setStateData] = useState([
  13.     {id: 0, alt: "Mak", filename: "obraz1.jpg", category:1, downloads: 35},
  14.     {id: 1, alt:"Bukiet", filename: "obraz2.jpg", category: 1, downloads: 43},
  15.     {id: 2, alt:"Dalmatyńczyk", filename: "obraz3.jpg", category:2, downloads: 2},
  16.     {id: 3, alt:"Świnka morska", filename: "obraz4.jpg", category:2, downloads: 53},
  17.     {id: 4, alt:"Rotwailer", filename: "obraz5.jpg", category:2, downloads: 43},
  18.     {id: 5, alt:"Audi", filename: "obraz6.jpg", category:3, downloads: 11},
  19.     {id: 6, alt:"kotki", filename: "obraz7.jpg", category:2, downloads: 22},
  20.     {id: 7, alt:"Róża", filename: "obraz8.jpg", category:1, downloads: 33},
  21.     {id: 8, alt:"Świnka morska", filename: "obraz9.jpg", category:2, downloads: 123},
  22.     {id: 9, alt:"Foksterier", filename: "obraz10.jpg", category:2, downloads: 22},
  23.     {id: 10, alt:"Szczeniak", filename: "obraz11.jpg", category:2, downloads: 12},
  24.     {id: 11, alt:"Garbus", filename: "obraz12.jpg", category:3, downloads: 321}
  25.   ]);
  26.   function incrementDownloads(id) {
  27.     const nextStateData = stateData.concat([]);
  28.     for (let i = 0; i < nextStateData.length; i++) {
  29.       const elem = nextStateData[i];
  30.       if (elem.id == id) {
  31.         const newElem = {...elem, downloads: elem.downloads+1};
  32.         const newState = [...nextStateData];
  33.         newState[i] = newElem;
  34.         setStateData(newState);
  35.         return;
  36.       }
  37.     }
  38.   }
  39.  
  40.   return (
  41.     <div className="container">
  42.       <h1>Kategorie zdjęć</h1>
  43.       <div className="form-check form-switch form-check-inline">
  44.         <input type="checkbox" role="switch" className='form-check-input' name="kwiaty" id="" defaultChecked='true' onChange={(event) => setFlowers(event.target.checked)}/>
  45.         <label htmlFor="kwiaty" className="form-check-label">Kwiaty</label>
  46.       </div>
  47.       <div className="form-check form-switch form-check-inline">
  48.         <input type="checkbox" role="switch" className='form-check-input' name="zwierzęta" id="" defaultChecked='true' onChange={(event) => setAnimals(event.target.checked)}/>
  49.         <label htmlFor="zwierzęta" className="form-check-label">Zwierzęta</label>
  50.       </div>
  51.       <div className="form-check form-switch form-check-inline">
  52.         <input type="checkbox" role="switch" className='form-check-input' name="samochody" id="" defaultChecked='true' onChange={(event) => setCars(event.target.checked)}/>
  53.         <label htmlFor="samochody" className="form-check-label">Samochody</label>
  54.       </div>
  55.  
  56.       <div className='row g-0'>
  57.       { stateData.filter((elem) => switchArr[elem.category-1] ).map((elem, index) => {
  58.         return (
  59.           <div key={index} style={{width: "auto"}}>
  60.             <img src={"./assets/"+elem.filename} alt={elem.alt} className='rounded' style={{margin: "5px"}}></img>
  61.             <h4>Pobrań: {elem.downloads}</h4>
  62.             <input type="button" value="Pobierz" className='btn btn-success' onClick={() => incrementDownloads(elem.id)}/>
  63.           </div>
  64.           )
  65.       }
  66.       )}
  67.  
  68.       </div>
  69.  
  70.     </div>
  71.   );
  72. }
  73.  
  74. export default App;
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement