Change values in create sale page when percentage/fixed amount is switched (#1431)
* WIP * Fix get sale channel bug * Add sale value field text * Extract logic to useEffect * Code cleanup
This commit is contained in:
parent
9c92332d3c
commit
e018ea5b7d
4 changed files with 94 additions and 25 deletions
|
@ -4,7 +4,6 @@ import {
|
||||||
TableBody,
|
TableBody,
|
||||||
TableCell,
|
TableCell,
|
||||||
TableRow,
|
TableRow,
|
||||||
TextField,
|
|
||||||
Typography
|
Typography
|
||||||
} from "@material-ui/core";
|
} from "@material-ui/core";
|
||||||
import CardTitle from "@saleor/components/CardTitle";
|
import CardTitle from "@saleor/components/CardTitle";
|
||||||
|
@ -13,20 +12,21 @@ import Skeleton from "@saleor/components/Skeleton";
|
||||||
import TableHead from "@saleor/components/TableHead";
|
import TableHead from "@saleor/components/TableHead";
|
||||||
import { DiscountErrorFragment } from "@saleor/fragments/types/DiscountErrorFragment";
|
import { DiscountErrorFragment } from "@saleor/fragments/types/DiscountErrorFragment";
|
||||||
import { renderCollection } from "@saleor/misc";
|
import { renderCollection } from "@saleor/misc";
|
||||||
import { SaleType } from "@saleor/types/globalTypes";
|
|
||||||
import { getFormErrors } from "@saleor/utils/errors";
|
import { getFormErrors } from "@saleor/utils/errors";
|
||||||
import getDiscountErrorMessage from "@saleor/utils/errors/discounts";
|
import getDiscountErrorMessage from "@saleor/utils/errors/discounts";
|
||||||
import React from "react";
|
import * as React from "react";
|
||||||
import { FormattedMessage, useIntl } from "react-intl";
|
import { FormattedMessage, useIntl } from "react-intl";
|
||||||
|
|
||||||
import { SaleDetailsPageFormData } from "../SaleDetailsPage";
|
import { SaleDetailsPageFormData } from "../SaleDetailsPage";
|
||||||
|
import SaleValueTextField from "./SaleValueTextField";
|
||||||
import { useStyles } from "./styles";
|
import { useStyles } from "./styles";
|
||||||
|
import { SaleValueInputChangeType } from "./types";
|
||||||
|
|
||||||
export interface SaleValueProps {
|
export interface SaleValueProps {
|
||||||
data: SaleDetailsPageFormData;
|
data: SaleDetailsPageFormData;
|
||||||
disabled: boolean;
|
disabled: boolean;
|
||||||
errors: DiscountErrorFragment[];
|
errors: DiscountErrorFragment[];
|
||||||
onChange: (channelId: string, discountValue: string) => void;
|
onChange: SaleValueInputChangeType;
|
||||||
}
|
}
|
||||||
|
|
||||||
const numberOfColumns = 2;
|
const numberOfColumns = 2;
|
||||||
|
@ -37,6 +37,7 @@ const SaleValue: React.FC<SaleValueProps> = ({
|
||||||
errors,
|
errors,
|
||||||
onChange
|
onChange
|
||||||
}) => {
|
}) => {
|
||||||
|
const { type } = data;
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const classes = useStyles({});
|
const classes = useStyles({});
|
||||||
const formErrors = getFormErrors(["value"], errors);
|
const formErrors = getFormErrors(["value"], errors);
|
||||||
|
@ -93,8 +94,8 @@ const SaleValue: React.FC<SaleValueProps> = ({
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
{listing ? (
|
{listing ? (
|
||||||
<TextField
|
<SaleValueTextField
|
||||||
disabled={disabled}
|
dataType={type}
|
||||||
helperText={
|
helperText={
|
||||||
error
|
error
|
||||||
? getDiscountErrorMessage(
|
? getDiscountErrorMessage(
|
||||||
|
@ -103,24 +104,9 @@ const SaleValue: React.FC<SaleValueProps> = ({
|
||||||
)
|
)
|
||||||
: ""
|
: ""
|
||||||
}
|
}
|
||||||
name="value"
|
disabled={disabled}
|
||||||
onChange={e => onChange(listing.id, e.target.value)}
|
listing={listing}
|
||||||
label={intl.formatMessage({
|
onChange={onChange}
|
||||||
defaultMessage: "Discount Value",
|
|
||||||
description: "sale discount"
|
|
||||||
})}
|
|
||||||
value={listing.discountValue || ""}
|
|
||||||
type="number"
|
|
||||||
fullWidth
|
|
||||||
inputProps={{
|
|
||||||
min: 0
|
|
||||||
}}
|
|
||||||
InputProps={{
|
|
||||||
endAdornment:
|
|
||||||
data.type === SaleType.FIXED
|
|
||||||
? listing.currency
|
|
||||||
: "%"
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<Skeleton />
|
<Skeleton />
|
||||||
|
|
79
src/discounts/components/SaleValue/SaleValueTextField.tsx
Normal file
79
src/discounts/components/SaleValue/SaleValueTextField.tsx
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
import { TextField } from "@material-ui/core";
|
||||||
|
import { ChannelSaleData } from "@saleor/channels/utils";
|
||||||
|
import { SaleType } from "@saleor/types/globalTypes";
|
||||||
|
import React from "react";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { useIntl } from "react-intl";
|
||||||
|
|
||||||
|
import { SaleValueInputChangeType } from "./types";
|
||||||
|
|
||||||
|
interface SaleValueTextFieldProps {
|
||||||
|
dataType: keyof typeof SaleType;
|
||||||
|
helperText: string;
|
||||||
|
disabled: boolean;
|
||||||
|
listing: ChannelSaleData;
|
||||||
|
onChange: SaleValueInputChangeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SaleValueTextField: React.FC<SaleValueTextFieldProps> = ({
|
||||||
|
dataType,
|
||||||
|
helperText,
|
||||||
|
disabled,
|
||||||
|
listing,
|
||||||
|
onChange
|
||||||
|
}) => {
|
||||||
|
const intl = useIntl();
|
||||||
|
|
||||||
|
const [fixedValue, setFixedValue] = useState("");
|
||||||
|
const [percentageValue, setPercentageValue] = useState("");
|
||||||
|
|
||||||
|
const handleChange = (value: string) => {
|
||||||
|
onChange(listing.id, value);
|
||||||
|
};
|
||||||
|
|
||||||
|
const setCurrentValue = (value: string) => {
|
||||||
|
if (dataType === SaleType.PERCENTAGE) {
|
||||||
|
setPercentageValue(value);
|
||||||
|
} else {
|
||||||
|
setFixedValue(value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setCurrentValue(listing.discountValue);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
handleChange(getTextFieldValue());
|
||||||
|
}, [dataType]);
|
||||||
|
|
||||||
|
const getTextFieldValue = () =>
|
||||||
|
dataType === SaleType.PERCENTAGE ? percentageValue : fixedValue;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<TextField
|
||||||
|
disabled={disabled}
|
||||||
|
helperText={helperText || ""}
|
||||||
|
name="value"
|
||||||
|
onChange={e => {
|
||||||
|
handleChange(e.target.value);
|
||||||
|
setCurrentValue(e.target.value);
|
||||||
|
}}
|
||||||
|
label={intl.formatMessage({
|
||||||
|
defaultMessage: "Discount Value",
|
||||||
|
description: "sale discount"
|
||||||
|
})}
|
||||||
|
value={getTextFieldValue()}
|
||||||
|
type="number"
|
||||||
|
fullWidth
|
||||||
|
inputProps={{
|
||||||
|
min: 0
|
||||||
|
}}
|
||||||
|
InputProps={{
|
||||||
|
endAdornment: dataType === SaleType.FIXED ? listing.currency : "%"
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default SaleValueTextField;
|
4
src/discounts/components/SaleValue/types.ts
Normal file
4
src/discounts/components/SaleValue/types.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
export type SaleValueInputChangeType = (
|
||||||
|
channelId: string,
|
||||||
|
discountValue: string
|
||||||
|
) => void;
|
|
@ -129,7 +129,7 @@ export const getSaleChannelsVariables = (
|
||||||
formData: SaleDetailsPageFormData,
|
formData: SaleDetailsPageFormData,
|
||||||
prevChannels?: ChannelSaleData[]
|
prevChannels?: ChannelSaleData[]
|
||||||
) => {
|
) => {
|
||||||
const initialIds = prevChannels.map(channel => channel.id);
|
const initialIds = prevChannels?.map(channel => channel.id) || [];
|
||||||
const modifiedIds = formData.channelListings.map(channel => channel.id);
|
const modifiedIds = formData.channelListings.map(channel => channel.id);
|
||||||
|
|
||||||
const idsDiff = arrayDiff(initialIds, modifiedIds);
|
const idsDiff = arrayDiff(initialIds, modifiedIds);
|
||||||
|
|
Loading…
Reference in a new issue