Better input validation in Order Draft lines (#2003)
* Stop sending queries for empty & 0 values in TableLineForm * Provide default value for incorrect ones * Fix submit/change handlers * Remove async
This commit is contained in:
parent
7d3897f75f
commit
10e5b8d331
5 changed files with 33 additions and 30 deletions
|
@ -1,7 +1,7 @@
|
||||||
import { Card, CardContent } from "@material-ui/core";
|
import { Card, CardContent } from "@material-ui/core";
|
||||||
import { Button } from "@saleor/components/Button";
|
import { Button } from "@saleor/components/Button";
|
||||||
import CardTitle from "@saleor/components/CardTitle";
|
import CardTitle from "@saleor/components/CardTitle";
|
||||||
import { OrderDetailsFragment } from "@saleor/graphql";
|
import { OrderDetailsFragment, OrderLineInput } from "@saleor/graphql";
|
||||||
import {
|
import {
|
||||||
OrderDiscountContext,
|
OrderDiscountContext,
|
||||||
OrderDiscountContextConsumerProps,
|
OrderDiscountContextConsumerProps,
|
||||||
|
@ -10,18 +10,13 @@ import React from "react";
|
||||||
import { FormattedMessage, useIntl } from "react-intl";
|
import { FormattedMessage, useIntl } from "react-intl";
|
||||||
|
|
||||||
import { maybe } from "../../../misc";
|
import { maybe } from "../../../misc";
|
||||||
import OrderDraftDetailsProducts, {
|
import OrderDraftDetailsProducts from "../OrderDraftDetailsProducts";
|
||||||
FormData as OrderDraftDetailsProductsFormData,
|
|
||||||
} from "../OrderDraftDetailsProducts";
|
|
||||||
import OrderDraftDetailsSummary from "../OrderDraftDetailsSummary";
|
import OrderDraftDetailsSummary from "../OrderDraftDetailsSummary";
|
||||||
|
|
||||||
interface OrderDraftDetailsProps {
|
interface OrderDraftDetailsProps {
|
||||||
order: OrderDetailsFragment;
|
order: OrderDetailsFragment;
|
||||||
onOrderLineAdd: () => void;
|
onOrderLineAdd: () => void;
|
||||||
onOrderLineChange: (
|
onOrderLineChange: (id: string, data: OrderLineInput) => void;
|
||||||
id: string,
|
|
||||||
data: OrderDraftDetailsProductsFormData,
|
|
||||||
) => void;
|
|
||||||
onOrderLineRemove: (id: string) => void;
|
onOrderLineRemove: (id: string) => void;
|
||||||
onShippingMethodEdit: () => void;
|
onShippingMethodEdit: () => void;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ import Link from "@saleor/components/Link";
|
||||||
import Money from "@saleor/components/Money";
|
import Money from "@saleor/components/Money";
|
||||||
import TableCellAvatar from "@saleor/components/TableCellAvatar";
|
import TableCellAvatar from "@saleor/components/TableCellAvatar";
|
||||||
import { AVATAR_MARGIN } from "@saleor/components/TableCellAvatar/Avatar";
|
import { AVATAR_MARGIN } from "@saleor/components/TableCellAvatar/Avatar";
|
||||||
import { OrderLineFragment } from "@saleor/graphql";
|
import { OrderLineFragment, OrderLineInput } from "@saleor/graphql";
|
||||||
import { DeleteIcon, IconButton, makeStyles } from "@saleor/macaw-ui";
|
import { DeleteIcon, IconButton, makeStyles } from "@saleor/macaw-ui";
|
||||||
import { OrderLineDiscountContextConsumerProps } from "@saleor/products/components/OrderDiscountProviders/OrderLineDiscountProvider";
|
import { OrderLineDiscountContextConsumerProps } from "@saleor/products/components/OrderDiscountProviders/OrderLineDiscountProvider";
|
||||||
import React, { useRef } from "react";
|
import React, { useRef } from "react";
|
||||||
|
@ -11,7 +11,7 @@ import React, { useRef } from "react";
|
||||||
import { maybe } from "../../../misc";
|
import { maybe } from "../../../misc";
|
||||||
import OrderDiscountCommonModal from "../OrderDiscountCommonModal";
|
import OrderDiscountCommonModal from "../OrderDiscountCommonModal";
|
||||||
import { ORDER_LINE_DISCOUNT } from "../OrderDiscountCommonModal/types";
|
import { ORDER_LINE_DISCOUNT } from "../OrderDiscountCommonModal/types";
|
||||||
import TableLineForm, { FormData } from "./TableLineForm";
|
import TableLineForm from "./TableLineForm";
|
||||||
|
|
||||||
const useStyles = makeStyles(
|
const useStyles = makeStyles(
|
||||||
theme => ({
|
theme => ({
|
||||||
|
@ -52,7 +52,7 @@ const useStyles = makeStyles(
|
||||||
|
|
||||||
interface TableLineProps extends OrderLineDiscountContextConsumerProps {
|
interface TableLineProps extends OrderLineDiscountContextConsumerProps {
|
||||||
line: OrderLineFragment;
|
line: OrderLineFragment;
|
||||||
onOrderLineChange: (id: string, data: FormData) => void;
|
onOrderLineChange: (id: string, data: OrderLineInput) => void;
|
||||||
onOrderLineRemove: (id: string) => void;
|
onOrderLineRemove: (id: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { TextField } from "@material-ui/core";
|
import { TextField } from "@material-ui/core";
|
||||||
import DebounceForm from "@saleor/components/DebounceForm";
|
import DebounceForm from "@saleor/components/DebounceForm";
|
||||||
import Form from "@saleor/components/Form";
|
import Form from "@saleor/components/Form";
|
||||||
import { OrderLineFragment } from "@saleor/graphql";
|
import { OrderLineFragment, OrderLineInput } from "@saleor/graphql";
|
||||||
import { makeStyles } from "@saleor/macaw-ui";
|
import { makeStyles } from "@saleor/macaw-ui";
|
||||||
import createNonNegativeValueChangeHandler from "@saleor/utils/handlers/nonNegativeValueChangeHandler";
|
import createNonNegativeValueChangeHandler from "@saleor/utils/handlers/nonNegativeValueChangeHandler";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
@ -18,14 +18,9 @@ const useStyles = makeStyles(
|
||||||
}),
|
}),
|
||||||
{ name: "TableLineForm" },
|
{ name: "TableLineForm" },
|
||||||
);
|
);
|
||||||
|
|
||||||
export interface FormData {
|
|
||||||
quantity: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface TableLineFormProps {
|
interface TableLineFormProps {
|
||||||
line: OrderLineFragment;
|
line: OrderLineFragment;
|
||||||
onOrderLineChange: (id: string, data: FormData) => void;
|
onOrderLineChange: (id: string, data: OrderLineInput) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const TableLineForm: React.FC<TableLineFormProps> = ({
|
const TableLineForm: React.FC<TableLineFormProps> = ({
|
||||||
|
@ -35,9 +30,14 @@ const TableLineForm: React.FC<TableLineFormProps> = ({
|
||||||
const classes = useStyles({});
|
const classes = useStyles({});
|
||||||
const { id, quantity } = line;
|
const { id, quantity } = line;
|
||||||
|
|
||||||
|
const handleSubmit = (id: string, data: OrderLineInput) => {
|
||||||
|
const quantity = data?.quantity >= 1 ? Math.floor(data.quantity) : 1;
|
||||||
|
onOrderLineChange(id, { quantity });
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form initial={{ quantity }} onSubmit={data => onOrderLineChange(id, data)}>
|
<Form initial={{ quantity }} onSubmit={data => handleSubmit(id, data)}>
|
||||||
{({ change, data, submit }) => {
|
{({ change, data, submit, set }) => {
|
||||||
const handleQuantityChange = createNonNegativeValueChangeHandler(
|
const handleQuantityChange = createNonNegativeValueChangeHandler(
|
||||||
change,
|
change,
|
||||||
);
|
);
|
||||||
|
@ -56,10 +56,13 @@ const TableLineForm: React.FC<TableLineFormProps> = ({
|
||||||
type="number"
|
type="number"
|
||||||
value={data.quantity}
|
value={data.quantity}
|
||||||
onChange={debounce}
|
onChange={debounce}
|
||||||
onBlur={submit}
|
onBlur={() => {
|
||||||
inputProps={{
|
if (data.quantity < 1) {
|
||||||
min: 1,
|
set({ quantity: 1 });
|
||||||
|
}
|
||||||
|
submit();
|
||||||
}}
|
}}
|
||||||
|
inputProps={{ min: 1 }}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</DebounceForm>
|
</DebounceForm>
|
||||||
|
|
|
@ -8,7 +8,11 @@ import Grid from "@saleor/components/Grid";
|
||||||
import PageHeader from "@saleor/components/PageHeader";
|
import PageHeader from "@saleor/components/PageHeader";
|
||||||
import Savebar from "@saleor/components/Savebar";
|
import Savebar from "@saleor/components/Savebar";
|
||||||
import Skeleton from "@saleor/components/Skeleton";
|
import Skeleton from "@saleor/components/Skeleton";
|
||||||
import { OrderDetailsFragment, SearchCustomersQuery } from "@saleor/graphql";
|
import {
|
||||||
|
OrderDetailsFragment,
|
||||||
|
OrderLineInput,
|
||||||
|
SearchCustomersQuery,
|
||||||
|
} from "@saleor/graphql";
|
||||||
import { SubmitPromise } from "@saleor/hooks/useForm";
|
import { SubmitPromise } from "@saleor/hooks/useForm";
|
||||||
import useNavigator from "@saleor/hooks/useNavigator";
|
import useNavigator from "@saleor/hooks/useNavigator";
|
||||||
import { sectionNames } from "@saleor/intl";
|
import { sectionNames } from "@saleor/intl";
|
||||||
|
@ -21,7 +25,6 @@ import { useIntl } from "react-intl";
|
||||||
|
|
||||||
import OrderCustomer, { CustomerEditData } from "../OrderCustomer";
|
import OrderCustomer, { CustomerEditData } from "../OrderCustomer";
|
||||||
import OrderDraftDetails from "../OrderDraftDetails/OrderDraftDetails";
|
import OrderDraftDetails from "../OrderDraftDetails/OrderDraftDetails";
|
||||||
import { FormData as OrderDraftDetailsProductsFormData } from "../OrderDraftDetailsProducts";
|
|
||||||
import OrderHistory, { FormData as HistoryFormData } from "../OrderHistory";
|
import OrderHistory, { FormData as HistoryFormData } from "../OrderHistory";
|
||||||
|
|
||||||
const useStyles = makeStyles(
|
const useStyles = makeStyles(
|
||||||
|
@ -50,10 +53,7 @@ export interface OrderDraftPageProps extends FetchMoreProps {
|
||||||
onDraftRemove: () => void;
|
onDraftRemove: () => void;
|
||||||
onNoteAdd: (data: HistoryFormData) => SubmitPromise<any[]>;
|
onNoteAdd: (data: HistoryFormData) => SubmitPromise<any[]>;
|
||||||
onOrderLineAdd: () => void;
|
onOrderLineAdd: () => void;
|
||||||
onOrderLineChange: (
|
onOrderLineChange: (id: string, data: OrderLineInput) => void;
|
||||||
id: string,
|
|
||||||
data: OrderDraftDetailsProductsFormData,
|
|
||||||
) => void;
|
|
||||||
onOrderLineRemove: (id: string) => void;
|
onOrderLineRemove: (id: string) => void;
|
||||||
onProductClick: (id: string) => void;
|
onProductClick: (id: string) => void;
|
||||||
onShippingAddressEdit: () => void;
|
onShippingAddressEdit: () => void;
|
||||||
|
|
|
@ -6,6 +6,8 @@ import {
|
||||||
OrderDraftCancelMutationVariables,
|
OrderDraftCancelMutationVariables,
|
||||||
OrderDraftUpdateMutation,
|
OrderDraftUpdateMutation,
|
||||||
OrderDraftUpdateMutationVariables,
|
OrderDraftUpdateMutationVariables,
|
||||||
|
OrderLineUpdateMutation,
|
||||||
|
OrderLineUpdateMutationVariables,
|
||||||
StockAvailability,
|
StockAvailability,
|
||||||
useCustomerAddressesQuery,
|
useCustomerAddressesQuery,
|
||||||
} from "@saleor/graphql";
|
} from "@saleor/graphql";
|
||||||
|
@ -50,7 +52,10 @@ interface OrderDraftDetailsProps {
|
||||||
loading: any;
|
loading: any;
|
||||||
data: OrderDetailsQuery;
|
data: OrderDetailsQuery;
|
||||||
orderAddNote: any;
|
orderAddNote: any;
|
||||||
orderLineUpdate: any;
|
orderLineUpdate: PartialMutationProviderOutput<
|
||||||
|
OrderLineUpdateMutation,
|
||||||
|
OrderLineUpdateMutationVariables
|
||||||
|
>;
|
||||||
orderLineDelete: any;
|
orderLineDelete: any;
|
||||||
orderShippingMethodUpdate: any;
|
orderShippingMethodUpdate: any;
|
||||||
orderLinesAdd: any;
|
orderLinesAdd: any;
|
||||||
|
|
Loading…
Reference in a new issue