saleor-dashboard/src/collections/views/CollectionList/CollectionList.tsx

381 lines
13 KiB
TypeScript
Raw Normal View History

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";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import ActionDialog from "@saleor/components/ActionDialog";
2019-09-11 14:00:02 +00:00
import DeleteFilterTabDialog from "@saleor/components/DeleteFilterTabDialog";
import SaveFilterTabDialog, {
SaveFilterTabDialogFormData
} from "@saleor/components/SaveFilterTabDialog";
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";
import { commonMessages } from "@saleor/intl";
2019-12-06 17:11:46 +00:00
import { maybe } from "@saleor/misc";
2019-08-09 11:14:35 +00:00
import { ListViews } from "@saleor/types";
2019-12-17 17:13:56 +00:00
import { getSortParams } from "@saleor/utils/sort";
import createSortHandler from "@saleor/utils/handlers/sortHandler";
2019-12-06 14:58:28 +00:00
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
2020-01-07 13:34:45 +00:00
import { IFilter } from "@saleor/components/Filter";
import { getFilterQueryParams } from "@saleor/utils/filters";
import useShop from "@saleor/hooks/useShop";
2019-09-11 14:00:02 +00:00
import CollectionListPage from "../../components/CollectionListPage/CollectionListPage";
2019-06-19 14:40:52 +00:00
import {
TypedCollectionBulkDelete,
TypedCollectionBulkPublish
2019-09-11 14:00:02 +00:00
} from "../../mutations";
2019-12-17 17:13:56 +00:00
import { useCollectionListQuery } from "../../queries";
2019-09-11 14:00:02 +00:00
import { CollectionBulkDelete } from "../../types/CollectionBulkDelete";
import { CollectionBulkPublish } from "../../types/CollectionBulkPublish";
2019-06-19 14:40:52 +00:00
import {
collectionAddUrl,
collectionListUrl,
CollectionListUrlQueryParams,
2019-12-06 14:58:28 +00:00
collectionUrl,
CollectionListUrlDialog
2019-09-11 14:00:02 +00:00
} from "../../urls";
import {
areFiltersApplied,
deleteFilterTab,
getActiveFilters,
getFilterTabs,
getFilterVariables,
2020-01-07 13:34:45 +00:00
saveFilterTab,
CollectionFilterKeys,
getFilterQueryParam,
getFilterOpts
2019-09-11 14:00:02 +00:00
} from "./filter";
2019-12-17 17:13:56 +00:00
import { getSortQueryVariables } from "./sort";
2019-06-19 14:40:52 +00:00
interface CollectionListProps {
params: CollectionListUrlQueryParams;
}
export const CollectionList: React.FC<CollectionListProps> = ({ params }) => {
2019-06-19 14:40:52 +00:00
const navigate = useNavigator();
const notify = useNotifier();
const paginate = usePaginator();
2020-01-07 13:34:45 +00:00
const shop = useShop();
2019-06-19 14:40:52 +00:00
const { isSelected, listElements, reset, toggle, toggleAll } = useBulkActions(
params.ids
);
2019-08-09 11:14:35 +00:00
const { updateListSettings, settings } = useListSettings(
ListViews.COLLECTION_LIST
);
const intl = useIntl();
2019-06-19 14:40:52 +00:00
2019-12-17 17:13:56 +00:00
const paginationState = createPaginationState(settings.rowNumber, params);
const queryVariables = React.useMemo(
() => ({
...paginationState,
filter: getFilterVariables(params),
sort: getSortQueryVariables(params)
}),
[params]
);
const { data, loading, refetch } = useCollectionListQuery({
displayLoader: true,
variables: queryVariables
});
2019-09-11 14:00:02 +00:00
const tabs = getFilterTabs();
const currentTab =
params.activeTab === undefined
? areFiltersApplied(params)
? tabs.length + 1
: 0
: parseInt(params.activeTab, 0);
2020-01-07 13:34:45 +00:00
const changeFilters = (filter: IFilter<CollectionFilterKeys>) => {
2019-09-11 14:00:02 +00:00
reset();
navigate(
collectionListUrl({
2020-01-07 13:34:45 +00:00
...params,
...getFilterQueryParams(filter, getFilterQueryParam),
2019-09-11 14:00:02 +00:00
activeTab: undefined
})
);
};
2020-01-07 13:34:45 +00:00
const resetFilters = () => {
reset();
navigate(
collectionListUrl({
asc: params.asc,
sort: params.sort
})
);
};
const handleSearchChange = (query: string) => {
reset();
navigate(
collectionListUrl({
...params,
activeTab: undefined,
query
})
);
};
2019-12-06 14:58:28 +00:00
const [openModal, closeModal] = createDialogActionHandlers<
CollectionListUrlDialog,
CollectionListUrlQueryParams
>(navigate, collectionListUrl, params);
2019-06-19 14:40:52 +00:00
2019-09-11 14:00:02 +00:00
const handleTabChange = (tab: number) => {
reset();
navigate(
collectionListUrl({
activeTab: tab.toString(),
...getFilterTabs()[tab - 1].data
})
);
};
const handleTabDelete = () => {
deleteFilterTab(currentTab);
reset();
navigate(collectionListUrl());
};
const handleTabSave = (data: SaveFilterTabDialogFormData) => {
saveFilterTab(data.name, getActiveFilters(params));
handleTabChange(tabs.length + 1);
};
2019-12-17 17:13:56 +00:00
const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
maybe(() => data.collections.pageInfo),
paginationState,
params
2019-09-11 14:00:02 +00:00
);
2019-12-17 17:13:56 +00:00
const handleCollectionBulkDelete = (data: CollectionBulkDelete) => {
if (data.collectionBulkDelete.errors.length === 0) {
notify({
text: intl.formatMessage(commonMessages.savedChanges)
});
refetch();
reset();
closeModal();
}
};
2019-06-19 14:40:52 +00:00
2019-12-17 17:13:56 +00:00
const handleCollectionBulkPublish = (data: CollectionBulkPublish) => {
if (data.collectionBulkPublish.errors.length === 0) {
notify({
text: intl.formatMessage(commonMessages.savedChanges)
});
refetch();
reset();
closeModal();
}
};
2019-06-19 14:40:52 +00:00
2019-12-17 17:13:56 +00:00
const handleSort = createSortHandler(navigate, collectionListUrl, params);
2020-01-07 13:34:45 +00:00
const currencySymbol = maybe(() => shop.defaultCurrency, "USD");
2019-06-19 14:40:52 +00:00
2019-12-17 17:13:56 +00:00
return (
<TypedCollectionBulkDelete onCompleted={handleCollectionBulkDelete}>
{(collectionBulkDelete, collectionBulkDeleteOpts) => (
<TypedCollectionBulkPublish onCompleted={handleCollectionBulkPublish}>
{(collectionBulkPublish, collectionBulkPublishOpts) => (
<>
<CollectionListPage
2020-01-07 13:34:45 +00:00
currencySymbol={currencySymbol}
2019-12-17 17:13:56 +00:00
currentTab={currentTab}
2020-01-07 13:34:45 +00:00
filterOpts={getFilterOpts(params)}
2019-12-17 17:13:56 +00:00
initialSearch={params.query || ""}
2020-01-07 13:34:45 +00:00
onSearchChange={handleSearchChange}
onFilterChange={changeFilters}
2019-12-17 17:13:56 +00:00
onAdd={() => navigate(collectionAddUrl)}
2020-01-07 13:34:45 +00:00
onAll={resetFilters}
2019-12-17 17:13:56 +00:00
onTabChange={handleTabChange}
onTabDelete={() => openModal("delete-search")}
onTabSave={() => openModal("save-search")}
tabs={tabs.map(tab => tab.name)}
disabled={loading}
collections={maybe(() =>
data.collections.edges.map(edge => edge.node)
)}
settings={settings}
onNextPage={loadNextPage}
onPreviousPage={loadPreviousPage}
onSort={handleSort}
onUpdateListSettings={updateListSettings}
pageInfo={pageInfo}
sort={getSortParams(params)}
onRowClick={id => () => navigate(collectionUrl(id))}
toolbar={
2019-12-06 17:11:46 +00:00
<>
2019-12-17 17:13:56 +00:00
<Button
color="primary"
2019-12-06 14:58:28 +00:00
onClick={() =>
openModal("unpublish", {
ids: listElements
})
}
2019-12-06 17:11:46 +00:00
>
2019-12-17 17:13:56 +00:00
<FormattedMessage
defaultMessage="Unpublish"
description="unpublish collections"
/>
</Button>
<Button
color="primary"
2019-12-06 14:58:28 +00:00
onClick={() =>
openModal("publish", {
ids: listElements
})
}
2019-12-06 17:11:46 +00:00
>
2019-12-17 17:13:56 +00:00
<FormattedMessage
defaultMessage="Publish"
description="publish collections"
/>
</Button>
<IconButton
color="primary"
2019-12-06 14:58:28 +00:00
onClick={() =>
openModal("remove", {
ids: listElements
})
}
2019-12-06 17:11:46 +00:00
>
2019-12-17 17:13:56 +00:00
<DeleteIcon />
</IconButton>
2019-12-06 17:11:46 +00:00
</>
2019-12-17 17:13:56 +00:00
}
isChecked={isSelected}
selected={listElements.length}
toggle={toggle}
toggleAll={toggleAll}
/>
<ActionDialog
open={
params.action === "publish" &&
maybe(() => params.ids.length > 0)
}
onClose={closeModal}
confirmButtonState={collectionBulkPublishOpts.status}
onConfirm={() =>
collectionBulkPublish({
variables: {
ids: params.ids,
isPublished: true
}
})
}
variant="default"
title={intl.formatMessage({
defaultMessage: "Publish collections",
description: "dialog title"
})}
>
<DialogContentText>
<FormattedMessage
defaultMessage="Are you sure you want to publish {counter,plural,one{this collection} other{{displayQuantity} collections}}?"
values={{
counter: maybe(() => params.ids.length),
displayQuantity: (
<strong>{maybe(() => params.ids.length)}</strong>
)
}}
/>
</DialogContentText>
</ActionDialog>
<ActionDialog
open={
params.action === "unpublish" &&
maybe(() => params.ids.length > 0)
}
onClose={closeModal}
confirmButtonState={collectionBulkPublishOpts.status}
onConfirm={() =>
collectionBulkPublish({
variables: {
ids: params.ids,
isPublished: false
}
})
}
variant="default"
title={intl.formatMessage({
defaultMessage: "Unpublish collections",
description: "dialog title"
})}
>
<DialogContentText>
<FormattedMessage
defaultMessage="Are you sure you want to unpublish {counter,plural,one{this collection} other{{displayQuantity} collections}}?"
values={{
counter: maybe(() => params.ids.length),
displayQuantity: (
<strong>{maybe(() => params.ids.length)}</strong>
)
}}
/>
</DialogContentText>
</ActionDialog>
<ActionDialog
open={
params.action === "remove" &&
maybe(() => params.ids.length > 0)
}
onClose={closeModal}
confirmButtonState={collectionBulkDeleteOpts.status}
onConfirm={() =>
collectionBulkDelete({
variables: {
ids: params.ids
}
})
}
variant="delete"
title={intl.formatMessage({
defaultMessage: "Delete collections",
description: "dialog title"
})}
>
<DialogContentText>
<FormattedMessage
defaultMessage="Are you sure you want to delete {counter,plural,one{this collection} other{{displayQuantity} collections}}?"
values={{
counter: maybe(() => params.ids.length),
displayQuantity: (
<strong>{maybe(() => params.ids.length)}</strong>
)
}}
/>
</DialogContentText>
</ActionDialog>
<SaveFilterTabDialog
open={params.action === "save-search"}
confirmButtonState="default"
onClose={closeModal}
onSubmit={handleTabSave}
/>
<DeleteFilterTabDialog
open={params.action === "delete-search"}
confirmButtonState="default"
onClose={closeModal}
onSubmit={handleTabDelete}
tabName={maybe(() => tabs[currentTab - 1].name, "...")}
/>
</>
)}
</TypedCollectionBulkPublish>
)}
</TypedCollectionBulkDelete>
2019-06-19 14:40:52 +00:00
);
};
export default CollectionList;