-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathPostList.tsx
216 lines (199 loc) · 5.62 KB
/
PostList.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
import * as React from 'react';
import { Fragment, memo } from 'react';
import BookIcon from '@mui/icons-material/Book';
import { Box, Chip, useMediaQuery } from '@mui/material';
import { Theme, styled } from '@mui/material/styles';
import lodashGet from 'lodash/get';
import jsonExport from 'jsonexport/dist';
import {
BooleanField,
BulkDeleteButton,
BulkExportButton,
ChipField,
SelectColumnsButton,
CreateButton,
DatagridConfigurable,
DateField,
downloadCSV,
EditButton,
ExportButton,
FilterButton,
List,
InfiniteList,
NumberField,
ReferenceArrayField,
ReferenceManyCount,
SearchInput,
ShowButton,
SimpleList,
SingleFieldList,
TextField,
TextInput,
TopToolbar,
useTranslate,
} from 'react-admin'; // eslint-disable-line import/no-unresolved
import ResetViewsButton from './ResetViewsButton';
export const PostIcon = BookIcon;
const QuickFilter = ({
label,
}: {
label?: string;
source?: string;
defaultValue?: any;
}) => {
const translate = useTranslate();
return <Chip sx={{ marginBottom: 1 }} label={translate(label)} />;
};
const postFilter = [
<SearchInput source="q" alwaysOn />,
<TextInput source="title" defaultValue="Qui tempore rerum et voluptates" />,
<QuickFilter
label="resources.posts.fields.commentable"
source="commentable"
defaultValue
/>,
];
const exporter = posts => {
const data = posts.map(post => ({
...post,
backlinks: lodashGet(post, 'backlinks', []).map(
backlink => backlink.url
),
}));
return jsonExport(data, (err, csv) => downloadCSV(csv, 'posts'));
};
const PostListMobileActions = () => (
<TopToolbar>
<FilterButton />
<CreateButton />
<ExportButton />
</TopToolbar>
);
const PostListMobile = () => (
<InfiniteList
filters={postFilter}
sort={{ field: 'published_at', order: 'DESC' }}
exporter={exporter}
actions={<PostListMobileActions />}
>
<SimpleList
primaryText={record => record.title}
secondaryText={record => `${record.views} views`}
tertiaryText={record =>
new Date(record.published_at).toLocaleDateString()
}
/>
</InfiniteList>
);
const StyledDatagrid = styled(DatagridConfigurable)(({ theme }) => ({
'& .title': {
maxWidth: '16em',
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
},
'& .hiddenOnSmallScreens': {
[theme.breakpoints.down('lg')]: {
display: 'none',
},
},
'& .column-tags': {
minWidth: '9em',
},
'& .publishedAt': { fontStyle: 'italic' },
}));
const PostListBulkActions = memo(
({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
children,
...props
}) => (
<Fragment>
<ResetViewsButton {...props} />
<BulkDeleteButton {...props} />
<BulkExportButton {...props} />
</Fragment>
)
);
const PostListActions = () => (
<TopToolbar>
<SelectColumnsButton />
<FilterButton />
<CreateButton />
<ExportButton />
</TopToolbar>
);
const PostListActionToolbar = ({ children }) => (
<Box sx={{ alignItems: 'center', display: 'flex' }}>{children}</Box>
);
const rowClick = (_id, _resource, record) => {
if (record.commentable) {
return 'edit';
}
return 'show';
};
const PostPanel = ({ record }) => (
<div dangerouslySetInnerHTML={{ __html: record.body }} />
);
const tagSort = { field: 'name.en', order: 'ASC' };
const PostListDesktop = () => (
<List
filters={postFilter}
sort={{ field: 'published_at', order: 'DESC' }}
exporter={exporter}
actions={<PostListActions />}
>
<StyledDatagrid
bulkActionButtons={<PostListBulkActions />}
rowClick={rowClick}
expand={PostPanel}
omit={['average_note']}
>
<TextField source="id" />
<TextField source="title" cellClassName="title" />
<DateField
source="published_at"
sortByOrder="DESC"
cellClassName="publishedAt"
/>
<ReferenceManyCount
label="resources.posts.fields.nb_comments"
reference="comments"
target="post_id"
link
/>
<BooleanField
source="commentable"
label="resources.posts.fields.commentable_short"
sortable={false}
/>
<NumberField source="views" sortByOrder="DESC" />
<ReferenceArrayField
label="Tags"
reference="tags"
source="tags"
sortBy="tags.name"
sort={tagSort}
cellClassName="hiddenOnSmallScreens"
headerClassName="hiddenOnSmallScreens"
>
<SingleFieldList>
<ChipField clickable source="name.en" size="small" />
</SingleFieldList>
</ReferenceArrayField>
<NumberField source="average_note" />
<PostListActionToolbar>
<EditButton />
<ShowButton />
</PostListActionToolbar>
</StyledDatagrid>
</List>
);
const PostList = () => {
const isSmall = useMediaQuery<Theme>(
theme => theme.breakpoints.down('md'),
{ noSsr: true }
);
return isSmall ? <PostListMobile /> : <PostListDesktop />;
};
export default PostList;