-
Notifications
You must be signed in to change notification settings - Fork 582
/
PagesDataList.tsx
336 lines (316 loc) · 13.5 KB
/
PagesDataList.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import React, { useCallback, useEffect, useMemo, useState } from "react";
import styled from "@emotion/styled";
import { i18n } from "@webiny/app/i18n";
import { useRouter } from "@webiny/react-router";
import { useQuery } from "@apollo/react-hooks";
import debounce from "lodash/debounce";
import get from "lodash/get";
import { LIST_PAGES } from "../../graphql/pages";
import TimeAgo from "timeago-react";
import {
DataList,
DataListModalOverlay,
DataListModalOverlayAction,
ListItem,
ListItemMeta,
ListItemText,
ListItemTextSecondary,
ListTextOverline,
ListSelectBox
} from "@webiny/ui/List";
import { Checkbox } from "@webiny/ui/Checkbox";
import { Typography } from "@webiny/ui/Typography";
import { css } from "emotion";
import { Form } from "@webiny/form";
import { Select } from "@webiny/ui/Select";
import { LIST_CATEGORIES } from "./../Categories/graphql";
import { Cell, Grid } from "@webiny/ui/Grid";
import { AutoComplete } from "@webiny/ui/AutoComplete";
import { Scrollbar } from "@webiny/ui/Scrollbar";
import statusesLabels from "../../constants/pageStatusesLabels";
import { ButtonIcon, ButtonSecondary } from "@webiny/ui/Button";
import { ReactComponent as AddIcon } from "@webiny/app-admin/assets/icons/add-18px.svg";
import { ReactComponent as FilterIcon } from "@webiny/app-admin/assets/icons/filter-24px.svg";
import SearchUI from "@webiny/app-admin/components/SearchUI";
import * as GQLCache from "~/admin/views/Pages/cache";
import { ReactComponent as FileUploadIcon } from "~/editor/plugins/defaultBar/components/icons/file_upload.svg";
import useImportPageDialog from "~/editor/plugins/defaultBar/components/ImportPageButton/useImportPageDialog";
import { useMultiSelect } from "~/admin/views/Pages/hooks/useMultiSelect";
import { ExportPagesButton } from "~/editor/plugins/defaultBar/components/ExportPageButton";
const t = i18n.ns("app-page-builder/admin/pages/data-list");
const rightAlign = css({
alignItems: "flex-end !important"
});
const InlineLoaderWrapper = styled("div")({
position: "absolute",
bottom: 0,
left: 0,
display: "flex",
justifyContent: "center",
alignItems: "center",
width: "100%",
height: 40,
backgroundColor: "var(--mdc-theme-surface)"
});
const Actions = styled("div")({
display: "flex",
justifyContent: "space-between"
});
const SORTERS = [
{
label: t`Newest to oldest`,
sort: "createdOn_DESC"
},
{
label: t`Oldest to newest`,
sort: "createdOn_ASC"
},
{
label: t`Title A-Z`,
sort: "title_ASC"
},
{
label: t`Title Z-A`,
sort: "title_DESC"
}
];
type PagesDataListProps = {
onCreatePage: (event?: React.SyntheticEvent) => void;
canCreate: boolean;
onImportPage: (event?: React.SyntheticEvent) => void;
};
const PagesDataList = ({ onCreatePage, canCreate, onImportPage }: PagesDataListProps) => {
const [filter, setFilter] = useState("");
const { history, location } = useRouter();
const query = new URLSearchParams(location.search);
const [fetchMoreLoading, setFetchMoreLoading] = useState(false);
const [where, setWhere] = useState({});
const [sort, setSort] = useState<string>(SORTERS[0].sort);
const search = {
query: query.get("search") || undefined
};
const setSearchParams = useMemo(() => {
return debounce(({ filter, location }) => {
const params = new URLSearchParams(location.search);
if (filter) {
params.set("search", filter);
} else {
params.delete("search");
}
// Update search in URL.
history.push(`${location.pathname}?${params.toString()}`);
}, 250);
}, []);
// Update "search" param whenever filter is changed
useEffect(() => {
setSearchParams({
filter,
location
});
}, [filter]);
const variables = {
search,
sort,
where
};
const listQuery = useQuery(LIST_PAGES, {
variables
});
GQLCache.writePageListVariablesToLocalStorage(variables);
const listPagesData = get(listQuery, "data.pageBuilder.listPages.data", []);
const selectedPageId = new URLSearchParams(location.search).get("id");
const categoriesQuery = useQuery(LIST_CATEGORIES);
const categoriesData = get(categoriesQuery, "data.pageBuilder.listCategories.data", []);
const loading = [listQuery].find(item => item.loading);
// Load more pages on page list scroll
const loadMoreOnScroll = useCallback(
debounce(({ scrollFrame, fetchMore }) => {
if (scrollFrame.top > 0.9) {
const meta = get(listQuery, "data.pageBuilder.listPages.meta", {});
if (meta.cursor) {
setFetchMoreLoading(true);
fetchMore({
variables: { after: meta.cursor },
updateQuery: (prev, { fetchMoreResult }) => {
if (!fetchMoreResult) {
return prev;
}
const next = { ...fetchMoreResult };
next.pageBuilder.listPages.data = [
...prev.pageBuilder.listPages.data,
...fetchMoreResult.pageBuilder.listPages.data
];
setFetchMoreLoading(false);
return next;
}
});
}
}
}, 500),
[listQuery]
);
const pagesDataListModalOverlay = useMemo(
() => (
<DataListModalOverlay>
<Form
data={{ ...where, sort }}
onChange={({ status, category, sort }) => {
// Update "where" filter.
const where = { category, status: undefined };
if (status !== "all") {
where.status = status;
}
setWhere(where);
// Update "sort".
if (typeof sort === "string") {
setSort(sort);
}
}}
>
{({ Bind }) => (
<Grid>
<Cell span={12}>
<Bind name={"category"}>
<AutoComplete
description={"Filter by a specific category."}
label={t`Filter by category`}
options={categoriesData.map(item => ({
id: item.slug,
name: item.name
}))}
/>
</Bind>
</Cell>
<Cell span={12}>
<Bind name={"status"}>
<Select
label={t`Filter by status`}
description={"Filter by a specific page status."}
>
<option value={"all"}>{t`All`}</option>
<option value={"draft"}>{t`Draft`}</option>
<option value={"published"}>{t`Published`}</option>
<option value={"unpublished"}>{t`Unpublished`}</option>
<option
value={"reviewRequested"}
>{t`Review requested`}</option>
<option
value={"changesRequested"}
>{t`Changes requested`}</option>
</Select>
</Bind>
</Cell>
<Cell span={12}>
<Bind name={"sort"}>
<Select label={t`Sort by`} description={"Sort pages by"}>
{SORTERS.map(({ label, sort: value }) => {
return (
<option key={label} value={value}>
{label}
</option>
);
})}
</Select>
</Bind>
</Cell>
</Grid>
)}
</Form>
</DataListModalOverlay>
),
[categoriesData, where, sort]
);
const { showImportPageDialog } = useImportPageDialog();
const listActions = useMemo(() => {
if (!canCreate) {
return null;
}
return (
<Actions>
<ButtonSecondary data-testid="import-page-button" onClick={onImportPage}>
<ButtonIcon icon={<FileUploadIcon />} /> {t`Import Page`}
</ButtonSecondary>
<ButtonSecondary data-testid="new-record-button" onClick={onCreatePage}>
<ButtonIcon icon={<AddIcon />} /> {t`New Page`}
</ButtonSecondary>
</Actions>
);
}, [canCreate, showImportPageDialog]);
const multiSelectProps = useMultiSelect({ useRouter: false, getValue: item => item.pid });
return (
<DataList
title={t`Pages`}
loading={Boolean(loading)}
actions={listActions}
data={listPagesData}
search={
<SearchUI value={filter} onChange={setFilter} inputPlaceholder={t`Search pages`} />
}
modalOverlay={pagesDataListModalOverlay}
modalOverlayAction={
<DataListModalOverlayAction
icon={<FilterIcon />}
data-testid={"default-data-list.filter"}
/>
}
multiSelectActions={
<ExportPagesButton getMultiSelected={multiSelectProps.getMultiSelected} />
}
multiSelectAll={multiSelectProps.multiSelectAll}
isAllMultiSelected={multiSelectProps.isAllMultiSelected}
isNoneMultiSelected={multiSelectProps.isNoneMultiSelected}
>
{({ data }) => (
<>
<Scrollbar
data-testid="default-data-list"
onScrollFrame={scrollFrame =>
loadMoreOnScroll({ scrollFrame, fetchMore: listQuery.fetchMore })
}
>
{Array.isArray(data) &&
data.map(page => (
<ListItem key={page.id} selected={page.id === selectedPageId}>
<ListSelectBox>
<Checkbox
onChange={() => multiSelectProps.multiSelect(page)}
value={multiSelectProps.isMultiSelected(page)}
/>
</ListSelectBox>
<ListItemText
data-testid={"pages-default-data-list.select-page"}
onClick={() => {
query.set("id", page.id);
history.push({ search: query.toString() });
}}
>
{page.title}
<ListTextOverline>
{page.category?.name || t`Unknown category`}
</ListTextOverline>
{page.createdBy && (
<ListItemTextSecondary>
Created by: {page.createdBy.displayName || "N/A"}.
Last modified: <TimeAgo datetime={page.savedOn} />.
</ListItemTextSecondary>
)}
</ListItemText>
<ListItemMeta className={rightAlign}>
<Typography use={"subtitle2"}>
{`${statusesLabels[page.status]} (v${page.version})`}
</Typography>
</ListItemMeta>
</ListItem>
))}
</Scrollbar>
{fetchMoreLoading && (
<InlineLoaderWrapper>
<Typography use={"overline"}>{t`Loading more pages...`}</Typography>
</InlineLoaderWrapper>
)}
</>
)}
</DataList>
);
};
export default PagesDataList;