Use query hooks in product list
This commit is contained in:
parent
7726bacb77
commit
4df1c71a79
2 changed files with 305 additions and 347 deletions
|
@ -150,10 +150,9 @@ const productListQuery = gql`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
export const TypedProductListQuery = TypedQuery<
|
export const useProductListQuery = makeQuery<ProductList, ProductListVariables>(
|
||||||
ProductList,
|
productListQuery
|
||||||
ProductListVariables
|
);
|
||||||
>(productListQuery);
|
|
||||||
|
|
||||||
const countAllProductsQuery = gql`
|
const countAllProductsQuery = gql`
|
||||||
query CountAllProducts {
|
query CountAllProducts {
|
||||||
|
@ -288,7 +287,7 @@ const availableInGridAttributes = gql`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
export const AvailableInGridAttributesQuery = TypedQuery<
|
export const useAvailableInGridAttributesQuery = makeQuery<
|
||||||
GridAttributes,
|
GridAttributes,
|
||||||
GridAttributesVariables
|
GridAttributesVariables
|
||||||
>(availableInGridAttributes);
|
>(availableInGridAttributes);
|
||||||
|
|
|
@ -45,18 +45,16 @@ import { FormattedMessage, useIntl } from "react-intl";
|
||||||
|
|
||||||
import ProductListPage from "../../components/ProductListPage";
|
import ProductListPage from "../../components/ProductListPage";
|
||||||
import {
|
import {
|
||||||
TypedProductBulkDeleteMutation,
|
useProductBulkDeleteMutation,
|
||||||
TypedProductBulkPublishMutation,
|
useProductBulkPublishMutation,
|
||||||
useProductExport
|
useProductExport
|
||||||
} from "../../mutations";
|
} from "../../mutations";
|
||||||
import {
|
import {
|
||||||
AvailableInGridAttributesQuery,
|
useAvailableInGridAttributesQuery,
|
||||||
TypedProductListQuery,
|
|
||||||
useCountAllProducts,
|
useCountAllProducts,
|
||||||
useInitialProductFilterDataQuery
|
useInitialProductFilterDataQuery,
|
||||||
|
useProductListQuery
|
||||||
} from "../../queries";
|
} from "../../queries";
|
||||||
import { productBulkDelete } from "../../types/productBulkDelete";
|
|
||||||
import { productBulkPublish } from "../../types/productBulkPublish";
|
|
||||||
import {
|
import {
|
||||||
productAddUrl,
|
productAddUrl,
|
||||||
productListUrl,
|
productListUrl,
|
||||||
|
@ -235,6 +233,53 @@ export const ProductList: React.FC<ProductListProps> = ({ params }) => {
|
||||||
}),
|
}),
|
||||||
[params, settings.rowNumber]
|
[params, settings.rowNumber]
|
||||||
);
|
);
|
||||||
|
const { data, loading, refetch } = useProductListQuery({
|
||||||
|
displayLoader: true,
|
||||||
|
variables: queryVariables
|
||||||
|
});
|
||||||
|
|
||||||
|
function filterColumnIds(columns: ProductListColumns[]) {
|
||||||
|
return columns
|
||||||
|
.filter(isAttributeColumnValue)
|
||||||
|
.map(getAttributeIdFromColumnValue);
|
||||||
|
}
|
||||||
|
const attributes = useAvailableInGridAttributesQuery({
|
||||||
|
variables: { first: 6, ids: filterColumnIds(settings.columns) }
|
||||||
|
});
|
||||||
|
|
||||||
|
const [
|
||||||
|
productBulkDelete,
|
||||||
|
productBulkDeleteOpts
|
||||||
|
] = useProductBulkDeleteMutation({
|
||||||
|
onCompleted: data => {
|
||||||
|
if (data.productBulkDelete.errors.length === 0) {
|
||||||
|
closeModal();
|
||||||
|
notify({
|
||||||
|
status: "success",
|
||||||
|
text: intl.formatMessage(commonMessages.savedChanges)
|
||||||
|
});
|
||||||
|
reset();
|
||||||
|
refetch();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const [
|
||||||
|
productBulkPublish,
|
||||||
|
productBulkPublishOpts
|
||||||
|
] = useProductBulkPublishMutation({
|
||||||
|
onCompleted: data => {
|
||||||
|
if (data.productBulkPublish.errors.length === 0) {
|
||||||
|
closeModal();
|
||||||
|
notify({
|
||||||
|
status: "success",
|
||||||
|
text: intl.formatMessage(commonMessages.savedChanges)
|
||||||
|
});
|
||||||
|
reset();
|
||||||
|
refetch();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const filterOpts = getFilterOpts(
|
const filterOpts = getFilterOpts(
|
||||||
params,
|
params,
|
||||||
|
@ -262,56 +307,13 @@ export const ProductList: React.FC<ProductListProps> = ({ params }) => {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
function filterColumnIds(columns: ProductListColumns[]) {
|
|
||||||
return columns
|
|
||||||
.filter(isAttributeColumnValue)
|
|
||||||
.map(getAttributeIdFromColumnValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<AvailableInGridAttributesQuery
|
|
||||||
variables={{ first: 6, ids: filterColumnIds(settings.columns) }}
|
|
||||||
>
|
|
||||||
{attributes => (
|
|
||||||
<TypedProductListQuery displayLoader variables={queryVariables}>
|
|
||||||
{({ data, loading, refetch }) => {
|
|
||||||
const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
|
const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
|
||||||
maybe(() => data.products.pageInfo),
|
maybe(() => data.products.pageInfo),
|
||||||
paginationState,
|
paginationState,
|
||||||
params
|
params
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleBulkDelete = (data: productBulkDelete) => {
|
|
||||||
if (data.productBulkDelete.errors.length === 0) {
|
|
||||||
closeModal();
|
|
||||||
notify({
|
|
||||||
status: "success",
|
|
||||||
text: intl.formatMessage(commonMessages.savedChanges)
|
|
||||||
});
|
|
||||||
reset();
|
|
||||||
refetch();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleBulkPublish = (data: productBulkPublish) => {
|
|
||||||
if (data.productBulkPublish.errors.length === 0) {
|
|
||||||
closeModal();
|
|
||||||
notify({
|
|
||||||
status: "success",
|
|
||||||
text: intl.formatMessage(commonMessages.savedChanges)
|
|
||||||
});
|
|
||||||
reset();
|
|
||||||
refetch();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TypedProductBulkDeleteMutation onCompleted={handleBulkDelete}>
|
|
||||||
{(productBulkDelete, productBulkDeleteOpts) => (
|
|
||||||
<TypedProductBulkPublishMutation
|
|
||||||
onCompleted={handleBulkPublish}
|
|
||||||
>
|
|
||||||
{(productBulkPublish, productBulkPublishOpts) => (
|
|
||||||
<>
|
<>
|
||||||
<ProductListPage
|
<ProductListPage
|
||||||
activeAttributeSortId={params.attributeId}
|
activeAttributeSortId={params.attributeId}
|
||||||
|
@ -321,21 +323,15 @@ export const ProductList: React.FC<ProductListProps> = ({ params }) => {
|
||||||
}}
|
}}
|
||||||
onSort={handleSort}
|
onSort={handleSort}
|
||||||
availableInGridAttributes={maybe(
|
availableInGridAttributes={maybe(
|
||||||
() =>
|
() => attributes.data.availableInGrid.edges.map(edge => edge.node),
|
||||||
attributes.data.availableInGrid.edges.map(
|
|
||||||
edge => edge.node
|
|
||||||
),
|
|
||||||
[]
|
[]
|
||||||
)}
|
)}
|
||||||
currencySymbol={currencySymbol}
|
currencySymbol={currencySymbol}
|
||||||
currentTab={currentTab}
|
currentTab={currentTab}
|
||||||
defaultSettings={
|
defaultSettings={defaultListSettings[ListViews.PRODUCT_LIST]}
|
||||||
defaultListSettings[ListViews.PRODUCT_LIST]
|
|
||||||
}
|
|
||||||
filterOpts={filterOpts}
|
filterOpts={filterOpts}
|
||||||
gridAttributes={maybe(
|
gridAttributes={maybe(
|
||||||
() =>
|
() => attributes.data.grid.edges.map(edge => edge.node),
|
||||||
attributes.data.grid.edges.map(edge => edge.node),
|
|
||||||
[]
|
[]
|
||||||
)}
|
)}
|
||||||
totalGridAttributes={maybe(
|
totalGridAttributes={maybe(
|
||||||
|
@ -345,16 +341,12 @@ export const ProductList: React.FC<ProductListProps> = ({ params }) => {
|
||||||
settings={settings}
|
settings={settings}
|
||||||
loading={attributes.loading}
|
loading={attributes.loading}
|
||||||
hasMore={maybe(
|
hasMore={maybe(
|
||||||
() =>
|
() => attributes.data.availableInGrid.pageInfo.hasNextPage,
|
||||||
attributes.data.availableInGrid.pageInfo
|
|
||||||
.hasNextPage,
|
|
||||||
false
|
false
|
||||||
)}
|
)}
|
||||||
onAdd={() => navigate(productAddUrl)}
|
onAdd={() => navigate(productAddUrl)}
|
||||||
disabled={loading}
|
disabled={loading}
|
||||||
products={maybe(() =>
|
products={maybe(() => data.products.edges.map(edge => edge.node))}
|
||||||
data.products.edges.map(edge => edge.node)
|
|
||||||
)}
|
|
||||||
onFetchMore={() =>
|
onFetchMore={() =>
|
||||||
attributes.loadMore(
|
attributes.loadMore(
|
||||||
(prev, next) => {
|
(prev, next) => {
|
||||||
|
@ -377,9 +369,7 @@ export const ProductList: React.FC<ProductListProps> = ({ params }) => {
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
after:
|
after: attributes.data.availableInGrid.pageInfo.endCursor
|
||||||
attributes.data.availableInGrid.pageInfo
|
|
||||||
.endCursor
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -463,11 +453,7 @@ export const ProductList: React.FC<ProductListProps> = ({ params }) => {
|
||||||
description="dialog content"
|
description="dialog content"
|
||||||
values={{
|
values={{
|
||||||
counter: maybe(() => params.ids.length),
|
counter: maybe(() => params.ids.length),
|
||||||
displayQuantity: (
|
displayQuantity: <strong>{maybe(() => params.ids.length)}</strong>
|
||||||
<strong>
|
|
||||||
{maybe(() => params.ids.length)}
|
|
||||||
</strong>
|
|
||||||
)
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</DialogContentText>
|
</DialogContentText>
|
||||||
|
@ -495,11 +481,7 @@ export const ProductList: React.FC<ProductListProps> = ({ params }) => {
|
||||||
description="dialog content"
|
description="dialog content"
|
||||||
values={{
|
values={{
|
||||||
counter: maybe(() => params.ids.length),
|
counter: maybe(() => params.ids.length),
|
||||||
displayQuantity: (
|
displayQuantity: <strong>{maybe(() => params.ids.length)}</strong>
|
||||||
<strong>
|
|
||||||
{maybe(() => params.ids.length)}
|
|
||||||
</strong>
|
|
||||||
)
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</DialogContentText>
|
</DialogContentText>
|
||||||
|
@ -527,40 +509,29 @@ export const ProductList: React.FC<ProductListProps> = ({ params }) => {
|
||||||
description="dialog content"
|
description="dialog content"
|
||||||
values={{
|
values={{
|
||||||
counter: maybe(() => params.ids.length),
|
counter: maybe(() => params.ids.length),
|
||||||
displayQuantity: (
|
displayQuantity: <strong>{maybe(() => params.ids.length)}</strong>
|
||||||
<strong>
|
|
||||||
{maybe(() => params.ids.length)}
|
|
||||||
</strong>
|
|
||||||
)
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</DialogContentText>
|
</DialogContentText>
|
||||||
</ActionDialog>
|
</ActionDialog>
|
||||||
<ProductExportDialog
|
<ProductExportDialog
|
||||||
attributes={(
|
attributes={(searchAttributes.result.data?.search.edges || []).map(
|
||||||
searchAttributes.result.data?.search.edges || []
|
edge => edge.node
|
||||||
).map(edge => edge.node)}
|
)}
|
||||||
hasMore={
|
hasMore={searchAttributes.result.data?.search.pageInfo.hasNextPage}
|
||||||
searchAttributes.result.data?.search.pageInfo
|
|
||||||
.hasNextPage
|
|
||||||
}
|
|
||||||
loading={searchAttributes.result.loading}
|
loading={searchAttributes.result.loading}
|
||||||
onFetch={searchAttributes.search}
|
onFetch={searchAttributes.search}
|
||||||
onFetchMore={searchAttributes.loadMore}
|
onFetchMore={searchAttributes.loadMore}
|
||||||
open={params.action === "export"}
|
open={params.action === "export"}
|
||||||
confirmButtonState={exportProductsOpts.status}
|
confirmButtonState={exportProductsOpts.status}
|
||||||
errors={
|
errors={exportProductsOpts.data?.exportProducts.errors || []}
|
||||||
exportProductsOpts.data?.exportProducts.errors || []
|
|
||||||
}
|
|
||||||
productQuantity={{
|
productQuantity={{
|
||||||
all: countAllProducts.data?.products.totalCount,
|
all: countAllProducts.data?.products.totalCount,
|
||||||
filter: data?.products.totalCount
|
filter: data?.products.totalCount
|
||||||
}}
|
}}
|
||||||
selectedProducts={listElements.length}
|
selectedProducts={listElements.length}
|
||||||
warehouses={
|
warehouses={
|
||||||
warehouses.data?.warehouses.edges.map(
|
warehouses.data?.warehouses.edges.map(edge => edge.node) || []
|
||||||
edge => edge.node
|
|
||||||
) || []
|
|
||||||
}
|
}
|
||||||
onClose={closeModal}
|
onClose={closeModal}
|
||||||
onSubmit={data =>
|
onSubmit={data =>
|
||||||
|
@ -586,21 +557,9 @@ export const ProductList: React.FC<ProductListProps> = ({ params }) => {
|
||||||
confirmButtonState="default"
|
confirmButtonState="default"
|
||||||
onClose={closeModal}
|
onClose={closeModal}
|
||||||
onSubmit={handleFilterTabDelete}
|
onSubmit={handleFilterTabDelete}
|
||||||
tabName={maybe(
|
tabName={maybe(() => tabs[currentTab - 1].name, "...")}
|
||||||
() => tabs[currentTab - 1].name,
|
|
||||||
"..."
|
|
||||||
)}
|
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
)}
|
|
||||||
</TypedProductBulkPublishMutation>
|
|
||||||
)}
|
|
||||||
</TypedProductBulkDeleteMutation>
|
|
||||||
);
|
|
||||||
}}
|
|
||||||
</TypedProductListQuery>
|
|
||||||
)}
|
|
||||||
</AvailableInGridAttributesQuery>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
export default ProductList;
|
export default ProductList;
|
||||||
|
|
Loading…
Reference in a new issue