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"; import * as React from "react"; import ActionDialog from "@saleor/components/ActionDialog"; import useBulkActions from "@saleor/hooks/useBulkActions"; import useNavigator from "@saleor/hooks/useNavigator"; import useNotifier from "@saleor/hooks/useNotifier"; import usePaginator, { createPaginationState } from "@saleor/hooks/usePaginator"; import { PAGINATE_BY } from "../../config"; import i18n from "../../i18n"; import { getMutationState, maybe } from "../../misc"; 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 = ({ params }) => { const navigate = useNavigator(); const notify = useNotifier(); const paginate = usePaginator(); const { isSelected, listElements, reset, toggle, toggleAll } = useBulkActions( params.ids ); const closeModal = () => navigate( collectionListUrl({ ...params, action: undefined, ids: undefined }), true ); const openModal = (action: CollectionListUrlDialog, ids: string[]) => navigate( collectionListUrl({ action, ids }) ); const paginationState = createPaginationState(PAGINATE_BY, params); return ( {({ 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({ text: i18n.t("Removed collections") }); refetch(); reset(); closeModal(); } }; const handleCollectionBulkPublish = (data: CollectionBulkPublish) => { if (data.collectionBulkPublish.errors.length === 0) { notify({ text: i18n.t("Changed publication status") }); refetch(); reset(); closeModal(); } }; return ( {(collectionBulkDelete, collectionBulkDeleteOpts) => ( {(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 ( <> navigate(collectionAddUrl)} disabled={loading} collections={maybe(() => data.collections.edges.map(edge => edge.node) )} onNextPage={loadNextPage} onPreviousPage={loadPreviousPage} pageInfo={pageInfo} onRowClick={id => () => navigate(collectionUrl(id))} toolbar={ <> openModal("remove", listElements)} > } isChecked={isSelected} selected={listElements.length} toggle={toggle} toggleAll={toggleAll} /> collectionBulkPublish({ variables: { ids: params.ids, isPublished: true } }) } variant="default" title={i18n.t("Publish collections")} > {{ number }} collections?", { number: maybe( () => params.ids.length.toString(), "..." ) } ) }} /> collectionBulkPublish({ variables: { ids: params.ids, isPublished: false } }) } variant="default" title={i18n.t("Unpublish collections")} > {{ number }} collections?", { number: maybe( () => params.ids.length.toString(), "..." ) } ) }} /> collectionBulkDelete({ variables: { ids: params.ids } }) } variant="delete" title={i18n.t("Remove collections")} > {{ number }} collections?", { number: maybe( () => params.ids.length.toString(), "..." ) } ) }} /> ); }} )} ); }} ); }; export default CollectionList;