import Typography from "@material-ui/core/Typography"; import React from "react"; 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 CountryList from "@saleor/components/CountryList"; import Form from "@saleor/components/Form"; import Grid from "@saleor/components/Grid"; import PageHeader from "@saleor/components/PageHeader"; import SaveButtonBar from "@saleor/components/SaveButtonBar"; import { Tab, TabContainer } from "@saleor/components/Tab"; import { RequirementsPicker } from "@saleor/discounts/types"; import i18n from "../../../i18n"; import { maybe, splitDateTime } from "../../../misc"; import { ListProps, TabListActions, UserError } from "../../../types"; import { DiscountValueTypeEnum, VoucherTypeEnum } from "../../../types/globalTypes"; import { VoucherDetails_voucher } from "../../types/VoucherDetails"; import DiscountCategories from "../DiscountCategories"; import DiscountCollections from "../DiscountCollections"; import DiscountProducts from "../DiscountProducts"; import VoucherDates from "../VoucherDates"; import VoucherInfo from "../VoucherInfo"; import VoucherLimits from "../VoucherLimits"; import VoucherRequirements from "../VoucherRequirements"; import VoucherSummary from "../VoucherSummary"; import VoucherTypes from "../VoucherTypes"; import VoucherValue from "../VoucherValue"; export enum VoucherDetailsPageTab { categories = "categories", collections = "collections", products = "products" } export function voucherDetailsPageTab(tab: string): VoucherDetailsPageTab { return tab === VoucherDetailsPageTab.products ? VoucherDetailsPageTab.products : tab === VoucherDetailsPageTab.collections ? VoucherDetailsPageTab.collections : VoucherDetailsPageTab.categories; } export interface FormData { applyOncePerCustomer: boolean; applyOncePerOrder: boolean; code: string; discountType: DiscountValueTypeEnum; endDate: string; endTime: string; hasEndDate: boolean; hasUsageLimit: boolean; minAmountSpent: string; minCheckoutItemsQuantity: string; requirementsPicker: RequirementsPicker; startDate: string; startTime: string; type: VoucherTypeEnum; usageLimit: string; value: number; } export interface VoucherDetailsPageProps extends Pick>, TabListActions< "categoryListToolbar" | "collectionListToolbar" | "productListToolbar" > { activeTab: VoucherDetailsPageTab; defaultCurrency: string; errors: UserError[]; saveButtonBarState: ConfirmButtonTransitionState; voucher: VoucherDetails_voucher; onBack: () => void; onCategoryAssign: () => void; onCategoryUnassign: (id: string) => void; onCategoryClick: (id: string) => () => void; onCollectionAssign: () => void; onCollectionUnassign: (id: string) => void; onCollectionClick: (id: string) => () => void; onCountryAssign: () => void; onCountryUnassign: (code: string) => void; onProductAssign: () => void; onProductUnassign: (id: string) => void; onProductClick: (id: string) => () => void; onRemove: () => void; onSubmit: (data: FormData) => void; onTabClick: (index: VoucherDetailsPageTab) => void; } const CategoriesTab = Tab(VoucherDetailsPageTab.categories); const CollectionsTab = Tab(VoucherDetailsPageTab.collections); const ProductsTab = Tab(VoucherDetailsPageTab.products); const VoucherDetailsPage: React.StatelessComponent = ({ activeTab, defaultCurrency, disabled, errors, pageInfo, saveButtonBarState, voucher, onBack, onCategoryAssign, onCategoryClick, onCategoryUnassign, onCountryAssign, onCountryUnassign, onCollectionAssign, onCollectionClick, onCollectionUnassign, onNextPage, onPreviousPage, onProductAssign, onProductClick, onProductUnassign, onTabClick, onRemove, onSubmit, toggle, toggleAll, selected, isChecked, categoryListToolbar, collectionListToolbar, productListToolbar }) => { let requirementsPickerInitValue; if (maybe(() => voucher.minAmountSpent.amount) > 0) { requirementsPickerInitValue = RequirementsPicker.ORDER; } else if (maybe(() => voucher.minCheckoutItemsQuantity) > 0) { requirementsPickerInitValue = RequirementsPicker.ITEM; } else { requirementsPickerInitValue = RequirementsPicker.NONE; } const initialForm: FormData = { applyOncePerCustomer: maybe(() => voucher.applyOncePerCustomer, false), applyOncePerOrder: maybe(() => voucher.applyOncePerOrder, false), code: maybe(() => voucher.code, ""), discountType: maybe( () => voucher.discountValueType, DiscountValueTypeEnum.FIXED ), endDate: splitDateTime(maybe(() => voucher.endDate, "")).date, endTime: splitDateTime(maybe(() => voucher.endDate, "")).time, hasEndDate: maybe(() => !!voucher.endDate), hasUsageLimit: maybe(() => !!voucher.usageLimit), minAmountSpent: maybe(() => voucher.minAmountSpent.amount.toString(), "0"), minCheckoutItemsQuantity: maybe( () => voucher.minCheckoutItemsQuantity.toString(), "0" ), requirementsPicker: requirementsPickerInitValue, startDate: splitDateTime(maybe(() => voucher.startDate, "")).date, startTime: splitDateTime(maybe(() => voucher.startDate, "")).time, type: maybe(() => voucher.type, VoucherTypeEnum.ENTIRE_ORDER), usageLimit: maybe(() => voucher.usageLimit.toString(), "0"), value: maybe(() => voucher.discountValue, 0) }; return (
{({ change, data, errors: formErrors, hasChanged, submit }) => ( {i18n.t("Vouchers")} voucher.code)} />
{data.discountType.toString() !== "SHIPPING" ? ( ) : null} {data.type === VoucherTypeEnum.SPECIFIC_PRODUCT && data.discountType.toString() !== "SHIPPING" ? ( <> {i18n.t("Categories ({{ number }})", { number: maybe( () => voucher.categories.totalCount.toString(), "…" ) })} {i18n.t("Collections ({{ number }})", { number: maybe( () => voucher.collections.totalCount.toString(), "…" ) })} {i18n.t("Products ({{ number }})", { number: maybe( () => voucher.products.totalCount.toString(), "…" ) })} {activeTab === VoucherDetailsPageTab.categories ? ( ) : activeTab === VoucherDetailsPageTab.collections ? ( ) : ( )} ) : null} {data.discountType.toString() === "SHIPPING" ? ( voucher.countries)} disabled={disabled} emptyText={i18n.t("Voucher applies to all countries")} title={ <> {i18n.t("Countries")} {i18n.t("Vouchers limited to these countries")} } onCountryAssign={onCountryAssign} onCountryUnassign={onCountryUnassign} /> ) : null}
)}
); }; VoucherDetailsPage.displayName = "VoucherDetailsPage"; export default VoucherDetailsPage;