2023-06-21 09:28:00 +00:00
|
|
|
// @ts-strict-ignore
|
2023-05-30 07:42:22 +00:00
|
|
|
import { ConfirmButton } from "@dashboard/components/ConfirmButton";
|
2023-01-16 09:45:12 +00:00
|
|
|
import { Task } from "@dashboard/containers/BackgroundTasks/types";
|
2022-03-09 08:56:55 +00:00
|
|
|
import {
|
|
|
|
useExportGiftCardsMutation,
|
2022-06-21 09:36:55 +00:00
|
|
|
useGiftCardTotalCountQuery,
|
2023-01-16 09:45:12 +00:00
|
|
|
} from "@dashboard/graphql";
|
|
|
|
import useBackgroundTask from "@dashboard/hooks/useBackgroundTask";
|
|
|
|
import useForm from "@dashboard/hooks/useForm";
|
|
|
|
import useNotifier from "@dashboard/hooks/useNotifier";
|
|
|
|
import ExportDialogSettings from "@dashboard/products/components/ProductExportDialog/ExportDialogSettings";
|
2022-01-25 12:44:19 +00:00
|
|
|
import {
|
|
|
|
ExportSettingsFormData,
|
|
|
|
exportSettingsInitialFormData,
|
2022-06-21 09:36:55 +00:00
|
|
|
exportSettingsInitialFormDataWithIds,
|
2023-01-16 09:45:12 +00:00
|
|
|
} from "@dashboard/products/components/ProductExportDialog/types";
|
|
|
|
import { DialogProps } from "@dashboard/types";
|
|
|
|
import {
|
|
|
|
DialogActions,
|
|
|
|
DialogContent,
|
|
|
|
DialogTitle,
|
|
|
|
Typography,
|
|
|
|
} from "@material-ui/core";
|
2022-01-25 12:44:19 +00:00
|
|
|
import React from "react";
|
|
|
|
import { FormattedMessage, useIntl } from "react-intl";
|
|
|
|
|
|
|
|
import ContentWithProgress from "../GiftCardCreateDialog/ContentWithProgress";
|
2022-03-09 08:56:55 +00:00
|
|
|
import { useGiftCardList } from "../GiftCardsList/providers/GiftCardListProvider";
|
2022-01-25 12:44:19 +00:00
|
|
|
import { giftCardExportDialogMessages as messages } from "./messages";
|
2022-01-28 12:34:20 +00:00
|
|
|
import useStyles from "./styles";
|
2022-01-25 12:44:19 +00:00
|
|
|
import { getExportGiftCardsInput } from "./utils";
|
|
|
|
|
2023-02-20 15:21:28 +00:00
|
|
|
const GiftCardExportDialog: React.FC<
|
|
|
|
Pick<DialogProps, "onClose"> & {
|
|
|
|
idsToExport?: string[] | null;
|
|
|
|
}
|
|
|
|
> = ({ onClose, idsToExport }) => {
|
2022-01-25 12:44:19 +00:00
|
|
|
const intl = useIntl();
|
|
|
|
const notify = useNotifier();
|
|
|
|
const { queue } = useBackgroundTask();
|
2022-01-28 12:34:20 +00:00
|
|
|
const classes = useStyles();
|
2022-01-25 12:44:19 +00:00
|
|
|
|
|
|
|
const hasIdsToExport = !!idsToExport?.length;
|
|
|
|
|
|
|
|
const {
|
|
|
|
loading: loadingGiftCardList,
|
2022-03-09 08:56:55 +00:00
|
|
|
totalCount: filteredGiftCardsCount,
|
2022-06-21 09:36:55 +00:00
|
|
|
listElements,
|
2022-01-25 12:44:19 +00:00
|
|
|
} = useGiftCardList();
|
|
|
|
|
|
|
|
const selectedIds = idsToExport ?? listElements;
|
|
|
|
|
2023-02-20 15:21:28 +00:00
|
|
|
const { data: allGiftCardsCountData, loading: loadingGiftCardCount } =
|
|
|
|
useGiftCardTotalCountQuery();
|
2022-01-25 12:44:19 +00:00
|
|
|
|
|
|
|
const loading = loadingGiftCardList || loadingGiftCardCount;
|
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
const [exportGiftCards, exportGiftCardsOpts] = useExportGiftCardsMutation({
|
|
|
|
onCompleted: data => {
|
|
|
|
const errors = data?.exportGiftCards?.errors;
|
2022-01-25 12:44:19 +00:00
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
if (!errors.length) {
|
|
|
|
notify({
|
|
|
|
text: intl.formatMessage(messages.successAlertDescription),
|
2022-06-21 09:36:55 +00:00
|
|
|
title: intl.formatMessage(messages.successAlertTitle),
|
2022-03-09 08:56:55 +00:00
|
|
|
});
|
2022-01-25 12:44:19 +00:00
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
queue(Task.EXPORT, {
|
2022-06-21 09:36:55 +00:00
|
|
|
id: data.exportGiftCards.exportFile.id,
|
2022-03-09 08:56:55 +00:00
|
|
|
});
|
2022-01-25 12:44:19 +00:00
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
onClose();
|
|
|
|
}
|
2022-06-21 09:36:55 +00:00
|
|
|
},
|
2022-01-25 12:44:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const handleSubmit = (data: ExportSettingsFormData) => {
|
|
|
|
exportGiftCards({
|
|
|
|
variables: {
|
|
|
|
input: getExportGiftCardsInput({
|
|
|
|
data,
|
2022-06-21 09:36:55 +00:00
|
|
|
ids: selectedIds,
|
|
|
|
}),
|
|
|
|
},
|
2022-01-25 12:44:19 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const { data, change, submit } = useForm(
|
|
|
|
hasIdsToExport
|
|
|
|
? exportSettingsInitialFormDataWithIds
|
|
|
|
: exportSettingsInitialFormData,
|
2022-06-21 09:36:55 +00:00
|
|
|
handleSubmit,
|
2022-01-25 12:44:19 +00:00
|
|
|
);
|
|
|
|
const allGiftCardsCount = allGiftCardsCountData?.giftCards?.totalCount;
|
|
|
|
|
|
|
|
const exportScopeLabels = {
|
|
|
|
allItems: intl.formatMessage(
|
|
|
|
{
|
2022-05-05 07:54:28 +00:00
|
|
|
id: "uQk8gB",
|
2022-01-25 12:44:19 +00:00
|
|
|
defaultMessage: "All gift cards ({number})",
|
2022-06-21 09:36:55 +00:00
|
|
|
description: "export all items to csv file",
|
2022-01-25 12:44:19 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-21 09:36:55 +00:00
|
|
|
number: allGiftCardsCount || "...",
|
|
|
|
},
|
2022-01-25 12:44:19 +00:00
|
|
|
),
|
|
|
|
selectedItems: intl.formatMessage(
|
|
|
|
{
|
2022-05-05 07:54:28 +00:00
|
|
|
id: "n97Ii0",
|
2022-01-25 12:44:19 +00:00
|
|
|
defaultMessage: "Selected giftCards ({number})",
|
2022-06-21 09:36:55 +00:00
|
|
|
description: "export selected items to csv file",
|
2022-01-25 12:44:19 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-21 09:36:55 +00:00
|
|
|
number: listElements.length,
|
|
|
|
},
|
|
|
|
),
|
2022-01-25 12:44:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-02-20 15:21:28 +00:00
|
|
|
<DialogTitle disableTypography>
|
2022-01-25 12:44:19 +00:00
|
|
|
<FormattedMessage {...messages.title} />
|
|
|
|
</DialogTitle>
|
|
|
|
<DialogContent>
|
|
|
|
<ContentWithProgress>
|
|
|
|
{!loading && (
|
|
|
|
<>
|
|
|
|
<ExportDialogSettings
|
|
|
|
errors={exportGiftCardsOpts?.data?.exportGiftCards?.errors}
|
|
|
|
onChange={change}
|
|
|
|
selectedItems={selectedIds?.length}
|
|
|
|
data={data}
|
|
|
|
exportScopeLabels={exportScopeLabels}
|
|
|
|
allowScopeSelection={!hasIdsToExport}
|
|
|
|
itemsQuantity={{
|
|
|
|
filter: filteredGiftCardsCount,
|
2022-06-21 09:36:55 +00:00
|
|
|
all: allGiftCardsCount,
|
2022-01-25 12:44:19 +00:00
|
|
|
}}
|
|
|
|
/>
|
2022-01-28 12:34:20 +00:00
|
|
|
<Typography className={classes.note} variant="body2">
|
2022-01-25 12:44:19 +00:00
|
|
|
{intl.formatMessage(messages.exportNote)}
|
|
|
|
</Typography>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</ContentWithProgress>
|
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
|
|
<ConfirmButton
|
|
|
|
transitionState={exportGiftCardsOpts.status}
|
2022-01-28 12:34:20 +00:00
|
|
|
variant="primary"
|
2022-01-25 12:44:19 +00:00
|
|
|
type="submit"
|
2022-12-13 07:04:54 +00:00
|
|
|
data-test-id="submit"
|
2022-01-25 12:44:19 +00:00
|
|
|
onClick={submit}
|
|
|
|
>
|
|
|
|
<FormattedMessage {...messages.confirmButtonLabel} />
|
|
|
|
</ConfirmButton>
|
|
|
|
</DialogActions>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default GiftCardExportDialog;
|