-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
67 lines (62 loc) · 2.14 KB
/
Copy pathApp.js
File metadata and controls
67 lines (62 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { AnimatePresence } from 'framer-motion'
import { useEffect, useState } from 'react'
import useFetch from 'react-fetch-hook'
import ContactCards from './ContactCards'
import ContactModal from './ContactModal'
const App = () => {
const url = 'https://randomuser.me/api/?results=200'
const { data, isLoading, error } = useFetch(url)
const [selectedContact, setSelectedContact] = useState(null)
const [contactList, setContactList] = useState()
const [filterQuery, setFilterQuery] = useState()
useEffect(() => {
if (!filterQuery) {
setContactList(data?.results?.slice(0, 10))
} else {
const queryString = filterQuery.toLowerCase()
const filteredData = data?.results?.filter(contact => {
const fullName = `${contact.name.first} ${contact.name.last}`
// if it's just one letter, return all names that start with it
if (queryString.length === 1) {
const firstLetter = fullName.charAt(0).toLowerCase()
return firstLetter === queryString
}
else {
return fullName.toLowerCase().includes(queryString)
}
})
setContactList(filteredData)
}
}, [data, filterQuery])
return (
<div className="bg-gray-100">
<section>
<form>
<input
type={"text"}
placeholder={"type here to filter..."}
onChange={event => setFilterQuery(event.target.value)}
className={"ml-20 mt-6 rounded-md p-2"}
/>
</form>
</section>
<section className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-4 gap-6 p-10 md:p-20 lg:p-20">
{isLoading
? <h1>Fetching data...</h1>
: <ContactCards contactList={contactList} setSelectedContact={setSelectedContact} />
}
{error && <h1>Error fetching data...</h1>}
{contactList?.length < 1 && <h1>No data matches your search</h1>}
</section>
<AnimatePresence>
{selectedContact &&
<ContactModal
contact={selectedContact}
setSelectedContact={setSelectedContact}
/>
}
</AnimatePresence>
</div>
)
}
export default App