2021-08-16 13:44:00 +00:00
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
|
|
|
import { commonMessages } from "@saleor/intl";
|
|
|
|
import { ConfirmButton } from "@saleor/macaw-ui";
|
|
|
|
import commonErrorMessages from "@saleor/utils/errors/common";
|
2021-09-14 13:57:02 +00:00
|
|
|
import React from "react";
|
2021-08-16 13:44:00 +00:00
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
2021-09-14 13:57:02 +00:00
|
|
|
import { bulkEnableDisableSectionMessages as buttonMessages } from "../../GiftCardsList/GiftCardsListTable/GiftCardsListTableHeader/messages";
|
2021-08-16 13:44:00 +00:00
|
|
|
import useGiftCardDetails from "../providers/GiftCardDetailsProvider/hooks/useGiftCardDetails";
|
|
|
|
import { giftCardEnableDisableSectionMessages as messages } from "./messages";
|
|
|
|
import {
|
|
|
|
useGiftCardActivateMutation,
|
|
|
|
useGiftCardDeactivateMutation
|
|
|
|
} from "./mutations";
|
|
|
|
import { GiftCardActivate } from "./types/GiftCardActivate";
|
|
|
|
import { GiftCardDeactivate } from "./types/GiftCardDeactivate";
|
|
|
|
|
|
|
|
const GiftCardEnableDisableSection: React.FC = () => {
|
|
|
|
const notify = useNotifier();
|
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
const {
|
2021-09-20 13:41:18 +00:00
|
|
|
giftCard: { id, isActive, isExpired }
|
2021-08-16 13:44:00 +00:00
|
|
|
} = useGiftCardDetails();
|
|
|
|
|
2021-09-20 13:41:18 +00:00
|
|
|
if (isExpired) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-08-16 13:44:00 +00:00
|
|
|
const onActivateCompleted = (data: GiftCardActivate) => {
|
|
|
|
const errors = data?.giftCardActivate?.errors;
|
|
|
|
|
|
|
|
if (!!errors?.length) {
|
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: intl.formatMessage(commonErrorMessages.unknownError)
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
notify({
|
|
|
|
status: "success",
|
|
|
|
text: intl.formatMessage(messages.successfullyEnabledTitle)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDeactivateCompleted = (data: GiftCardDeactivate) => {
|
|
|
|
const errors = data?.giftCardDeactivate?.errors;
|
|
|
|
|
|
|
|
if (!!errors?.length) {
|
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: intl.formatMessage(commonErrorMessages.unknownError)
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
notify({
|
|
|
|
status: "success",
|
|
|
|
text: intl.formatMessage(messages.successfullyDisabledTitle)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const [giftCardActivate, giftCardActivateOpts] = useGiftCardActivateMutation({
|
|
|
|
onCompleted: onActivateCompleted
|
|
|
|
});
|
|
|
|
|
|
|
|
const [
|
|
|
|
giftCardDeactivate,
|
|
|
|
giftCardDeactivateOpts
|
|
|
|
] = useGiftCardDeactivateMutation({
|
|
|
|
onCompleted: onDeactivateCompleted
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleClick = () =>
|
|
|
|
isActive
|
|
|
|
? giftCardDeactivate({ variables: { id } })
|
|
|
|
: giftCardActivate({ variables: { id } });
|
|
|
|
|
2021-09-14 13:57:02 +00:00
|
|
|
const buttonLabel = isActive
|
|
|
|
? buttonMessages.disableLabel
|
|
|
|
: buttonMessages.enableLabel;
|
2021-08-16 13:44:00 +00:00
|
|
|
|
|
|
|
const currentOpts = isActive ? giftCardDeactivateOpts : giftCardActivateOpts;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ConfirmButton
|
|
|
|
onClick={handleClick}
|
|
|
|
transitionState={currentOpts?.status}
|
|
|
|
labels={{
|
|
|
|
confirm: intl.formatMessage(buttonLabel),
|
|
|
|
error: intl.formatMessage(commonMessages.error)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default GiftCardEnableDisableSection;
|