2019-06-19 14:40:52 +00:00
|
|
|
import Button from "@material-ui/core/Button";
|
|
|
|
import DialogContentText from "@material-ui/core/DialogContentText";
|
|
|
|
import IconButton from "@material-ui/core/IconButton";
|
|
|
|
import DeleteIcon from "@material-ui/icons/Delete";
|
2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-08-26 17:48:13 +00:00
|
|
|
import { FormattedMessage, useIntl } from "react-intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
import ActionDialog from "@saleor/components/ActionDialog";
|
2019-08-09 11:14:35 +00:00
|
|
|
import { configurationMenuUrl } from "@saleor/configuration";
|
2019-06-19 14:40:52 +00:00
|
|
|
import useBulkActions from "@saleor/hooks/useBulkActions";
|
2019-08-09 11:14:35 +00:00
|
|
|
import useListSettings from "@saleor/hooks/useListSettings";
|
2019-06-19 14:40:52 +00:00
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
|
|
|
import usePaginator, {
|
|
|
|
createPaginationState
|
|
|
|
} from "@saleor/hooks/usePaginator";
|
2019-08-09 11:14:35 +00:00
|
|
|
import { getMutationState, maybe } from "@saleor/misc";
|
|
|
|
import { ListViews } from "@saleor/types";
|
2019-06-19 14:40:52 +00:00
|
|
|
import PageListPage from "../components/PageListPage/PageListPage";
|
|
|
|
import { TypedPageBulkPublish, TypedPageBulkRemove } from "../mutations";
|
|
|
|
import { TypedPageListQuery } from "../queries";
|
|
|
|
import { PageBulkPublish } from "../types/PageBulkPublish";
|
|
|
|
import { PageBulkRemove } from "../types/PageBulkRemove";
|
|
|
|
import {
|
|
|
|
pageCreateUrl,
|
|
|
|
pageListUrl,
|
|
|
|
PageListUrlDialog,
|
|
|
|
PageListUrlQueryParams,
|
|
|
|
pageUrl
|
|
|
|
} from "../urls";
|
|
|
|
|
|
|
|
interface PageListProps {
|
|
|
|
params: PageListUrlQueryParams;
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
export const PageList: React.FC<PageListProps> = ({ params }) => {
|
2019-06-19 14:40:52 +00:00
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
|
|
|
const paginate = usePaginator();
|
|
|
|
const { isSelected, listElements, reset, toggle, toggleAll } = useBulkActions(
|
|
|
|
params.ids
|
|
|
|
);
|
2019-08-09 11:14:35 +00:00
|
|
|
const { updateListSettings, settings } = useListSettings(
|
|
|
|
ListViews.PAGES_LIST
|
|
|
|
);
|
2019-08-26 17:48:13 +00:00
|
|
|
const intl = useIntl();
|
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
const paginationState = createPaginationState(settings.rowNumber, params);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<TypedPageListQuery displayLoader variables={paginationState}>
|
|
|
|
{({ data, loading, refetch }) => {
|
|
|
|
const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
|
|
|
|
maybe(() => data.pages.pageInfo),
|
|
|
|
paginationState,
|
|
|
|
params
|
|
|
|
);
|
|
|
|
|
|
|
|
const closeModal = () =>
|
|
|
|
navigate(
|
|
|
|
pageListUrl({
|
|
|
|
...params,
|
|
|
|
action: undefined,
|
|
|
|
ids: undefined
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
const openModal = (action: PageListUrlDialog, ids: string[]) =>
|
|
|
|
navigate(
|
|
|
|
pageListUrl({
|
|
|
|
...params,
|
|
|
|
action,
|
|
|
|
ids
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const handlePageBulkPublish = (data: PageBulkPublish) => {
|
|
|
|
if (data.pageBulkPublish.errors.length === 0) {
|
|
|
|
closeModal();
|
|
|
|
notify({
|
2019-08-26 17:48:13 +00:00
|
|
|
text: intl.formatMessage({
|
|
|
|
defaultMessage: "Published pages",
|
|
|
|
description: "notification"
|
|
|
|
})
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
reset();
|
|
|
|
refetch();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handlePageBulkRemove = (data: PageBulkRemove) => {
|
|
|
|
if (data.pageBulkDelete.errors.length === 0) {
|
|
|
|
closeModal();
|
|
|
|
notify({
|
2019-08-26 17:48:13 +00:00
|
|
|
text: intl.formatMessage({
|
|
|
|
defaultMessage: "Removed pages",
|
|
|
|
description: "notification"
|
|
|
|
})
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
reset();
|
|
|
|
refetch();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TypedPageBulkRemove onCompleted={handlePageBulkRemove}>
|
|
|
|
{(bulkPageRemove, bulkPageRemoveOpts) => (
|
|
|
|
<TypedPageBulkPublish onCompleted={handlePageBulkPublish}>
|
|
|
|
{(bulkPagePublish, bulkPagePublishOpts) => {
|
|
|
|
const deleteTransitionState = getMutationState(
|
|
|
|
bulkPageRemoveOpts.called,
|
|
|
|
bulkPageRemoveOpts.loading,
|
|
|
|
maybe(() => bulkPageRemoveOpts.data.pageBulkDelete.errors)
|
|
|
|
);
|
|
|
|
|
|
|
|
const publishTransitionState = getMutationState(
|
|
|
|
bulkPagePublishOpts.called,
|
|
|
|
bulkPagePublishOpts.loading,
|
|
|
|
maybe(() => bulkPagePublishOpts.data.pageBulkPublish.errors)
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<PageListPage
|
|
|
|
disabled={loading}
|
2019-08-09 11:14:35 +00:00
|
|
|
settings={settings}
|
2019-06-19 14:40:52 +00:00
|
|
|
pages={maybe(() =>
|
|
|
|
data.pages.edges.map(edge => edge.node)
|
|
|
|
)}
|
|
|
|
pageInfo={pageInfo}
|
|
|
|
onAdd={() => navigate(pageCreateUrl)}
|
|
|
|
onBack={() => navigate(configurationMenuUrl)}
|
|
|
|
onNextPage={loadNextPage}
|
|
|
|
onPreviousPage={loadPreviousPage}
|
2019-08-09 11:14:35 +00:00
|
|
|
onUpdateListSettings={updateListSettings}
|
2019-06-19 14:40:52 +00:00
|
|
|
onRowClick={id => () => navigate(pageUrl(id))}
|
|
|
|
toolbar={
|
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
color="primary"
|
|
|
|
onClick={() =>
|
|
|
|
openModal("unpublish", listElements)
|
|
|
|
}
|
|
|
|
>
|
2019-08-26 17:48:13 +00:00
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Unpublish"
|
|
|
|
description="unpublish page, button"
|
|
|
|
/>
|
2019-06-19 14:40:52 +00:00
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
color="primary"
|
|
|
|
onClick={() => openModal("publish", listElements)}
|
|
|
|
>
|
2019-08-26 17:48:13 +00:00
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Publish"
|
|
|
|
description="publish page, button"
|
|
|
|
/>
|
2019-06-19 14:40:52 +00:00
|
|
|
</Button>
|
|
|
|
<IconButton
|
|
|
|
color="primary"
|
|
|
|
onClick={() => openModal("remove", listElements)}
|
|
|
|
>
|
|
|
|
<DeleteIcon />
|
|
|
|
</IconButton>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
isChecked={isSelected}
|
|
|
|
selected={listElements.length}
|
|
|
|
toggle={toggle}
|
|
|
|
toggleAll={toggleAll}
|
|
|
|
/>
|
|
|
|
<ActionDialog
|
|
|
|
open={params.action === "publish"}
|
|
|
|
onClose={closeModal}
|
|
|
|
confirmButtonState={publishTransitionState}
|
|
|
|
onConfirm={() =>
|
|
|
|
bulkPagePublish({
|
|
|
|
variables: {
|
|
|
|
ids: params.ids,
|
|
|
|
isPublished: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-08-26 17:48:13 +00:00
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Publish Pages",
|
|
|
|
description: "dialog header"
|
|
|
|
})}
|
2019-06-19 14:40:52 +00:00
|
|
|
>
|
2019-08-26 17:48:13 +00:00
|
|
|
<DialogContentText>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Are you sure you want to publish {counter, plural,
|
|
|
|
one {this page}
|
|
|
|
other {{displayQuantity} pages}
|
|
|
|
}?"
|
|
|
|
description="dialog content"
|
|
|
|
values={{
|
|
|
|
counter: maybe(() => params.ids.length),
|
|
|
|
displayQuantity: (
|
|
|
|
<strong>
|
|
|
|
{maybe(() => params.ids.length)}
|
|
|
|
</strong>
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</DialogContentText>
|
2019-06-19 14:40:52 +00:00
|
|
|
</ActionDialog>
|
|
|
|
<ActionDialog
|
|
|
|
open={params.action === "unpublish"}
|
|
|
|
onClose={closeModal}
|
|
|
|
confirmButtonState={publishTransitionState}
|
|
|
|
onConfirm={() =>
|
|
|
|
bulkPagePublish({
|
|
|
|
variables: {
|
|
|
|
ids: params.ids,
|
|
|
|
isPublished: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-08-26 17:48:13 +00:00
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Unpublish Pages",
|
|
|
|
description: "dialog header"
|
|
|
|
})}
|
2019-06-19 14:40:52 +00:00
|
|
|
>
|
2019-08-26 17:48:13 +00:00
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Are you sure you want to unpublish {counter, plural,
|
|
|
|
one {this page}
|
|
|
|
other {{displayQuantity} pages}
|
|
|
|
}?"
|
|
|
|
description="dialog content"
|
|
|
|
values={{
|
|
|
|
counter: maybe(() => params.ids.length),
|
|
|
|
displayQuantity: (
|
|
|
|
<strong>{maybe(() => params.ids.length)}</strong>
|
2019-06-19 14:40:52 +00:00
|
|
|
)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</ActionDialog>
|
|
|
|
<ActionDialog
|
|
|
|
open={params.action === "remove"}
|
|
|
|
onClose={closeModal}
|
|
|
|
confirmButtonState={deleteTransitionState}
|
|
|
|
onConfirm={() =>
|
|
|
|
bulkPageRemove({
|
|
|
|
variables: {
|
|
|
|
ids: params.ids
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
variant="delete"
|
2019-08-26 17:48:13 +00:00
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Delete Pages",
|
|
|
|
description: "dialog header"
|
|
|
|
})}
|
2019-06-19 14:40:52 +00:00
|
|
|
>
|
2019-08-26 17:48:13 +00:00
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Are you sure you want to delete {counter, plural,
|
|
|
|
one {this page}
|
|
|
|
other {{displayQuantity} pages}
|
|
|
|
}?"
|
|
|
|
description="dialog content"
|
|
|
|
values={{
|
|
|
|
counter: maybe(() => params.ids.length),
|
|
|
|
displayQuantity: (
|
|
|
|
<strong>{maybe(() => params.ids.length)}</strong>
|
2019-06-19 14:40:52 +00:00
|
|
|
)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</ActionDialog>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedPageBulkPublish>
|
|
|
|
)}
|
|
|
|
</TypedPageBulkRemove>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedPageListQuery>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PageList;
|