2019-08-09 10:17:04 +00:00
|
|
|
import IconButton from "@material-ui/core/IconButton";
|
|
|
|
import DeleteIcon from "@material-ui/icons/Delete";
|
|
|
|
import React from "react";
|
2019-08-16 11:47:30 +00:00
|
|
|
import { useIntl } from "react-intl";
|
2019-08-09 10:17:04 +00:00
|
|
|
|
|
|
|
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 useBulkActions from "../../../hooks/useBulkActions";
|
|
|
|
import { getMutationState, maybe } from "../../../misc";
|
|
|
|
import AttributeBulkDeleteDialog from "../../components/AttributeBulkDeleteDialog";
|
|
|
|
import AttributeListPage from "../../components/AttributeListPage";
|
|
|
|
import { AttributeBulkDeleteMutation } from "../../mutations";
|
|
|
|
import { AttributeListQuery } from "../../queries";
|
|
|
|
import { AttributeBulkDelete } from "../../types/AttributeBulkDelete";
|
|
|
|
import {
|
|
|
|
attributeAddUrl,
|
|
|
|
attributeListUrl,
|
|
|
|
AttributeListUrlDialog,
|
|
|
|
AttributeListUrlQueryParams,
|
|
|
|
attributeUrl
|
|
|
|
} from "../../urls";
|
|
|
|
|
|
|
|
interface AttributeListProps {
|
|
|
|
params: AttributeListUrlQueryParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AttributeList: React.FC<AttributeListProps> = ({ params }) => {
|
|
|
|
const navigate = useNavigator();
|
|
|
|
const paginate = usePaginator();
|
|
|
|
const notify = useNotifier();
|
|
|
|
const { isSelected, listElements, reset, toggle, toggleAll } = useBulkActions(
|
|
|
|
params.ids
|
|
|
|
);
|
2019-08-16 11:47:30 +00:00
|
|
|
const intl = useIntl();
|
2019-08-09 10:17:04 +00:00
|
|
|
|
|
|
|
const closeModal = () =>
|
|
|
|
navigate(
|
|
|
|
attributeListUrl({
|
|
|
|
...params,
|
|
|
|
action: undefined,
|
|
|
|
ids: undefined
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
const openModal = (action: AttributeListUrlDialog, ids?: string[]) =>
|
|
|
|
navigate(
|
|
|
|
attributeListUrl({
|
|
|
|
...params,
|
|
|
|
action,
|
|
|
|
ids
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const paginationState = createPaginationState(PAGINATE_BY, params);
|
|
|
|
const queryVariables = React.useMemo(() => paginationState, [params]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AttributeListQuery variables={queryVariables}>
|
|
|
|
{({ data, loading, refetch }) => {
|
|
|
|
const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
|
|
|
|
maybe(() => data.attributes.pageInfo),
|
|
|
|
paginationState,
|
|
|
|
params
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleBulkDelete = (data: AttributeBulkDelete) => {
|
|
|
|
if (data.attributeBulkDelete.errors.length === 0) {
|
|
|
|
closeModal();
|
|
|
|
notify({
|
2019-08-16 11:47:30 +00:00
|
|
|
text: intl.formatMessage({
|
2019-08-20 10:05:08 +00:00
|
|
|
defaultMessage: "Attributes successfully delete",
|
2019-08-22 16:25:55 +00:00
|
|
|
description: "deleted multiple attributes"
|
2019-08-16 11:47:30 +00:00
|
|
|
})
|
2019-08-09 10:17:04 +00:00
|
|
|
});
|
|
|
|
reset();
|
|
|
|
refetch();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AttributeBulkDeleteMutation onCompleted={handleBulkDelete}>
|
|
|
|
{(attributeBulkDelete, attributeBulkDeleteOpts) => {
|
|
|
|
const bulkDeleteMutationState = getMutationState(
|
|
|
|
attributeBulkDeleteOpts.called,
|
|
|
|
attributeBulkDeleteOpts.loading,
|
|
|
|
maybe(
|
|
|
|
() => attributeBulkDeleteOpts.data.attributeBulkDelete.errors
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<AttributeListPage
|
|
|
|
attributes={maybe(() =>
|
|
|
|
data.attributes.edges.map(edge => edge.node)
|
|
|
|
)}
|
|
|
|
disabled={loading || attributeBulkDeleteOpts.loading}
|
|
|
|
isChecked={isSelected}
|
|
|
|
onAdd={() => navigate(attributeAddUrl())}
|
|
|
|
onNextPage={loadNextPage}
|
|
|
|
onPreviousPage={loadPreviousPage}
|
|
|
|
onRowClick={id => () => navigate(attributeUrl(id))}
|
|
|
|
pageInfo={pageInfo}
|
|
|
|
selected={listElements.length}
|
|
|
|
toggle={toggle}
|
|
|
|
toggleAll={toggleAll}
|
|
|
|
toolbar={
|
|
|
|
<IconButton
|
|
|
|
color="primary"
|
|
|
|
onClick={() => openModal("remove", listElements)}
|
|
|
|
>
|
|
|
|
<DeleteIcon />
|
|
|
|
</IconButton>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<AttributeBulkDeleteDialog
|
|
|
|
confirmButtonState={bulkDeleteMutationState}
|
2019-08-16 11:47:30 +00:00
|
|
|
open={
|
|
|
|
params.action === "remove" &&
|
|
|
|
maybe(() => params.ids.length > 0)
|
|
|
|
}
|
2019-08-09 10:17:04 +00:00
|
|
|
onConfirm={() =>
|
|
|
|
attributeBulkDelete({ variables: { ids: params.ids } })
|
|
|
|
}
|
|
|
|
onClose={closeModal}
|
2019-08-16 11:47:30 +00:00
|
|
|
quantity={maybe(() => params.ids.length)}
|
2019-08-09 10:17:04 +00:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</AttributeBulkDeleteMutation>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</AttributeListQuery>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
AttributeList.displayName = "AttributeList";
|
|
|
|
|
|
|
|
export default AttributeList;
|