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-21 12:31:55 +00:00
|
|
|
import { FormattedMessage, useIntl } from "react-intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
import ActionDialog from "@saleor/components/ActionDialog";
|
|
|
|
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-21 12:31:55 +00:00
|
|
|
import { commonMessages } from "@saleor/intl";
|
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 CollectionListPage from "../components/CollectionListPage/CollectionListPage";
|
|
|
|
import {
|
|
|
|
TypedCollectionBulkDelete,
|
|
|
|
TypedCollectionBulkPublish
|
|
|
|
} from "../mutations";
|
|
|
|
import { TypedCollectionListQuery } from "../queries";
|
|
|
|
import { CollectionBulkDelete } from "../types/CollectionBulkDelete";
|
|
|
|
import { CollectionBulkPublish } from "../types/CollectionBulkPublish";
|
|
|
|
import {
|
|
|
|
collectionAddUrl,
|
|
|
|
collectionListUrl,
|
|
|
|
CollectionListUrlDialog,
|
|
|
|
CollectionListUrlQueryParams,
|
|
|
|
collectionUrl
|
|
|
|
} from "../urls";
|
|
|
|
|
|
|
|
interface CollectionListProps {
|
|
|
|
params: CollectionListUrlQueryParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const CollectionList: React.StatelessComponent<CollectionListProps> = ({
|
|
|
|
params
|
|
|
|
}) => {
|
|
|
|
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.COLLECTION_LIST
|
|
|
|
);
|
2019-08-21 12:31:55 +00:00
|
|
|
const intl = useIntl();
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
const closeModal = () =>
|
|
|
|
navigate(
|
|
|
|
collectionListUrl({
|
|
|
|
...params,
|
|
|
|
action: undefined,
|
|
|
|
ids: undefined
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
const openModal = (action: CollectionListUrlDialog, ids: string[]) =>
|
|
|
|
navigate(
|
|
|
|
collectionListUrl({
|
|
|
|
action,
|
|
|
|
ids
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
const paginationState = createPaginationState(settings.rowNumber, params);
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
|
|
|
<TypedCollectionListQuery displayLoader variables={paginationState}>
|
|
|
|
{({ data, loading, refetch }) => {
|
|
|
|
const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
|
|
|
|
maybe(() => data.collections.pageInfo),
|
|
|
|
paginationState,
|
|
|
|
params
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleCollectionBulkDelete = (data: CollectionBulkDelete) => {
|
|
|
|
if (data.collectionBulkDelete.errors.length === 0) {
|
|
|
|
notify({
|
2019-08-21 12:31:55 +00:00
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
refetch();
|
|
|
|
reset();
|
|
|
|
closeModal();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleCollectionBulkPublish = (data: CollectionBulkPublish) => {
|
|
|
|
if (data.collectionBulkPublish.errors.length === 0) {
|
|
|
|
notify({
|
2019-08-21 12:31:55 +00:00
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
refetch();
|
|
|
|
reset();
|
|
|
|
closeModal();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TypedCollectionBulkDelete onCompleted={handleCollectionBulkDelete}>
|
|
|
|
{(collectionBulkDelete, collectionBulkDeleteOpts) => (
|
|
|
|
<TypedCollectionBulkPublish
|
|
|
|
onCompleted={handleCollectionBulkPublish}
|
|
|
|
>
|
|
|
|
{(collectionBulkPublish, collectionBulkPublishOpts) => {
|
|
|
|
const bulkDeleteTransitionState = getMutationState(
|
|
|
|
collectionBulkDeleteOpts.called,
|
|
|
|
collectionBulkDeleteOpts.loading,
|
|
|
|
maybe(
|
|
|
|
() =>
|
|
|
|
collectionBulkDeleteOpts.data.collectionBulkDelete
|
|
|
|
.errors
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
const bulkPublishTransitionState = getMutationState(
|
|
|
|
collectionBulkPublishOpts.called,
|
|
|
|
collectionBulkPublishOpts.loading,
|
|
|
|
maybe(
|
|
|
|
() =>
|
|
|
|
collectionBulkPublishOpts.data.collectionBulkPublish
|
|
|
|
.errors
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<CollectionListPage
|
|
|
|
onAdd={() => navigate(collectionAddUrl)}
|
|
|
|
disabled={loading}
|
|
|
|
collections={maybe(() =>
|
|
|
|
data.collections.edges.map(edge => edge.node)
|
|
|
|
)}
|
2019-08-09 11:14:35 +00:00
|
|
|
settings={settings}
|
2019-06-19 14:40:52 +00:00
|
|
|
onNextPage={loadNextPage}
|
|
|
|
onPreviousPage={loadPreviousPage}
|
2019-08-09 11:14:35 +00:00
|
|
|
onUpdateListSettings={updateListSettings}
|
2019-06-19 14:40:52 +00:00
|
|
|
pageInfo={pageInfo}
|
|
|
|
onRowClick={id => () => navigate(collectionUrl(id))}
|
|
|
|
toolbar={
|
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
color="primary"
|
|
|
|
onClick={() =>
|
|
|
|
openModal("unpublish", listElements)
|
|
|
|
}
|
|
|
|
>
|
2019-08-21 12:31:55 +00:00
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Unpublish"
|
|
|
|
description="unpublish collections"
|
|
|
|
id="collectionListUnpublish"
|
|
|
|
/>
|
2019-06-19 14:40:52 +00:00
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
color="primary"
|
|
|
|
onClick={() => openModal("publish", listElements)}
|
|
|
|
>
|
2019-08-21 12:31:55 +00:00
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Publish"
|
|
|
|
description="publish collections"
|
|
|
|
id="collectionListPublish"
|
|
|
|
/>
|
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
|
2019-08-21 12:31:55 +00:00
|
|
|
open={
|
|
|
|
params.action === "publish" &&
|
|
|
|
maybe(() => params.ids.length > 0)
|
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
onClose={closeModal}
|
|
|
|
confirmButtonState={bulkPublishTransitionState}
|
|
|
|
onConfirm={() =>
|
|
|
|
collectionBulkPublish({
|
|
|
|
variables: {
|
|
|
|
ids: params.ids,
|
|
|
|
isPublished: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
variant="default"
|
2019-08-21 12:31:55 +00:00
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Publish collections",
|
|
|
|
description: "dialog title",
|
|
|
|
id: "collectionListPublishCollectionsDialogTitle"
|
|
|
|
})}
|
2019-06-19 14:40:52 +00:00
|
|
|
>
|
2019-08-21 12:31:55 +00:00
|
|
|
<DialogContentText>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Are you sure you want to publish {counter, plural,
|
|
|
|
one {this collection}
|
|
|
|
other {{displayQuantity} collections}
|
|
|
|
}?"
|
|
|
|
id="collectionListPublishCollectionsDialogContent"
|
|
|
|
values={{
|
2019-08-21 12:44:29 +00:00
|
|
|
counter: maybe(() => params.ids.length),
|
|
|
|
displayQuantity: (
|
|
|
|
<strong>
|
|
|
|
{maybe(() => params.ids.length)}
|
|
|
|
</strong>
|
|
|
|
)
|
2019-08-21 12:31:55 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</DialogContentText>
|
2019-06-19 14:40:52 +00:00
|
|
|
</ActionDialog>
|
|
|
|
<ActionDialog
|
2019-08-21 12:31:55 +00:00
|
|
|
open={
|
|
|
|
params.action === "unpublish" &&
|
|
|
|
maybe(() => params.ids.length > 0)
|
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
onClose={closeModal}
|
|
|
|
confirmButtonState={bulkPublishTransitionState}
|
|
|
|
onConfirm={() =>
|
|
|
|
collectionBulkPublish({
|
|
|
|
variables: {
|
|
|
|
ids: params.ids,
|
|
|
|
isPublished: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
variant="default"
|
2019-08-21 12:31:55 +00:00
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Unpublish collections",
|
|
|
|
description: "dialog title",
|
|
|
|
id: "collectionListUnpublishCollectionsDialogTitle"
|
|
|
|
})}
|
2019-06-19 14:40:52 +00:00
|
|
|
>
|
2019-08-21 12:31:55 +00:00
|
|
|
<DialogContentText>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Are you sure you want to unpublish {counter, plural,
|
|
|
|
one {this collection}
|
|
|
|
other {{displayQuantity} collections}
|
|
|
|
}?"
|
|
|
|
id="collectionListUnpublishCollectionsDialogContent"
|
|
|
|
values={{
|
2019-08-21 12:44:29 +00:00
|
|
|
counter: maybe(() => params.ids.length),
|
|
|
|
displayQuantity: (
|
|
|
|
<strong>
|
|
|
|
{maybe(() => params.ids.length)}
|
|
|
|
</strong>
|
|
|
|
)
|
2019-08-21 12:31:55 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</DialogContentText>
|
2019-06-19 14:40:52 +00:00
|
|
|
</ActionDialog>
|
|
|
|
<ActionDialog
|
2019-08-21 12:31:55 +00:00
|
|
|
open={
|
|
|
|
params.action === "remove" &&
|
|
|
|
maybe(() => params.ids.length > 0)
|
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
onClose={closeModal}
|
|
|
|
confirmButtonState={bulkDeleteTransitionState}
|
|
|
|
onConfirm={() =>
|
|
|
|
collectionBulkDelete({
|
|
|
|
variables: {
|
|
|
|
ids: params.ids
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
variant="delete"
|
2019-08-21 12:31:55 +00:00
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Delete collections",
|
|
|
|
description: "dialog title",
|
|
|
|
id: "collectionListDeleteCollectionsDialogTitle"
|
|
|
|
})}
|
2019-06-19 14:40:52 +00:00
|
|
|
>
|
2019-08-21 12:31:55 +00:00
|
|
|
<DialogContentText>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Are you sure you want to delete {counter, plural,
|
|
|
|
one {this collection}
|
|
|
|
other {{displayQuantity} collections}
|
|
|
|
}?"
|
|
|
|
id="collectionListDeleteCollectionsDialogContent"
|
|
|
|
values={{
|
2019-08-21 12:44:29 +00:00
|
|
|
counter: maybe(() => params.ids.length),
|
|
|
|
displayQuantity: (
|
|
|
|
<strong>
|
|
|
|
{maybe(() => params.ids.length)}
|
|
|
|
</strong>
|
|
|
|
)
|
2019-08-21 12:31:55 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</DialogContentText>
|
2019-06-19 14:40:52 +00:00
|
|
|
</ActionDialog>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedCollectionBulkPublish>
|
|
|
|
)}
|
|
|
|
</TypedCollectionBulkDelete>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedCollectionListQuery>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default CollectionList;
|