|
| 1 | +import { useState } from 'react' |
| 2 | +import * as ReactDOM from 'react-dom/client' |
| 3 | +import { generateGradient, getMatchingPosts } from '#shared/blog-posts' |
| 4 | + |
| 5 | +// 🐨 make a function here called getQueryParam |
| 6 | + |
| 7 | +function App() { |
| 8 | + // 🐨 move 👇 up to getQueryParam |
| 9 | + const params = new URLSearchParams(window.location.search) |
| 10 | + const initialQuery = params.get('query') ?? '' |
| 11 | + // 🐨 move 👆 up to getQueryParam and return the initialQuery |
| 12 | + |
| 13 | + // 🐨 pass getQueryParam into useState |
| 14 | + const [query, setQuery] = useState(initialQuery) |
| 15 | + const words = query.split(' ') |
| 16 | + |
| 17 | + const dogChecked = words.includes('dog') |
| 18 | + const catChecked = words.includes('cat') |
| 19 | + const caterpillarChecked = words.includes('caterpillar') |
| 20 | + |
| 21 | + function handleCheck(tag: string, checked: boolean) { |
| 22 | + const newWords = checked ? [...words, tag] : words.filter(w => w !== tag) |
| 23 | + setQuery(newWords.filter(Boolean).join(' ').trim()) |
| 24 | + } |
| 25 | + |
| 26 | + return ( |
| 27 | + <div className="app"> |
| 28 | + <form> |
| 29 | + <div> |
| 30 | + <label htmlFor="searchInput">Search:</label> |
| 31 | + <input |
| 32 | + id="searchInput" |
| 33 | + name="query" |
| 34 | + type="search" |
| 35 | + value={query} |
| 36 | + onChange={e => setQuery(e.currentTarget.value)} |
| 37 | + /> |
| 38 | + </div> |
| 39 | + <div> |
| 40 | + <label> |
| 41 | + <input |
| 42 | + type="checkbox" |
| 43 | + checked={dogChecked} |
| 44 | + onChange={e => handleCheck('dog', e.currentTarget.checked)} |
| 45 | + />{' '} |
| 46 | + 🐶 dog |
| 47 | + </label> |
| 48 | + <label> |
| 49 | + <input |
| 50 | + type="checkbox" |
| 51 | + checked={catChecked} |
| 52 | + onChange={e => handleCheck('cat', e.currentTarget.checked)} |
| 53 | + />{' '} |
| 54 | + 🐱 cat |
| 55 | + </label> |
| 56 | + <label> |
| 57 | + <input |
| 58 | + type="checkbox" |
| 59 | + checked={caterpillarChecked} |
| 60 | + onChange={e => |
| 61 | + handleCheck('caterpillar', e.currentTarget.checked) |
| 62 | + } |
| 63 | + />{' '} |
| 64 | + 🐛 caterpillar |
| 65 | + </label> |
| 66 | + </div> |
| 67 | + <button type="submit">Submit</button> |
| 68 | + </form> |
| 69 | + <MatchingPosts query={query} /> |
| 70 | + </div> |
| 71 | + ) |
| 72 | +} |
| 73 | + |
| 74 | +function MatchingPosts({ query }: { query: string }) { |
| 75 | + const matchingPosts = getMatchingPosts(query) |
| 76 | + |
| 77 | + return ( |
| 78 | + <ul className="post-list"> |
| 79 | + {matchingPosts.map(post => ( |
| 80 | + <li key={post.id}> |
| 81 | + <div |
| 82 | + className="post-image" |
| 83 | + style={{ background: generateGradient(post.id) }} |
| 84 | + /> |
| 85 | + <a |
| 86 | + href={post.id} |
| 87 | + onClick={event => { |
| 88 | + event.preventDefault() |
| 89 | + alert(`Great! Let's go to ${post.id}!`) |
| 90 | + }} |
| 91 | + > |
| 92 | + <h2>{post.title}</h2> |
| 93 | + <p>{post.description}</p> |
| 94 | + </a> |
| 95 | + </li> |
| 96 | + ))} |
| 97 | + </ul> |
| 98 | + ) |
| 99 | +} |
| 100 | + |
| 101 | +const rootEl = document.createElement('div') |
| 102 | +document.body.append(rootEl) |
| 103 | +ReactDOM.createRoot(rootEl).render(<App />) |
0 commit comments