Refactor Gift Card List Top Alert (#1388)
This commit is contained in:
parent
c566539cc1
commit
eaed88a23a
2 changed files with 62 additions and 43 deletions
|
@ -5,18 +5,13 @@ import CardMenu, { CardMenuItem } from "@saleor/components/CardMenu";
|
|||
import PageHeader from "@saleor/components/PageHeader";
|
||||
import useNavigator from "@saleor/hooks/useNavigator";
|
||||
import { sectionNames } from "@saleor/intl";
|
||||
import { Alert } from "@saleor/macaw-ui";
|
||||
import { productAddUrl } from "@saleor/products/urls";
|
||||
import { productTypeAddUrl } from "@saleor/productTypes/urls";
|
||||
import { ProductTypeKindEnum } from "@saleor/types/globalTypes";
|
||||
import React from "react";
|
||||
import { useIntl } from "react-intl";
|
||||
|
||||
import { giftCardSettingsUrl } from "../../urls";
|
||||
import { giftCardsListHeaderMenuItemsMessages as messages } from "../messages";
|
||||
import useGiftCardListDialogs from "../providers/GiftCardListDialogsProvider/hooks/useGiftCardListDialogs";
|
||||
import { useGiftCardProductsCountQuery } from "../queries";
|
||||
import GiftCardsListHeaderAlertContent from "./GiftCardsListHeaderAlertContent";
|
||||
import GiftCardsListHeaderAlert from "./GiftCardsListHeaderAlert";
|
||||
|
||||
const GiftCardsListHeader: React.FC = () => {
|
||||
const intl = useIntl();
|
||||
|
@ -24,11 +19,6 @@ const GiftCardsListHeader: React.FC = () => {
|
|||
|
||||
const { openCreateDialog } = useGiftCardListDialogs();
|
||||
|
||||
const {
|
||||
data: giftCardProductsCount,
|
||||
loading: giftCardProductsCountLoading
|
||||
} = useGiftCardProductsCountQuery();
|
||||
|
||||
const openSettings = () => navigate(giftCardSettingsUrl);
|
||||
|
||||
const menuItems: CardMenuItem[] = [
|
||||
|
@ -49,24 +39,6 @@ const GiftCardsListHeader: React.FC = () => {
|
|||
// }
|
||||
];
|
||||
|
||||
const giftCardProductTypesExist =
|
||||
giftCardProductsCount?.giftCardProductTypes.totalCount > 0;
|
||||
const giftCardProductsExist =
|
||||
giftCardProductsCount?.giftCardProducts.totalCount > 0;
|
||||
|
||||
const showNoGiftCardProductsAlert =
|
||||
!giftCardProductsCountLoading &&
|
||||
(!giftCardProductTypesExist || !giftCardProductsExist);
|
||||
|
||||
const handleCreateGiftCardProductType = () =>
|
||||
navigate(
|
||||
productTypeAddUrl({
|
||||
kind: ProductTypeKindEnum.GIFT_CARD
|
||||
})
|
||||
);
|
||||
|
||||
const handleCreateGiftCardProduct = () => navigate(productAddUrl());
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader title={intl.formatMessage(sectionNames.giftCards)}>
|
||||
|
@ -81,20 +53,7 @@ const GiftCardsListHeader: React.FC = () => {
|
|||
{intl.formatMessage(messages.issueButtonLabel)}
|
||||
</Button>
|
||||
</PageHeader>
|
||||
{showNoGiftCardProductsAlert && (
|
||||
<Alert
|
||||
title={intl.formatMessage(messages.noGiftCardsAlertTitle)}
|
||||
variant="warning"
|
||||
close={false}
|
||||
>
|
||||
<GiftCardsListHeaderAlertContent
|
||||
giftCardProductTypesExist={giftCardProductTypesExist}
|
||||
giftCardProductsExist={giftCardProductsExist}
|
||||
handleCreateGiftCardProductType={handleCreateGiftCardProductType}
|
||||
handleCreateGiftCardProduct={handleCreateGiftCardProduct}
|
||||
/>
|
||||
</Alert>
|
||||
)}
|
||||
<GiftCardsListHeaderAlert />
|
||||
<VerticalSpacer spacing={2} />
|
||||
</>
|
||||
);
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
import useNavigator from "@saleor/hooks/useNavigator";
|
||||
import { Alert } from "@saleor/macaw-ui";
|
||||
import { productAddUrl } from "@saleor/products/urls";
|
||||
import { productTypeAddUrl } from "@saleor/productTypes/urls";
|
||||
import { ProductTypeKindEnum } from "@saleor/types/globalTypes";
|
||||
import React from "react";
|
||||
import { useIntl } from "react-intl";
|
||||
|
||||
import { giftCardsListHeaderMenuItemsMessages as messages } from "../messages";
|
||||
import { useGiftCardProductsCountQuery } from "../queries";
|
||||
import GiftCardsListHeaderAlertContent from "./GiftCardsListHeaderAlertContent";
|
||||
|
||||
const GiftCardsListHeaderAlert: React.FC = () => {
|
||||
const intl = useIntl();
|
||||
const navigate = useNavigator();
|
||||
|
||||
const {
|
||||
data: giftCardProductsCount,
|
||||
loading: giftCardProductsCountLoading
|
||||
} = useGiftCardProductsCountQuery();
|
||||
|
||||
const giftCardProductTypesExist =
|
||||
giftCardProductsCount?.giftCardProductTypes.totalCount > 0;
|
||||
const giftCardProductsExist =
|
||||
giftCardProductsCount?.giftCardProducts.totalCount > 0;
|
||||
|
||||
const showNoGiftCardProductsAlert =
|
||||
!giftCardProductsCountLoading &&
|
||||
(!giftCardProductTypesExist || !giftCardProductsExist);
|
||||
|
||||
const handleCreateGiftCardProductType = () =>
|
||||
navigate(
|
||||
productTypeAddUrl({
|
||||
kind: ProductTypeKindEnum.GIFT_CARD
|
||||
})
|
||||
);
|
||||
|
||||
const handleCreateGiftCardProduct = () => navigate(productAddUrl());
|
||||
|
||||
if (showNoGiftCardProductsAlert) {
|
||||
return (
|
||||
<Alert
|
||||
title={intl.formatMessage(messages.noGiftCardsAlertTitle)}
|
||||
variant="warning"
|
||||
close={false}
|
||||
>
|
||||
<GiftCardsListHeaderAlertContent
|
||||
giftCardProductTypesExist={giftCardProductTypesExist}
|
||||
giftCardProductsExist={giftCardProductsExist}
|
||||
handleCreateGiftCardProductType={handleCreateGiftCardProductType}
|
||||
handleCreateGiftCardProduct={handleCreateGiftCardProduct}
|
||||
/>
|
||||
</Alert>
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
export default GiftCardsListHeaderAlert;
|
Loading…
Reference in a new issue