2020-10-29 11:59:48 +00:00
|
|
|
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<OrderUpdate, OrderUpdateVariables>;
|
|
|
|
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,
|
2021-05-06 11:38:15 +00:00
|
|
|
countries: data?.shop?.countries,
|
2020-10-29 11:59:48 +00:00
|
|
|
errors: isDraft
|
|
|
|
? orderDraftUpdate.opts.data?.draftOrderUpdate.errors
|
|
|
|
: orderUpdate.opts.data?.orderUpdate.errors,
|
|
|
|
onClose
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<OrderAddressEditDialog
|
|
|
|
{...addressFieldCommonProps}
|
|
|
|
address={transformAddressToForm(order?.shippingAddress)}
|
|
|
|
open={action === "edit-shipping-address"}
|
|
|
|
variant="shipping"
|
|
|
|
onConfirm={handleConfirm(FieldType.shipping)}
|
|
|
|
/>
|
|
|
|
<OrderAddressEditDialog
|
|
|
|
{...addressFieldCommonProps}
|
|
|
|
address={transformAddressToForm(order?.billingAddress)}
|
|
|
|
open={action === "edit-billing-address"}
|
|
|
|
variant="billing"
|
|
|
|
onConfirm={handleConfirm(FieldType.billing)}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default OrderAddressFields;
|