|
| 1 | +import { useState } from "react"; |
| 2 | + |
| 3 | +type Game = { |
| 4 | + name: string; |
| 5 | + category: string; |
| 6 | + link: string |
| 7 | +} |
| 8 | + |
1 | 9 | function App() {
|
| 10 | + const storedGames = localStorage.getItem('games'); |
| 11 | + |
| 12 | + const [games, setGames] = useState<Game[]>(storedGames ? JSON.parse(storedGames).games : []); |
| 13 | + const [isNewGameModalVisible, setIsNewGameModalVisible] = useState(false) |
| 14 | + |
| 15 | + const handleSubmit: React.FormEventHandler<HTMLFormElement> = (event) => { |
| 16 | + event.preventDefault(); |
| 17 | + const formData = new FormData(event.currentTarget); |
| 18 | + const newGame: Record<string, string> = {}; |
| 19 | + formData.forEach(function (value, key) { |
| 20 | + newGame[key] = value as string; |
| 21 | + }); |
| 22 | + const newGames = [...games, newGame as Game]; |
| 23 | + |
| 24 | + setGames(newGames); |
| 25 | + localStorage.setItem('games', JSON.stringify({ |
| 26 | + games: newGames |
| 27 | + })) |
| 28 | + |
| 29 | + setIsNewGameModalVisible(false); |
| 30 | + } |
| 31 | + |
2 | 32 | return (
|
3 | 33 | <>
|
4 |
| - <h1>Backlogdle</h1> |
| 34 | + <h1>Backlog-dle</h1> |
| 35 | + {games.map(game => ( |
| 36 | + <article> |
| 37 | + <h4>{game.name}</h4> |
| 38 | + <a href={game.link} target="_blank">Jugar</a> |
| 39 | + </article> |
| 40 | + ))} |
| 41 | + <button onClick={() => setIsNewGameModalVisible(true)}>Agregar Juego</button> |
| 42 | + <dialog open={isNewGameModalVisible}> |
| 43 | + <article> |
| 44 | + <header> |
| 45 | + <button aria-label="Close" rel="prev" onClick={() => setIsNewGameModalVisible(false)}></button> |
| 46 | + <p> |
| 47 | + <strong>Agregar juego</strong> |
| 48 | + </p> |
| 49 | + </header> |
| 50 | + <form onSubmit={handleSubmit}> |
| 51 | + <fieldset> |
| 52 | + <label> |
| 53 | + Nombre del juego |
| 54 | + <input type="text" name="name" placeholder="Wordle" /> |
| 55 | + </label> |
| 56 | + <label> |
| 57 | + Categoria |
| 58 | + <input type="text" name="category" placeholder="Palabras" /> |
| 59 | + </label> |
| 60 | + <label> |
| 61 | + Link |
| 62 | + <input type="text" name="link" placeholder="https://lapalabradeldia.com/" /> |
| 63 | + </label> |
| 64 | + </fieldset> |
| 65 | + <input type="submit" value="Agregar" /> |
| 66 | + </form> |
| 67 | + </article> |
| 68 | + </dialog> |
5 | 69 | </>
|
6 | 70 | )
|
7 | 71 | }
|
|
0 commit comments