-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameLibrary.page.tsx
202 lines (194 loc) · 5.05 KB
/
GameLibrary.page.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import Box from '@mui/material/Box';
import Chip from '@mui/material/Chip';
import FormControlLabel from '@mui/material/FormControlLabel';
import Grid from '@mui/material/Grid';
import Pagination from '@mui/material/Pagination';
import Paper from '@mui/material/Paper';
import Radio from '@mui/material/Radio';
import RadioGroup from '@mui/material/RadioGroup';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { useMemo } from 'react';
import { AddGameLibraryFormDialog } from './AddGameLibrary.form';
import GameListProvider, { Game, useGameList } from './GameList.provider';
function GameLibraryListItem({ game }: { game: Game }) {
return (
<Stack
alignItems={'center'}
component={'li'}
direction={'row'}
justifyContent={'space-between'}
sx={{
':hover': {
backgroundColor: 'grey.400',
border: '1px solid',
borderColor: 'primary.main',
},
backgroundColor: 'background.default',
borderRadius: '1rem',
height: '10.4rem',
listStyleType: 'none',
overflow: 'hidden',
pr: 2,
}}
>
<Stack alignItems={'center'} direction={'row'}>
<Box
component={'img'}
src={game.boxArtImageUrl}
sx={{
height: 1,
objectFit: 'fill',
width: '8rem',
}}
/>
<Stack spacing={1} sx={{ ml: 2, width: '16rem' }}>
<Typography
sx={{
textTransform: 'uppercase',
whiteSpace: 'nowrap',
}}
>
{game.name}
</Typography>
<Chip
label={game.platform}
sx={{
textTransform: 'uppercase',
}}
/>
</Stack>
</Stack>
<Box
sx={{
display: { sm: 'block', xs: 'none' },
}}
>
<Chip
label={game.publisher}
sx={{
textTransform: 'uppercase',
width: '16rem',
}}
/>
</Box>
</Stack>
);
}
function GameLibraryFilter() {
const { platformFilter, setFilter } = useGameList();
return (
<Stack>
<Typography variant={'h3'}>Filter</Typography>
<RadioGroup onChange={setFilter} value={platformFilter}>
<FormControlLabel
control={<Radio color="default" />}
label="PS4"
value="PS4"
/>
<FormControlLabel
control={<Radio color="default" />}
label="PS5"
value="PS5"
/>
<FormControlLabel
control={<Radio color="default" />}
label="ALL"
value="ALL"
/>
</RadioGroup>
</Stack>
);
}
function GameLibraryList() {
const { currentPage, data, error, loading, setPage, totalPage } =
useGameList();
const records = useMemo(() => {
if (loading || error || !data) return [];
return data.gameList.edges.map(edge => edge.node);
}, [data, error, loading]);
return (
<>
<Stack
component={'ul'}
spacing={2}
sx={{
m: 0,
mr: 2,
p: 0,
}}
>
{records.map(record => (
<GameLibraryListItem game={record} key={record.id} />
))}
</Stack>
<Stack
direction={'row'}
justifyContent={'center'}
sx={{
mb: 2,
mt: 2,
}}
>
<Pagination count={totalPage} onChange={setPage} page={currentPage} />
</Stack>
</>
);
}
function GameListHeading() {
const { data, error, loading, platformFilter, variables } = useGameList();
const { from, to, totalCount } = useMemo(() => {
if (loading || error || !data || !variables)
return { from: 0, to: 0, totalCount: 0 };
const offset = variables?.offset;
const totalCount = data.gameList.totalCount;
return {
from: totalCount > 0 ? offset + 1 : 0,
to: offset + data.gameList.edges.length,
totalCount: totalCount,
};
}, [data, error, loading, variables]);
return (
<Typography
sx={{
mb: 3,
mt: 0,
}}
variant={'h4'}
>
{totalCount === 0
? `Click Add Game to your library for add record to ${platformFilter} platform`
: `Saved ( ${from} - ${to} of ${totalCount}) on ${platformFilter} platform`}
</Typography>
);
}
export default function GameLibraryPage() {
return (
<Paper
sx={{ minHeight: '100%', pt: 2, px: { lg: 8, md: 4, sm: 2, xs: 1 } }}
>
<GameListProvider>
<Grid container>
<Grid item md={8} xs={10}>
<GameListHeading />
</Grid>
<Grid item md={4} xs={2}>
<Stack
sx={{
mt: 1,
}}
>
<AddGameLibraryFormDialog />
</Stack>
</Grid>
<Grid item md={8} xs={10}>
<GameLibraryList />
</Grid>
<Grid item md={4} xs={2}>
<GameLibraryFilter />
</Grid>
</Grid>
</GameListProvider>
</Paper>
);
}