forked from CyberAgentHack/web-speed-hackathon-2024
/
index.tsx
217 lines (200 loc) · 6.35 KB
/
index.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import {
Button,
Divider,
Flex,
Input,
Radio,
RadioGroup,
Spacer,
Stack,
StackItem,
Table,
TableContainer,
Tbody,
Td,
Text,
Th,
Thead,
Tr,
} from '@chakra-ui/react';
import { useId, useMemo, useState } from 'react';
import { useForm, useWatch } from 'react-hook-form';
import { useBookList } from '../../features/books/hooks/useBookList';
import { isContains } from '../../lib/filter/isContains';
import { BookDetailModal } from './internal/BookDetailModal';
import { CreateBookModal } from './internal/CreateBookModal';
const BookSearchKind = {
AuthorId: 'AuthorId',
AuthorName: 'AuthorName',
BookId: 'BookId',
BookName: 'BookName',
} as const;
type BookSearchKind = (typeof BookSearchKind)[keyof typeof BookSearchKind];
const BookModalMode = {
Create: 'Create',
Detail: 'Detail',
None: 'None',
} as const;
type BookModalMode = (typeof BookModalMode)[keyof typeof BookModalMode];
type BookModalState =
| {
mode: typeof BookModalMode.None;
params: object;
}
| {
mode: typeof BookModalMode.Detail;
params: { bookId: string };
}
| {
mode: typeof BookModalMode.Create;
params: object;
};
type BookModalAction = {
close: () => void;
openCreate: () => void;
openDetail: (bookId: string) => void;
};
export const BookListPage: React.FC = () => {
const { data: bookList = [] } = useBookList();
const bookListA11yId = useId();
const form = useForm({
defaultValues: {
kind: BookSearchKind.BookId as BookSearchKind,
query: '',
},
});
const values = useWatch({ control: form.control });
const filteredBookList = useMemo(() => {
if (values.query === '') {
return bookList;
}
switch (values.kind) {
case BookSearchKind.BookId: {
return bookList.filter((book) => book.id === values.query);
}
case BookSearchKind.BookName: {
return bookList.filter((book) => {
return (
isContains({ query: values.query ?? '', target: book.name }) ||
isContains({ query: values.query ?? '', target: book.nameRuby })
);
});
}
case BookSearchKind.AuthorId: {
return bookList.filter((book) => book.author.id === values.query);
}
case BookSearchKind.AuthorName: {
return bookList.filter((book) => {
return isContains({ query: values.query ?? '', target: book.author.name });
});
}
case undefined:
return bookList;
default: {
values.kind satisfies never;
return bookList;
}
}
}, [values, bookList]);
const [modal, setModal] = useState<BookModalState>({
mode: BookModalMode.None,
params: {},
});
const modalMethod: BookModalAction = {
close() {
setModal({ mode: BookModalMode.None, params: {} });
},
openCreate() {
setModal({ mode: BookModalMode.Create, params: {} });
},
openDetail(bookId) {
setModal({ mode: BookModalMode.Detail, params: { bookId } });
},
};
return (
<>
<Stack height="100%" p={4} spacing={6}>
<StackItem aria-label="検索セクション" as="section">
<RadioGroup name="kind" value={values.kind}>
<Stack direction="row" spacing={4}>
<Radio color="gray.400" colorScheme="teal" {...form.register('kind')} value={BookSearchKind.BookId}>
作品 ID
</Radio>
<Radio color="gray.400" colorScheme="teal" {...form.register('kind')} value={BookSearchKind.BookName}>
作品名
</Radio>
<Radio color="gray.400" colorScheme="teal" value={BookSearchKind.AuthorId}>
作者 ID
</Radio>
<Radio color="gray.400" colorScheme="teal" value={BookSearchKind.AuthorName}>
作者名
</Radio>
</Stack>
</RadioGroup>
<Spacer height={2} />
<Flex gap={2}>
<Input borderColor="gray.400" {...form.register('query')} placeholder="条件を入力" />
</Flex>
</StackItem>
<Divider />
<StackItem
aria-labelledby={bookListA11yId}
as="section"
display="flex"
flexBasis={0}
flexDirection="column"
flexGrow={1}
flexShrink={1}
overflow="hidden"
>
<Flex align="center" justify="space-between">
<Text as="h2" fontSize="xl" fontWeight="bold" id={bookListA11yId}>
作品一覧
</Text>
<Button colorScheme="teal" onClick={() => modalMethod.openCreate()} variant="solid">
作品を追加
</Button>
</Flex>
<TableContainer flexGrow={1} flexShrink={1} overflowY="auto">
<Table variant="striped">
<Thead backgroundColor="white" position="sticky" top={0} zIndex={1}>
<Tr>
<Th w={120}></Th>
<Th>作品名</Th>
<Th>作者名</Th>
</Tr>
</Thead>
<Tbody>
{filteredBookList.map((book) => (
<Tr key={book.id}>
<Td textAlign="center" verticalAlign="middle">
<Button colorScheme="teal" onClick={() => modalMethod.openDetail(book.id)} variant="solid">
詳細
</Button>
</Td>
<Td verticalAlign="middle">
<Text fontWeight="bold">{book.name}</Text>
<Text color="gray.400" fontSize="small">
{book.id}
</Text>
</Td>
<Td verticalAlign="middle">
<Text fontWeight="bold">{book.author.name}</Text>
<Text color="gray.400" fontSize="small">
{book.author.id}
</Text>
</Td>
</Tr>
))}
</Tbody>
</Table>
</TableContainer>
</StackItem>
</Stack>
{modal.mode === BookModalMode.Detail ? (
<BookDetailModal isOpen bookId={modal.params.bookId} onClose={() => modalMethod.close()} />
) : null}
{modal.mode === BookModalMode.Create ? <CreateBookModal isOpen onClose={() => modalMethod.close()} /> : null}
</>
);
};