2019-06-19 14:40:52 +00:00
|
|
|
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-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-09 11:14:35 +00:00
|
|
|
import i18n from "@saleor/i18n";
|
|
|
|
import { getMutationState, maybe } from "@saleor/misc";
|
|
|
|
import { ListViews } from "@saleor/types";
|
2019-06-19 14:40:52 +00:00
|
|
|
import CustomerListPage from "../components/CustomerListPage";
|
|
|
|
import { TypedBulkRemoveCustomers } from "../mutations";
|
|
|
|
import { TypedCustomerListQuery } from "../queries";
|
|
|
|
import { BulkRemoveCustomers } from "../types/BulkRemoveCustomers";
|
|
|
|
import {
|
|
|
|
customerAddUrl,
|
|
|
|
customerListUrl,
|
|
|
|
CustomerListUrlQueryParams,
|
|
|
|
customerUrl
|
|
|
|
} from "../urls";
|
|
|
|
|
|
|
|
interface CustomerListProps {
|
|
|
|
params: CustomerListUrlQueryParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const CustomerList: React.StatelessComponent<CustomerListProps> = ({
|
|
|
|
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.CUSTOMER_LIST
|
|
|
|
);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
const closeModal = () =>
|
|
|
|
navigate(
|
|
|
|
customerListUrl({
|
|
|
|
...params,
|
|
|
|
action: undefined,
|
|
|
|
ids: undefined
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
const paginationState = createPaginationState(settings.rowNumber, params);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<TypedCustomerListQuery displayLoader variables={paginationState}>
|
|
|
|
{({ data, loading, refetch }) => {
|
|
|
|
const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
|
|
|
|
maybe(() => data.customers.pageInfo),
|
|
|
|
paginationState,
|
|
|
|
params
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleBulkCustomerDelete = (data: BulkRemoveCustomers) => {
|
|
|
|
if (data.customerBulkDelete.errors.length === 0) {
|
|
|
|
notify({
|
|
|
|
text: i18n.t("Customers removed")
|
|
|
|
});
|
|
|
|
reset();
|
|
|
|
refetch();
|
|
|
|
closeModal();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TypedBulkRemoveCustomers onCompleted={handleBulkCustomerDelete}>
|
|
|
|
{(bulkRemoveCustomers, bulkRemoveCustomersOpts) => {
|
|
|
|
const removeTransitionState = getMutationState(
|
|
|
|
bulkRemoveCustomersOpts.called,
|
|
|
|
bulkRemoveCustomersOpts.loading,
|
|
|
|
maybe(
|
|
|
|
() => bulkRemoveCustomersOpts.data.customerBulkDelete.errors
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<CustomerListPage
|
|
|
|
customers={maybe(() =>
|
|
|
|
data.customers.edges.map(edge => edge.node)
|
|
|
|
)}
|
2019-08-09 11:14:35 +00:00
|
|
|
settings={settings}
|
2019-06-19 14:40:52 +00:00
|
|
|
disabled={loading}
|
|
|
|
pageInfo={pageInfo}
|
|
|
|
onAdd={() => navigate(customerAddUrl)}
|
|
|
|
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(customerUrl(id))}
|
|
|
|
toolbar={
|
|
|
|
<IconButton
|
|
|
|
color="primary"
|
|
|
|
onClick={() =>
|
|
|
|
navigate(
|
|
|
|
customerListUrl({
|
|
|
|
action: "remove",
|
|
|
|
ids: listElements
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<DeleteIcon />
|
|
|
|
</IconButton>
|
|
|
|
}
|
|
|
|
isChecked={isSelected}
|
|
|
|
selected={listElements.length}
|
|
|
|
toggle={toggle}
|
|
|
|
toggleAll={toggleAll}
|
|
|
|
/>
|
|
|
|
<ActionDialog
|
|
|
|
open={params.action === "remove"}
|
|
|
|
onClose={closeModal}
|
|
|
|
confirmButtonState={removeTransitionState}
|
|
|
|
onConfirm={() =>
|
|
|
|
bulkRemoveCustomers({
|
|
|
|
variables: {
|
|
|
|
ids: params.ids
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
variant="delete"
|
|
|
|
title={i18n.t("Remove customers")}
|
|
|
|
>
|
|
|
|
<DialogContentText
|
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: i18n.t(
|
|
|
|
"Are you sure you want to remove <strong>{{ number }}</strong> customers?",
|
|
|
|
{
|
|
|
|
number: maybe(
|
|
|
|
() => params.ids.length.toString(),
|
|
|
|
"..."
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</ActionDialog>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedBulkRemoveCustomers>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedCustomerListQuery>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default CustomerList;
|