Skip to content

Commit

Permalink
[SALEOR-3965] Extend Sale & Voucher to include Metadata #7653 (#1245)
Browse files Browse the repository at this point in the history
* Add base logic

* Add missing metadata handlers

* Update changelog

* Cleanup schema

* Add messages
  • Loading branch information
piotrgrundas authored Jul 29, 2021
1 parent 6abdc46 commit cacf303
Show file tree
Hide file tree
Showing 40 changed files with 1,937 additions and 94 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Fix list pagination crash on search - #1230 by @orzechdev
- Fix positive float number input validation - #1233 by @orzechdev
- Use MacawUI - #1229 by @dominik-zeglen
- Add Metadata for Sale & Voucher - #7653 by @piotrgrundas
- Add variant create options dialog - #1238 by @orzechdev

# 2.11.1
Expand Down
8 changes: 6 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4839,12 +4839,14 @@ type RequestPasswordReset {
errors: [AccountError!]!
}

type Sale implements Node {
type Sale implements Node & ObjectWithMetadata {
id: ID!
name: String!
type: SaleType!
startDate: DateTime!
endDate: DateTime
privateMetadata: [MetadataItem]!
metadata: [MetadataItem]!
categories(before: String, after: String, first: Int, last: Int): CategoryCountableConnection
collections(before: String, after: String, first: Int, last: Int): CollectionCountableConnection
products(before: String, after: String, first: Int, last: Int): ProductCountableConnection
Expand Down Expand Up @@ -5757,7 +5759,7 @@ enum VolumeUnitsEnum {
ACRE_FT
}

type Voucher implements Node {
type Voucher implements Node & ObjectWithMetadata {
id: ID!
name: String
type: VoucherTypeEnum!
Expand All @@ -5771,6 +5773,8 @@ type Voucher implements Node {
onlyForStaff: Boolean!
discountValueType: DiscountValueTypeEnum!
minCheckoutItemsQuantity: Int
privateMetadata: [MetadataItem]!
metadata: [MetadataItem]!
categories(before: String, after: String, first: Int, last: Int): CategoryCountableConnection
collections(before: String, after: String, first: Int, last: Int): CollectionCountableConnection
products(before: String, after: String, first: Int, last: Int): ProductCountableConnection
Expand Down
14 changes: 12 additions & 2 deletions src/discounts/components/SaleCreatePage/SaleCreatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ 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 Metadata, { MetadataFormData } from "@saleor/components/Metadata";
import PageHeader from "@saleor/components/PageHeader";
import Savebar from "@saleor/components/Savebar";
import { createSaleChannelsChangeHandler } from "@saleor/discounts/handlers";
import { DiscountErrorFragment } from "@saleor/fragments/types/DiscountErrorFragment";
import { sectionNames } from "@saleor/intl";
import { Backlink } from "@saleor/macaw-ui";
import { validatePrice } from "@saleor/products/utils/validation";
import useMetadataChangeTrigger from "@saleor/utils/metadata/useMetadataChangeTrigger";
import React from "react";
import { useIntl } from "react-intl";

Expand All @@ -24,7 +26,7 @@ import SaleInfo from "../SaleInfo";
import SaleType from "../SaleType";
import SaleValue from "../SaleValue";

export interface FormData {
export interface FormData extends MetadataFormData {
channelListings: ChannelSaleData[];
endDate: string;
endTime: string;
Expand Down Expand Up @@ -60,6 +62,9 @@ const SaleCreatePage: React.FC<SaleCreatePageProps> = ({
onBack
}) => {
const intl = useIntl();
const {
makeChangeHandler: makeMetadataChangeHandler
} = useMetadataChangeTrigger();

const initialForm: FormData = {
channelListings,
Expand All @@ -70,7 +75,9 @@ const SaleCreatePage: React.FC<SaleCreatePageProps> = ({
startDate: "",
startTime: "",
type: SaleTypeEnum.FIXED,
value: ""
value: "",
metadata: [],
privateMetadata: []
};
return (
<Form initial={initialForm} onSubmit={onSubmit}>
Expand All @@ -83,6 +90,8 @@ const SaleCreatePage: React.FC<SaleCreatePageProps> = ({
const formDisabled = data.channelListings?.some(channel =>
validatePrice(channel?.discountValue)
);
const changeMetadata = makeMetadataChangeHandler(change);

return (
<Container>
<Backlink onClick={onBack}>
Expand Down Expand Up @@ -132,6 +141,7 @@ const SaleCreatePage: React.FC<SaleCreatePageProps> = ({
openModal={openChannelsModal}
/>
</div>
<Metadata data={data} onChange={changeMetadata} />
</Grid>
<Savebar
disabled={disabled || formDisabled || !hasChanged}
Expand Down
27 changes: 19 additions & 8 deletions src/discounts/components/SaleDetailsPage/SaleDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 Metadata, { MetadataFormData } from "@saleor/components/Metadata";
import PageHeader from "@saleor/components/PageHeader";
import Savebar from "@saleor/components/Savebar";
import { Tab, TabContainer } from "@saleor/components/Tab";
Expand All @@ -13,6 +14,8 @@ import { DiscountErrorFragment } from "@saleor/fragments/types/DiscountErrorFrag
import { sectionNames } from "@saleor/intl";
import { Backlink } from "@saleor/macaw-ui";
import { validatePrice } from "@saleor/products/utils/validation";
import { mapMetadataItemToInput } from "@saleor/utils/maps";
import useMetadataChangeTrigger from "@saleor/utils/metadata/useMetadataChangeTrigger";
import React from "react";
import { useIntl } from "react-intl";

Expand All @@ -32,7 +35,7 @@ import SaleSummary from "../SaleSummary";
import SaleType from "../SaleType";
import SaleValue from "../SaleValue";

export interface SaleDetailsPageFormData {
export interface SaleDetailsPageFormData extends MetadataFormData {
channelListings: ChannelSaleData[];
endDate: string;
endTime: string;
Expand Down Expand Up @@ -127,16 +130,21 @@ const SaleDetailsPage: React.FC<SaleDetailsPageProps> = ({
toggleAll
}) => {
const intl = useIntl();
const {
makeChangeHandler: makeMetadataChangeHandler
} = useMetadataChangeTrigger();

const initialForm: SaleDetailsPageFormData = {
channelListings,
endDate: splitDateTime(maybe(() => sale.endDate, "")).date,
endTime: splitDateTime(maybe(() => sale.endDate, "")).time,
hasEndDate: maybe(() => !!sale.endDate),
name: maybe(() => sale.name, ""),
startDate: splitDateTime(maybe(() => sale.startDate, "")).date,
startTime: splitDateTime(maybe(() => sale.startDate, "")).time,
type: maybe(() => sale.type, SaleTypeEnum.FIXED)
endDate: splitDateTime(sale?.endDate ?? "").date,
endTime: splitDateTime(sale?.endDate ?? "").time,
hasEndDate: !!sale?.endDate,
name: sale?.name ?? "",
startDate: splitDateTime(sale?.startDate ?? "").date,
startTime: splitDateTime(sale?.startDate ?? "").time,
type: sale?.type ?? SaleTypeEnum.FIXED,
metadata: sale?.metadata.map(mapMetadataItemToInput),
privateMetadata: sale?.privateMetadata.map(mapMetadataItemToInput)
};
return (
<Form initial={initialForm} onSubmit={onSubmit}>
Expand All @@ -149,6 +157,8 @@ const SaleDetailsPage: React.FC<SaleDetailsPageProps> = ({
const formDisabled = data.channelListings?.some(channel =>
validatePrice(channel.discountValue)
);
const changeMetadata = makeMetadataChangeHandler(change);

return (
<Container>
<Backlink onClick={onBack}>
Expand Down Expand Up @@ -307,6 +317,7 @@ const SaleDetailsPage: React.FC<SaleDetailsPageProps> = ({
openModal={openChannelsModal}
/>
</div>
<Metadata data={data} onChange={changeMetadata} />
</Grid>
<Savebar
disabled={
Expand Down
14 changes: 12 additions & 2 deletions src/discounts/components/VoucherCreatePage/VoucherCreatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 Metadata, { MetadataFormData } from "@saleor/components/Metadata";
import PageHeader from "@saleor/components/PageHeader";
import Savebar from "@saleor/components/Savebar";
import {
Expand All @@ -15,6 +16,7 @@ import { DiscountErrorFragment } from "@saleor/fragments/types/DiscountErrorFrag
import { sectionNames } from "@saleor/intl";
import { Backlink } from "@saleor/macaw-ui";
import { validatePrice } from "@saleor/products/utils/validation";
import useMetadataChangeTrigger from "@saleor/utils/metadata/useMetadataChangeTrigger";
import React from "react";
import { useIntl } from "react-intl";

Expand All @@ -27,7 +29,7 @@ import VoucherRequirements from "../VoucherRequirements";
import VoucherTypes from "../VoucherTypes";
import VoucherValue from "../VoucherValue";

export interface FormData {
export interface FormData extends MetadataFormData {
applyOncePerCustomer: boolean;
applyOncePerOrder: boolean;
onlyForStaff: boolean;
Expand Down Expand Up @@ -73,6 +75,9 @@ const VoucherCreatePage: React.FC<VoucherCreatePageProps> = ({
openChannelsModal
}) => {
const intl = useIntl();
const {
makeChangeHandler: makeMetadataChangeHandler
} = useMetadataChangeTrigger();

const initialForm: FormData = {
applyOncePerCustomer: false,
Expand All @@ -91,7 +96,9 @@ const VoucherCreatePage: React.FC<VoucherCreatePageProps> = ({
startTime: "",
type: VoucherTypeEnum.ENTIRE_ORDER,
usageLimit: "0",
value: 0
value: 0,
metadata: [],
privateMetadata: []
};

return (
Expand All @@ -113,6 +120,8 @@ const VoucherCreatePage: React.FC<VoucherCreatePageProps> = ({
(data.requirementsPicker === RequirementsPicker.ORDER &&
validatePrice(channel.minSpent))
);
const changeMetadata = makeMetadataChangeHandler(change);

return (
<Container>
<Backlink onClick={onBack}>
Expand Down Expand Up @@ -189,6 +198,7 @@ const VoucherCreatePage: React.FC<VoucherCreatePageProps> = ({
openModal={openChannelsModal}
/>
</div>
<Metadata data={data} onChange={changeMetadata} />
</Grid>
<Savebar
disabled={
Expand Down
35 changes: 22 additions & 13 deletions src/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 Metadata, { MetadataFormData } from "@saleor/components/Metadata";
import PageHeader from "@saleor/components/PageHeader";
import Savebar from "@saleor/components/Savebar";
import { Tab, TabContainer } from "@saleor/components/Tab";
Expand All @@ -19,6 +20,8 @@ import { DiscountErrorFragment } from "@saleor/fragments/types/DiscountErrorFrag
import { sectionNames } from "@saleor/intl";
import { Backlink } from "@saleor/macaw-ui";
import { validatePrice } from "@saleor/products/utils/validation";
import { mapMetadataItemToInput } from "@saleor/utils/maps";
import useMetadataChangeTrigger from "@saleor/utils/metadata/useMetadataChangeTrigger";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";

Expand Down Expand Up @@ -55,7 +58,7 @@ export function voucherDetailsPageTab(tab: string): VoucherDetailsPageTab {
: VoucherDetailsPageTab.categories;
}

export interface VoucherDetailsPageFormData {
export interface VoucherDetailsPageFormData extends MetadataFormData {
applyOncePerCustomer: boolean;
applyOncePerOrder: boolean;
onlyForStaff: boolean;
Expand Down Expand Up @@ -149,6 +152,9 @@ const VoucherDetailsPage: React.FC<VoucherDetailsPageProps> = ({
productListToolbar
}) => {
const intl = useIntl();
const {
makeChangeHandler: makeMetadataChangeHandler
} = useMetadataChangeTrigger();
const channel = voucher?.channelListings?.find(
listing => listing.channel.id === selectedChannelId
);
Expand All @@ -175,19 +181,19 @@ const VoucherDetailsPage: React.FC<VoucherDetailsPageProps> = ({
channelListings,
code: voucher?.code || "",
discountType,
endDate: splitDateTime(maybe(() => voucher.endDate, "")).date,
endTime: splitDateTime(maybe(() => voucher.endDate, "")).time,
hasEndDate: maybe(() => !!voucher.endDate),
hasUsageLimit: maybe(() => !!voucher.usageLimit),
minCheckoutItemsQuantity: maybe(
() => voucher.minCheckoutItemsQuantity.toString(),
"0"
),
endDate: splitDateTime(voucher?.endDate ?? "").date,
endTime: splitDateTime(voucher?.endDate ?? "").time,
hasEndDate: !!voucher?.endDate,
hasUsageLimit: !!voucher?.usageLimit,
minCheckoutItemsQuantity:
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")
startDate: splitDateTime(voucher?.startDate ?? "").date,
startTime: splitDateTime(voucher?.startDate ?? "").time,
type: voucher?.type ?? VoucherTypeEnum.ENTIRE_ORDER,
usageLimit: voucher?.usageLimit?.toString() ?? "0",
metadata: voucher?.metadata.map(mapMetadataItemToInput),
privateMetadata: voucher?.privateMetadata.map(mapMetadataItemToInput)
};

return (
Expand All @@ -209,6 +215,8 @@ const VoucherDetailsPage: React.FC<VoucherDetailsPageProps> = ({
(data.requirementsPicker === RequirementsPicker.ORDER &&
validatePrice(channel.minSpent))
);
const changeMetadata = makeMetadataChangeHandler(change);

return (
<Container>
<Backlink onClick={onBack}>
Expand Down Expand Up @@ -420,6 +428,7 @@ const VoucherDetailsPage: React.FC<VoucherDetailsPageProps> = ({
openModal={openChannelsModal}
/>
</div>
<Metadata data={data} onChange={changeMetadata} />
</Grid>
<Savebar
disabled={
Expand Down
Loading

0 comments on commit cacf303

Please sign in to comment.