-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDashboardView.tsx
121 lines (113 loc) · 4.14 KB
/
DashboardView.tsx
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {
ActionIcon,
Box,
Button,
Center,
Container,
Grid,
Group,
MediaQuery,
Pagination,
Text
} from '@mantine/core'
import { IconFilter, IconX, IconZoomCancel } from '@tabler/icons'
import { useState } from 'react'
import { Outlet, useParams } from 'react-router'
import CharacterCard from 'src/components/characters/CharacterCard'
import CustomError from 'src/components/common/CustomError'
import CustomLoading from 'src/components/common/CustomLoading'
import FilterDrawer from 'src/components/filter/FilterDrawer'
import TextField from 'src/components/forms/TextField'
import MobilePagination from 'src/components/pagination/MobilePagination'
import useGetCharacters from 'src/hooks/characters/useGetCharacters'
import { filterDrawerIsOpenVar } from 'src/state/dashboard'
import View from './View'
const DashboardView = () => {
const { id } = useParams()
const [page, setPage] = useState(1)
const { data, loading, error, searchInput, setSearchInput } = useGetCharacters(page)
const characters = data?.characters?.results
const numberOfPages = data?.characters?.info?.pages || 0
const charactersFound = characters?.length !== 0
return (
<View>
<Box>
<FilterDrawer />
<Box>
{!id ? (
<Container>
{error && <CustomError />}
<Text size="xl">Characters</Text>
<Text style={{ paddingTop: 12 }}>
Search for any character from any episode of Rick and Morty
</Text>
<Group style={{ paddingTop: 12 }}>
<MediaQuery smallerThan="sm" styles={{ width: '100%' }}>
<Button
leftIcon={<IconFilter size={16} />}
onClick={() => filterDrawerIsOpenVar(true)}
data-cy="filter-button">
Filter
</Button>
</MediaQuery>
</Group>
<TextField
label="Search for a character"
value={searchInput}
style={{ flex: 1 }}
my={24}
autoFocus
rightSection={
searchInput && (
<ActionIcon>
<IconX size={16} onClick={() => setSearchInput('')} />
</ActionIcon>
)
}
onKeyDown={(e) => {
if (e.key !== 'Escape') return
setSearchInput('')
}}
styles={{ rightSection: { cursor: 'pointer' } }}
onChange={(event) => setSearchInput(event.currentTarget.value)}
data-testid="search-input"
data-cy="search-bar"
/>
{charactersFound ? (
<>
<Grid data-testid="characters-container" data-cy="characters-container">
{loading ? (
<CustomLoading />
) : (
characters?.map((character) => (
<Grid.Col lg={4} xs={6} key={character?.id}>
<CharacterCard character={character} />
</Grid.Col>
))
)}
</Grid>
<Center style={{ marginTop: 24, marginBottom: 24 }}>
<MediaQuery smallerThan="sm" styles={{ display: 'none' }}>
<Pagination page={page} onChange={setPage} total={numberOfPages} size="xl" />
</MediaQuery>
<MediaQuery largerThan="sm" styles={{ display: 'none' }}>
<MobilePagination page={page} onChange={setPage} total={numberOfPages} />
</MediaQuery>
</Center>
</>
) : (
<Center style={{ display: 'flex' }}>
<IconZoomCancel />
<Text ml={5}>No characters found with current filters</Text>
</Center>
)}
</Container>
) : (
<Outlet />
)}
</Box>
</Box>
</View>
)
}
export default DashboardView