From b5d92f3c446693a9932cd8422ed9a11619954bd6 Mon Sep 17 00:00:00 2001 From: Magdalena Markusik Date: Thu, 29 Oct 2020 12:59:48 +0100 Subject: [PATCH] Refactor --- .../OrderAddressEditDialog.tsx | 4 +- .../views/OrderDetails/OrderAddressFields.tsx | 90 +++++++++++++++++++ src/orders/views/OrderDetails/index.tsx | 82 ++--------------- 3 files changed, 101 insertions(+), 75 deletions(-) create mode 100644 src/orders/views/OrderDetails/OrderAddressFields.tsx diff --git a/src/orders/components/OrderAddressEditDialog/OrderAddressEditDialog.tsx b/src/orders/components/OrderAddressEditDialog/OrderAddressEditDialog.tsx index 1df64b36b..13e6f4487 100644 --- a/src/orders/components/OrderAddressEditDialog/OrderAddressEditDialog.tsx +++ b/src/orders/components/OrderAddressEditDialog/OrderAddressEditDialog.tsx @@ -49,9 +49,9 @@ const OrderAddressEditDialog: React.FC = props => { address, confirmButtonState, open, - errors, + errors = [], variant, - countries, + countries = [], onClose, onConfirm } = props; diff --git a/src/orders/views/OrderDetails/OrderAddressFields.tsx b/src/orders/views/OrderDetails/OrderAddressFields.tsx new file mode 100644 index 000000000..7d823d51d --- /dev/null +++ b/src/orders/views/OrderDetails/OrderAddressFields.tsx @@ -0,0 +1,90 @@ +import { transformAddressToForm } from "@saleor/misc"; +import OrderAddressEditDialog from "@saleor/orders/components/OrderAddressEditDialog"; +import { OrderDetails } from "@saleor/orders/types/OrderDetails"; +import { + OrderDraftUpdate, + OrderDraftUpdateVariables +} from "@saleor/orders/types/OrderDraftUpdate"; +import { + OrderUpdate, + OrderUpdateVariables +} from "@saleor/orders/types/OrderUpdate"; +import { PartialMutationProviderOutput } from "@saleor/types"; +import { AddressInput } from "@saleor/types/globalTypes"; +import React from "react"; + +enum FieldType { + shipping = "shippingAddress", + billing = "billingAddress" +} + +interface Props { + action: string; + id: string; + isDraft: boolean; + data: OrderDetails; + onClose: () => void; + orderUpdate: PartialMutationProviderOutput; + orderDraftUpdate: PartialMutationProviderOutput< + OrderDraftUpdate, + OrderDraftUpdateVariables + >; +} + +const OrderAddressFields = ({ + action, + isDraft, + id, + onClose, + orderUpdate, + orderDraftUpdate, + data +}: Props) => { + const order = data?.order; + + const handleConfirm = (type: FieldType) => (value: AddressInput) => { + const updateMutation = isDraft ? orderDraftUpdate : orderUpdate; + + updateMutation.mutate({ + id, + input: { + [type]: value + } + }); + }; + + const addressFieldCommonProps = { + confirmButtonState: isDraft + ? orderDraftUpdate.opts.status + : orderUpdate.opts.status, + countries: data?.shop?.countries.map(country => ({ + code: country.code, + label: country.country + })), + errors: isDraft + ? orderDraftUpdate.opts.data?.draftOrderUpdate.errors + : orderUpdate.opts.data?.orderUpdate.errors, + onClose + }; + + return ( + <> + + + + ); +}; + +export default OrderAddressFields; diff --git a/src/orders/views/OrderDetails/index.tsx b/src/orders/views/OrderDetails/index.tsx index 45e5473d2..89482df93 100644 --- a/src/orders/views/OrderDetails/index.tsx +++ b/src/orders/views/OrderDetails/index.tsx @@ -23,19 +23,13 @@ import React from "react"; import { useIntl } from "react-intl"; import { customerUrl } from "../../../customers/urls"; -import { - getMutationState, - getStringOrPlaceholder, - maybe, - transformAddressToForm -} from "../../../misc"; +import { getMutationState, getStringOrPlaceholder, maybe } from "../../../misc"; import { productUrl } from "../../../products/urls"; import { FulfillmentStatus, JobStatusEnum, OrderStatus } from "../../../types/globalTypes"; -import OrderAddressEditDialog from "../../components/OrderAddressEditDialog"; import OrderCancelDialog from "../../components/OrderCancelDialog"; import OrderDetailsPage from "../../components/OrderDetailsPage"; import OrderDraftCancelDialog from "../../components/OrderDraftCancelDialog/OrderDraftCancelDialog"; @@ -57,6 +51,7 @@ import { OrderUrlDialog, OrderUrlQueryParams } from "../../urls"; +import OrderAddressFields from "./OrderAddressFields"; import { OrderDetailsMessages } from "./OrderDetailsMessages"; interface OrderDetailsProps { @@ -579,73 +574,14 @@ export const OrderDetails: React.FC = ({ id, params }) => { /> )} - ({ - code: country.code, - label: country.country - })) || [] - } - errors={ - (order?.status === OrderStatus.DRAFT - ? orderDraftUpdate.opts.data?.draftOrderUpdate.errors - : orderUpdate.opts.data?.orderUpdate.errors) || [] - } - open={params.action === "edit-shipping-address"} - variant="shipping" + { - const updateMutation = - order?.status === OrderStatus.DRAFT - ? orderDraftUpdate - : orderUpdate; - updateMutation.mutate({ - id, - input: { - shippingAddress - } - }); - }} - /> - ({ - code: country.code, - label: country.country - })) || [] - } - errors={ - (order?.status === OrderStatus.DRAFT - ? orderDraftUpdate.opts.data?.draftOrderUpdate.errors - : orderUpdate.opts.data?.orderUpdate.errors) || [] - } - open={params.action === "edit-billing-address"} - variant="billing" - onClose={closeModal} - onConfirm={billingAddress => { - const updateMutation = - order?.status === OrderStatus.DRAFT - ? orderDraftUpdate - : orderUpdate; - updateMutation.mutate({ - id, - input: { - billingAddress - } - }); - }} + action={params.action} /> )}