saleor-dashboard/src/discounts/views/VoucherCreate.tsx

96 lines
3.6 KiB
TypeScript
Raw Normal View History

2019-08-09 10:26:22 +00:00
import React from "react";
import { useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import { WindowTitle } from "@saleor/components/WindowTitle";
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
import useShop from "@saleor/hooks/useShop";
import { sectionNames } from "@saleor/intl";
2019-12-06 17:11:46 +00:00
import { decimal, joinDateTime, maybe } from "../../misc";
2019-06-19 14:40:52 +00:00
import {
DiscountValueTypeEnum,
VoucherTypeEnum
} from "../../types/globalTypes";
import VoucherCreatePage from "../components/VoucherCreatePage";
import { TypedVoucherCreate } from "../mutations";
2019-08-09 11:14:35 +00:00
import { RequirementsPicker } from "../types";
2019-06-19 14:40:52 +00:00
import { VoucherCreate } from "../types/VoucherCreate";
import { voucherListUrl, voucherUrl } from "../urls";
export const VoucherDetails: React.FC = () => {
2019-06-19 14:40:52 +00:00
const navigate = useNavigator();
const notify = useNotifier();
const shop = useShop();
const intl = useIntl();
2019-06-19 14:40:52 +00:00
const handleVoucherCreate = (data: VoucherCreate) => {
if (data.voucherCreate.errors.length === 0) {
notify({
text: intl.formatMessage({
defaultMessage: "Successfully created voucher"
2019-06-19 14:40:52 +00:00
})
});
navigate(voucherUrl(data.voucherCreate.voucher.id), true);
}
};
return (
<TypedVoucherCreate onCompleted={handleVoucherCreate}>
2019-12-06 17:11:46 +00:00
{(voucherCreate, voucherCreateOpts) => (
<>
<WindowTitle title={intl.formatMessage(sectionNames.vouchers)} />
<VoucherCreatePage
defaultCurrency={maybe(() => shop.defaultCurrency)}
disabled={voucherCreateOpts.loading}
errors={maybe(() => voucherCreateOpts.data.voucherCreate.errors)}
onBack={() => navigate(voucherListUrl())}
onSubmit={formData =>
voucherCreate({
variables: {
input: {
applyOncePerCustomer: formData.applyOncePerCustomer,
applyOncePerOrder: formData.applyOncePerOrder,
code: formData.code,
discountValue:
formData.discountType.toString() === "SHIPPING"
? 100
: decimal(formData.value),
discountValueType:
formData.discountType.toString() === "SHIPPING"
? DiscountValueTypeEnum.PERCENTAGE
: formData.discountType,
endDate: formData.hasEndDate
? joinDateTime(formData.endDate, formData.endTime)
: null,
minAmountSpent:
formData.requirementsPicker !== RequirementsPicker.ORDER
? 0
: parseFloat(formData.minAmountSpent),
minCheckoutItemsQuantity:
formData.requirementsPicker !== RequirementsPicker.ITEM
? 0
: parseFloat(formData.minCheckoutItemsQuantity),
startDate: joinDateTime(
formData.startDate,
formData.startTime
),
type:
formData.discountType.toString() === "SHIPPING"
? VoucherTypeEnum.ENTIRE_ORDER
: formData.type,
usageLimit: formData.hasUsageLimit
? parseInt(formData.usageLimit, 10)
: 0
2019-06-19 14:40:52 +00:00
}
2019-12-06 17:11:46 +00:00
}
})
}
2019-12-06 17:17:44 +00:00
saveButtonBarState={voucherCreateOpts.status}
2019-12-06 17:11:46 +00:00
/>
</>
)}
2019-06-19 14:40:52 +00:00
</TypedVoucherCreate>
);
};
export default VoucherDetails;