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

135 lines
4.2 KiB
TypeScript
Raw Normal View History

2019-08-09 10:17:04 +00:00
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
2019-10-28 16:16:49 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-08-09 10:17:04 +00:00
import Typography from "@material-ui/core/Typography";
import CardTitle from "@saleor/components/CardTitle";
2019-09-09 09:28:06 +00:00
import ControlledCheckbox from "@saleor/components/ControlledCheckbox";
2019-08-09 10:17:04 +00:00
import { FormSpacer } from "@saleor/components/FormSpacer";
import Hr from "@saleor/components/Hr";
import RadioGroupField from "@saleor/components/RadioGroupField";
import TextFieldWithChoice from "@saleor/components/TextFieldWithChoice";
import { DiscountErrorFragment } from "@saleor/fragments/types/DiscountErrorFragment";
2020-03-23 19:22:40 +00:00
import { getFormErrors } from "@saleor/utils/errors";
import getDiscountErrorMessage from "@saleor/utils/errors/discounts";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-08-09 10:17:04 +00:00
import { DiscountValueTypeEnum } from "../../../types/globalTypes";
import { translateVoucherTypes } from "../../translations";
import { FormData } from "../VoucherDetailsPage";
interface VoucherValueProps {
data: FormData;
defaultCurrency: string;
2020-03-23 19:22:40 +00:00
errors: DiscountErrorFragment[];
2019-08-09 10:17:04 +00:00
disabled: boolean;
variant: string;
onChange: (event: React.ChangeEvent<any>) => void;
}
export enum VoucherType {
ENTIRE_ORDER = "ENTIRE_ORDER",
SPECIFIC_PRODUCT = "SPECIFIC_PRODUCT"
}
2019-10-10 12:45:25 +00:00
const useStyles = makeStyles(
2019-10-28 16:16:49 +00:00
theme => ({
2019-10-10 12:45:25 +00:00
hr: {
2019-10-28 16:16:49 +00:00
margin: theme.spacing(2, 0)
2019-10-10 12:45:25 +00:00
}
}),
{
name: "VoucherValue"
}
);
const VoucherValue: React.FC<VoucherValueProps> = props => {
const { data, defaultCurrency, disabled, errors, variant, onChange } = props;
const classes = useStyles(props);
const intl = useIntl();
2020-03-23 19:22:40 +00:00
const formErrors = getFormErrors(["discountValue", "type"], errors);
2019-08-29 10:55:56 +00:00
const translatedVoucherTypes = translateVoucherTypes(intl);
2019-08-09 10:17:04 +00:00
const voucherTypeChoices = Object.values(VoucherType).map(type => ({
label: translatedVoucherTypes[type],
value: type
}));
return (
<Card>
<CardTitle
title={intl.formatMessage({
defaultMessage: "Value",
description: "section header"
})}
/>
2019-08-09 10:17:04 +00:00
<CardContent>
<TextFieldWithChoice
disabled={disabled}
2020-03-23 19:22:40 +00:00
error={!!formErrors.discountValue}
2019-08-09 10:17:04 +00:00
ChoiceProps={{
label:
data.discountType === DiscountValueTypeEnum.FIXED
? defaultCurrency
: "%",
name: "discountType" as keyof FormData,
values: null
}}
2020-03-23 19:22:40 +00:00
helperText={getDiscountErrorMessage(formErrors.discountValue, intl)}
2019-08-09 10:17:04 +00:00
name={"value" as keyof FormData}
onChange={onChange}
label={intl.formatMessage({
defaultMessage: "Discount Value"
})}
2019-08-09 10:17:04 +00:00
value={data.value}
type="number"
fullWidth
inputProps={{
min: 0
}}
/>
<FormSpacer />
{variant === "update" && (
<>
2019-10-10 12:45:25 +00:00
<Hr className={classes.hr} />
2019-08-09 10:17:04 +00:00
<RadioGroupField
choices={voucherTypeChoices}
disabled={disabled}
2020-03-23 19:22:40 +00:00
error={!!formErrors.type}
hint={getDiscountErrorMessage(formErrors.type, intl)}
label={intl.formatMessage({
2019-10-10 12:45:25 +00:00
defaultMessage: "Voucher Specific Information"
})}
2019-08-09 10:17:04 +00:00
name={"type" as keyof FormData}
value={data.type}
onChange={onChange}
/>
</>
)}
2019-10-10 12:45:25 +00:00
<Hr className={classes.hr} />
2019-08-09 10:17:04 +00:00
<FormSpacer />
2019-09-09 09:28:06 +00:00
<ControlledCheckbox
name={"applyOncePerOrder" as keyof FormData}
2019-08-09 10:17:04 +00:00
label={
<>
<FormattedMessage
defaultMessage="Only once per order"
description="voucher application, switch button"
/>
2019-08-09 10:17:04 +00:00
<Typography variant="caption">
<FormattedMessage defaultMessage="If this option is disabled, discount will be counted for every eligible product" />
2019-08-09 10:17:04 +00:00
</Typography>
</>
}
2019-09-09 09:28:06 +00:00
checked={data.applyOncePerOrder}
2019-08-09 10:17:04 +00:00
onChange={onChange}
disabled={disabled}
/>
</CardContent>
</Card>
);
};
export default VoucherValue;