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

354 lines
12 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Typography from "@material-ui/core/Typography";
2019-08-09 10:26:22 +00:00
import React from "react";
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 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";
2019-08-09 11:14:35 +00:00
import { RequirementsPicker } from "@saleor/discounts/types";
2019-06-19 14:40:52 +00:00
import i18n from "../../../i18n";
2019-08-09 11:14:35 +00:00
import { maybe, splitDateTime } from "../../../misc";
2019-06-19 14:40:52 +00:00
import { ListProps, TabListActions, UserError } from "../../../types";
import {
2019-08-09 11:14:35 +00:00
DiscountValueTypeEnum,
VoucherTypeEnum
2019-06-19 14:40:52 +00:00
} from "../../../types/globalTypes";
import { VoucherDetails_voucher } from "../../types/VoucherDetails";
import DiscountCategories from "../DiscountCategories";
import DiscountCollections from "../DiscountCollections";
import DiscountProducts from "../DiscountProducts";
2019-08-09 11:14:35 +00:00
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";
2019-06-19 14:40:52 +00:00
import VoucherSummary from "../VoucherSummary";
2019-08-09 11:14:35 +00:00
import VoucherTypes from "../VoucherTypes";
import VoucherValue from "../VoucherValue";
2019-06-19 14:40:52 +00:00
export enum VoucherDetailsPageTab {
categories = "categories",
collections = "collections",
products = "products"
}
2019-08-09 11:14:35 +00:00
2019-06-19 14:40:52 +00:00
export function voucherDetailsPageTab(tab: string): VoucherDetailsPageTab {
return tab === VoucherDetailsPageTab.products
? VoucherDetailsPageTab.products
: tab === VoucherDetailsPageTab.collections
? VoucherDetailsPageTab.collections
: VoucherDetailsPageTab.categories;
}
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;
minAmountSpent: string;
minCheckoutItemsQuantity: string;
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 VoucherDetailsPageProps
extends Pick<ListProps, Exclude<keyof ListProps, "onRowClick">>,
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<VoucherDetailsPageProps> = ({
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
}) => {
2019-08-09 11:14:35 +00:00
let requirementsPickerInitValue;
if (maybe(() => voucher.minAmountSpent.amount) > 0) {
requirementsPickerInitValue = RequirementsPicker.ORDER;
} else if (maybe(() => voucher.minCheckoutItemsQuantity) > 0) {
requirementsPickerInitValue = RequirementsPicker.ITEM;
} else {
requirementsPickerInitValue = RequirementsPicker.NONE;
}
2019-06-19 14:40:52 +00:00
const initialForm: FormData = {
2019-08-09 11:14:35 +00:00
applyOncePerCustomer: maybe(() => voucher.applyOncePerCustomer, false),
2019-06-19 14:40:52 +00:00
applyOncePerOrder: maybe(() => voucher.applyOncePerOrder, false),
code: maybe(() => voucher.code, ""),
discountType: maybe(
() => voucher.discountValueType,
2019-08-09 11:14:35 +00:00
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"
2019-06-19 14:40:52 +00:00
),
2019-08-09 11:14:35 +00:00
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"),
2019-06-19 14:40:52 +00:00
value: maybe(() => voucher.discountValue, 0)
};
return (
<Form errors={errors} initial={initialForm} onSubmit={onSubmit}>
{({ change, data, errors: formErrors, hasChanged, submit }) => (
<Container>
<AppHeader onBack={onBack}>{i18n.t("Vouchers")}</AppHeader>
2019-08-09 11:14:35 +00:00
<PageHeader title={maybe(() => voucher.code)} />
2019-06-19 14:40:52 +00:00
<Grid>
<div>
<VoucherInfo
data={data}
disabled={disabled}
errors={formErrors}
onChange={change}
2019-08-09 11:14:35 +00:00
variant="update"
2019-06-19 14:40:52 +00:00
/>
<CardSpacer />
2019-08-09 11:14:35 +00:00
<VoucherTypes
2019-06-19 14:40:52 +00:00
data={data}
disabled={disabled}
errors={formErrors}
onChange={change}
/>
<CardSpacer />
2019-08-09 11:14:35 +00:00
{data.discountType.toString() !== "SHIPPING" ? (
<VoucherValue
data={data}
disabled={disabled}
defaultCurrency={defaultCurrency}
errors={formErrors}
onChange={change}
variant="update"
/>
) : null}
<CardSpacer />
{data.type === VoucherTypeEnum.SPECIFIC_PRODUCT &&
data.discountType.toString() !== "SHIPPING" ? (
2019-06-19 14:40:52 +00:00
<>
<TabContainer>
<CategoriesTab
isActive={activeTab === VoucherDetailsPageTab.categories}
changeTab={onTabClick}
>
{i18n.t("Categories ({{ number }})", {
number: maybe(
() => voucher.categories.totalCount.toString(),
"…"
)
})}
</CategoriesTab>
<CollectionsTab
isActive={activeTab === VoucherDetailsPageTab.collections}
changeTab={onTabClick}
>
{i18n.t("Collections ({{ number }})", {
number: maybe(
() => voucher.collections.totalCount.toString(),
"…"
)
})}
</CollectionsTab>
<ProductsTab
isActive={activeTab === VoucherDetailsPageTab.products}
changeTab={onTabClick}
>
{i18n.t("Products ({{ number }})", {
number: maybe(
() => voucher.products.totalCount.toString(),
"…"
)
})}
</ProductsTab>
</TabContainer>
<CardSpacer />
{activeTab === VoucherDetailsPageTab.categories ? (
<DiscountCategories
disabled={disabled}
onCategoryAssign={onCategoryAssign}
onCategoryUnassign={onCategoryUnassign}
onNextPage={onNextPage}
onPreviousPage={onPreviousPage}
onRowClick={onCategoryClick}
pageInfo={pageInfo}
discount={voucher}
isChecked={isChecked}
selected={selected}
toggle={toggle}
toggleAll={toggleAll}
toolbar={categoryListToolbar}
/>
) : activeTab === VoucherDetailsPageTab.collections ? (
<DiscountCollections
disabled={disabled}
onCollectionAssign={onCollectionAssign}
onCollectionUnassign={onCollectionUnassign}
onNextPage={onNextPage}
onPreviousPage={onPreviousPage}
onRowClick={onCollectionClick}
pageInfo={pageInfo}
discount={voucher}
isChecked={isChecked}
selected={selected}
toggle={toggle}
toggleAll={toggleAll}
toolbar={collectionListToolbar}
/>
) : (
<DiscountProducts
disabled={disabled}
onNextPage={onNextPage}
onPreviousPage={onPreviousPage}
onProductAssign={onProductAssign}
onProductUnassign={onProductUnassign}
onRowClick={onProductClick}
pageInfo={pageInfo}
discount={voucher}
isChecked={isChecked}
selected={selected}
toggle={toggle}
toggleAll={toggleAll}
toolbar={productListToolbar}
/>
)}
</>
2019-08-09 11:14:35 +00:00
) : null}
<CardSpacer />
{data.discountType.toString() === "SHIPPING" ? (
2019-06-19 14:40:52 +00:00
<CountryList
countries={maybe(() => voucher.countries)}
disabled={disabled}
emptyText={i18n.t("Voucher applies to all countries")}
title={
<>
2019-08-09 11:14:35 +00:00
{i18n.t("Countries")}
2019-06-19 14:40:52 +00:00
<Typography variant="caption">
{i18n.t("Vouchers limited to these countries")}
</Typography>
</>
}
onCountryAssign={onCountryAssign}
onCountryUnassign={onCountryUnassign}
/>
) : null}
2019-08-09 11:14:35 +00:00
<CardSpacer />
<VoucherRequirements
data={data}
disabled={disabled}
defaultCurrency={defaultCurrency}
errors={formErrors}
onChange={change}
/>
<CardSpacer />
<VoucherLimits
data={data}
disabled={disabled}
defaultCurrency={defaultCurrency}
errors={formErrors}
onChange={change}
/>
<CardSpacer />
<VoucherDates
data={data}
disabled={disabled}
defaultCurrency={defaultCurrency}
errors={formErrors}
onChange={change}
/>
2019-06-19 14:40:52 +00:00
</div>
<div>
<VoucherSummary
defaultCurrency={defaultCurrency}
voucher={voucher}
/>
</div>
</Grid>
<SaveButtonBar
disabled={disabled || !hasChanged}
onCancel={onBack}
onDelete={onRemove}
onSave={submit}
state={saveButtonBarState}
/>
</Container>
)}
</Form>
);
};
VoucherDetailsPage.displayName = "VoucherDetailsPage";
export default VoucherDetailsPage;