saleor-dashboard/src/discounts/components/VoucherCreatePage/VoucherCreatePage.tsx

161 lines
4.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 AppHeader from "@saleor/components/AppHeader";
import CardSpacer from "@saleor/components/CardSpacer";
import { ConfirmButtonTransitionState } from "@saleor/components/ConfirmButton";
import Container from "@saleor/components/Container";
import Form from "@saleor/components/Form";
import Grid from "@saleor/components/Grid";
import PageHeader from "@saleor/components/PageHeader";
import SaveButtonBar from "@saleor/components/SaveButtonBar";
2019-12-02 10:49:14 +00:00
import { sectionNames } from "@saleor/intl";
2020-03-23 19:22:40 +00:00
import { DiscountErrorFragment } from "@saleor/discounts/types/DiscountErrorFragment";
2019-06-19 14:40:52 +00:00
import {
2019-08-09 11:14:35 +00:00
DiscountValueTypeEnum,
VoucherTypeEnum
2019-06-19 14:40:52 +00:00
} from "../../../types/globalTypes";
2019-08-09 11:14:35 +00:00
import { RequirementsPicker } from "../../types";
import VoucherDates from "../VoucherDates";
2019-06-19 14:40:52 +00:00
import VoucherInfo from "../VoucherInfo";
2019-08-09 11:14:35 +00:00
import VoucherLimits from "../VoucherLimits";
import VoucherRequirements from "../VoucherRequirements";
import VoucherTypes from "../VoucherTypes";
import VoucherValue from "../VoucherValue";
2020-03-23 19:22:40 +00:00
2019-06-19 14:40:52 +00:00
export interface FormData {
2019-08-09 11:14:35 +00:00
applyOncePerCustomer: boolean;
2019-06-19 14:40:52 +00:00
applyOncePerOrder: boolean;
code: string;
2019-08-09 11:14:35 +00:00
discountType: DiscountValueTypeEnum;
2019-06-19 14:40:52 +00:00
endDate: string;
2019-08-09 11:14:35 +00:00
endTime: string;
hasEndDate: boolean;
hasUsageLimit: boolean;
minCheckoutItemsQuantity: string;
2020-01-09 11:13:24 +00:00
minSpent: string;
2019-08-09 11:14:35 +00:00
requirementsPicker: RequirementsPicker;
2019-06-19 14:40:52 +00:00
startDate: string;
2019-08-09 11:14:35 +00:00
startTime: string;
type: VoucherTypeEnum;
usageLimit: string;
2019-06-19 14:40:52 +00:00
value: number;
}
export interface VoucherCreatePageProps {
defaultCurrency: string;
disabled: boolean;
2020-03-23 19:22:40 +00:00
errors: DiscountErrorFragment[];
2019-06-19 14:40:52 +00:00
saveButtonBarState: ConfirmButtonTransitionState;
onBack: () => void;
onSubmit: (data: FormData) => void;
}
const VoucherCreatePage: React.FC<VoucherCreatePageProps> = ({
2019-06-19 14:40:52 +00:00
defaultCurrency,
disabled,
errors,
saveButtonBarState,
onBack,
onSubmit
}) => {
const intl = useIntl();
2019-06-19 14:40:52 +00:00
const initialForm: FormData = {
2019-08-09 11:14:35 +00:00
applyOncePerCustomer: false,
2019-06-19 14:40:52 +00:00
applyOncePerOrder: false,
code: "",
2019-08-09 11:14:35 +00:00
discountType: DiscountValueTypeEnum.FIXED,
2019-06-19 14:40:52 +00:00
endDate: "",
2019-08-09 11:14:35 +00:00
endTime: "",
hasEndDate: false,
hasUsageLimit: false,
minCheckoutItemsQuantity: "0",
2020-01-09 11:13:24 +00:00
minSpent: "0",
2019-08-09 11:14:35 +00:00
requirementsPicker: RequirementsPicker.NONE,
2019-06-19 14:40:52 +00:00
startDate: "",
2019-08-09 11:14:35 +00:00
startTime: "",
type: VoucherTypeEnum.ENTIRE_ORDER,
usageLimit: "0",
2019-06-19 14:40:52 +00:00
value: 0
};
return (
2020-02-24 14:14:48 +00:00
<Form initial={initialForm} onSubmit={onSubmit}>
{({ change, data, hasChanged, submit }) => (
2019-06-19 14:40:52 +00:00
<Container>
<AppHeader onBack={onBack}>
{intl.formatMessage(sectionNames.vouchers)}
</AppHeader>
<PageHeader
title={intl.formatMessage({
defaultMessage: "Create Voucher",
description: "page header"
})}
/>
2019-06-19 14:40:52 +00:00
<Grid>
<div>
<VoucherInfo
data={data}
2020-02-24 14:14:48 +00:00
errors={errors}
2019-06-19 14:40:52 +00:00
disabled={disabled}
2019-08-09 11:14:35 +00:00
onChange={change}
2019-06-19 14:40:52 +00:00
variant="create"
2019-08-09 11:14:35 +00:00
/>
<CardSpacer />
<VoucherTypes
data={data}
disabled={disabled}
2020-02-24 14:14:48 +00:00
errors={errors}
2019-06-19 14:40:52 +00:00
onChange={change}
/>
2019-08-09 11:14:35 +00:00
{data.discountType.toString() !== "SHIPPING" ? (
<VoucherValue
data={data}
disabled={disabled}
defaultCurrency={defaultCurrency}
2020-02-24 14:14:48 +00:00
errors={errors}
2019-08-09 11:14:35 +00:00
onChange={change}
variant="create"
/>
) : null}
2019-06-19 14:40:52 +00:00
<CardSpacer />
2019-08-09 11:14:35 +00:00
<VoucherRequirements
2019-06-19 14:40:52 +00:00
data={data}
disabled={disabled}
defaultCurrency={defaultCurrency}
2020-02-24 14:14:48 +00:00
errors={errors}
2019-06-19 14:40:52 +00:00
onChange={change}
/>
<CardSpacer />
2019-08-09 11:14:35 +00:00
<VoucherLimits
data={data}
disabled={disabled}
defaultCurrency={defaultCurrency}
2020-02-24 14:14:48 +00:00
errors={errors}
2019-08-09 11:14:35 +00:00
onChange={change}
/>
<CardSpacer />
<VoucherDates
data={data}
disabled={disabled}
defaultCurrency={defaultCurrency}
2020-02-24 14:14:48 +00:00
errors={errors}
2019-08-09 11:14:35 +00:00
onChange={change}
/>
2019-06-19 14:40:52 +00:00
</div>
</Grid>
<SaveButtonBar
disabled={disabled || !hasChanged}
onCancel={onBack}
onSave={submit}
state={saveButtonBarState}
/>
</Container>
)}
</Form>
);
};
VoucherCreatePage.displayName = "VoucherCreatePage";
export default VoucherCreatePage;