2021-03-23 17:13:39 +00:00
|
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
|
|
|
import { DEFAULT_INITIAL_SEARCH_DATA } from "@saleor/config";
|
2022-03-09 08:56:55 +00:00
|
|
|
import {
|
|
|
|
OrderDetailsQuery,
|
2022-04-26 13:32:09 +00:00
|
|
|
OrderDraftCancelMutation,
|
|
|
|
OrderDraftCancelMutationVariables,
|
2022-08-10 09:11:32 +00:00
|
|
|
OrderDraftFinalizeMutation,
|
|
|
|
OrderDraftFinalizeMutationVariables,
|
2022-03-09 08:56:55 +00:00
|
|
|
OrderDraftUpdateMutation,
|
|
|
|
OrderDraftUpdateMutationVariables,
|
2022-07-07 15:31:26 +00:00
|
|
|
OrderLineUpdateMutation,
|
|
|
|
OrderLineUpdateMutationVariables,
|
2022-03-30 07:01:14 +00:00
|
|
|
StockAvailability,
|
2022-08-10 09:11:32 +00:00
|
|
|
useChannelUsabilityDataQuery,
|
2022-06-21 09:36:55 +00:00
|
|
|
useCustomerAddressesQuery,
|
2022-03-09 08:56:55 +00:00
|
|
|
} from "@saleor/graphql";
|
2021-03-23 17:13:39 +00:00
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
2021-05-06 11:38:15 +00:00
|
|
|
import { CustomerEditData } from "@saleor/orders/components/OrderCustomer";
|
2022-02-02 11:22:39 +00:00
|
|
|
import { OrderCustomerAddressesEditDialogOutput } from "@saleor/orders/components/OrderCustomerAddressesEditDialog/types";
|
2021-05-06 11:38:15 +00:00
|
|
|
import {
|
|
|
|
CustomerChangeActionEnum,
|
2022-06-21 09:36:55 +00:00
|
|
|
OrderCustomerChangeData,
|
2021-05-06 11:38:15 +00:00
|
|
|
} from "@saleor/orders/components/OrderCustomerChangeDialog/form";
|
|
|
|
import OrderCustomerChangeDialog from "@saleor/orders/components/OrderCustomerChangeDialog/OrderCustomerChangeDialog";
|
2021-12-23 15:00:37 +00:00
|
|
|
import { getVariantSearchAddress } from "@saleor/orders/utils/data";
|
2021-03-23 17:13:39 +00:00
|
|
|
import { OrderDiscountProvider } from "@saleor/products/components/OrderDiscountProviders/OrderDiscountProvider";
|
|
|
|
import { OrderLineDiscountProvider } from "@saleor/products/components/OrderDiscountProviders/OrderLineDiscountProvider";
|
|
|
|
import useCustomerSearch from "@saleor/searches/useCustomerSearch";
|
2022-03-09 08:56:55 +00:00
|
|
|
import { useOrderVariantSearch } from "@saleor/searches/useOrderVariantSearch";
|
2022-02-02 11:22:39 +00:00
|
|
|
import { PartialMutationProviderOutput } from "@saleor/types";
|
2021-05-10 15:25:54 +00:00
|
|
|
import { mapEdgesToItems } from "@saleor/utils/maps";
|
2021-03-23 17:13:39 +00:00
|
|
|
import React from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
|
|
|
import { customerUrl } from "../../../../customers/urls";
|
2022-02-01 09:58:06 +00:00
|
|
|
import {
|
|
|
|
extractMutationErrors,
|
2022-06-21 09:36:55 +00:00
|
|
|
getStringOrPlaceholder,
|
2022-02-01 09:58:06 +00:00
|
|
|
} from "../../../../misc";
|
2021-03-23 17:13:39 +00:00
|
|
|
import { productUrl } from "../../../../products/urls";
|
2022-02-02 11:22:39 +00:00
|
|
|
import OrderAddressFields from "../../../components/OrderAddressFields/OrderAddressFields";
|
2021-03-23 17:13:39 +00:00
|
|
|
import OrderDraftCancelDialog from "../../../components/OrderDraftCancelDialog/OrderDraftCancelDialog";
|
|
|
|
import OrderDraftPage from "../../../components/OrderDraftPage";
|
|
|
|
import OrderProductAddDialog from "../../../components/OrderProductAddDialog";
|
|
|
|
import OrderShippingMethodEditDialog from "../../../components/OrderShippingMethodEditDialog";
|
2022-03-09 08:56:55 +00:00
|
|
|
import {
|
|
|
|
orderDraftListUrl,
|
|
|
|
OrderUrlDialog,
|
2022-06-21 09:36:55 +00:00
|
|
|
OrderUrlQueryParams,
|
2022-03-09 08:56:55 +00:00
|
|
|
} from "../../../urls";
|
2021-03-23 17:13:39 +00:00
|
|
|
|
|
|
|
interface OrderDraftDetailsProps {
|
|
|
|
id: string;
|
|
|
|
params: OrderUrlQueryParams;
|
|
|
|
loading: any;
|
2022-03-09 08:56:55 +00:00
|
|
|
data: OrderDetailsQuery;
|
2021-03-23 17:13:39 +00:00
|
|
|
orderAddNote: any;
|
2022-07-07 15:31:26 +00:00
|
|
|
orderLineUpdate: PartialMutationProviderOutput<
|
|
|
|
OrderLineUpdateMutation,
|
|
|
|
OrderLineUpdateMutationVariables
|
|
|
|
>;
|
2021-03-23 17:13:39 +00:00
|
|
|
orderLineDelete: any;
|
|
|
|
orderShippingMethodUpdate: any;
|
|
|
|
orderLinesAdd: any;
|
2022-02-02 11:22:39 +00:00
|
|
|
orderDraftUpdate: PartialMutationProviderOutput<
|
2022-03-09 08:56:55 +00:00
|
|
|
OrderDraftUpdateMutation,
|
|
|
|
OrderDraftUpdateMutationVariables
|
2022-02-02 11:22:39 +00:00
|
|
|
>;
|
2022-04-26 13:32:09 +00:00
|
|
|
orderDraftCancel: PartialMutationProviderOutput<
|
|
|
|
OrderDraftCancelMutation,
|
|
|
|
OrderDraftCancelMutationVariables
|
|
|
|
>;
|
2022-08-10 09:11:32 +00:00
|
|
|
orderDraftFinalize: PartialMutationProviderOutput<
|
|
|
|
OrderDraftFinalizeMutation,
|
|
|
|
OrderDraftFinalizeMutationVariables
|
|
|
|
>;
|
2021-05-06 11:38:15 +00:00
|
|
|
openModal: (action: OrderUrlDialog, newParams?: OrderUrlQueryParams) => void;
|
2021-03-23 17:13:39 +00:00
|
|
|
closeModal: any;
|
|
|
|
}
|
|
|
|
|
2022-02-02 11:22:39 +00:00
|
|
|
export const isAnyAddressEditModalOpen = (uri: string | undefined): boolean =>
|
|
|
|
[
|
|
|
|
"edit-customer-addresses",
|
|
|
|
"edit-shipping-address",
|
2022-06-21 09:36:55 +00:00
|
|
|
"edit-billing-address",
|
2022-02-02 11:22:39 +00:00
|
|
|
].includes(uri);
|
|
|
|
|
2021-03-23 17:13:39 +00:00
|
|
|
export const OrderDraftDetails: React.FC<OrderDraftDetailsProps> = ({
|
|
|
|
id,
|
|
|
|
params,
|
|
|
|
loading,
|
|
|
|
data,
|
|
|
|
orderAddNote,
|
|
|
|
orderLineUpdate,
|
|
|
|
orderLineDelete,
|
|
|
|
orderShippingMethodUpdate,
|
|
|
|
orderLinesAdd,
|
|
|
|
orderDraftUpdate,
|
|
|
|
orderDraftCancel,
|
|
|
|
orderDraftFinalize,
|
|
|
|
openModal,
|
2022-06-21 09:36:55 +00:00
|
|
|
closeModal,
|
2021-03-23 17:13:39 +00:00
|
|
|
}) => {
|
|
|
|
const order = data.order;
|
|
|
|
const navigate = useNavigator();
|
|
|
|
|
2022-08-10 09:11:32 +00:00
|
|
|
const { data: channelUsabilityData } = useChannelUsabilityDataQuery({
|
|
|
|
variables: {
|
|
|
|
channel: order.channel.slug,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-03-23 17:13:39 +00:00
|
|
|
const {
|
|
|
|
loadMore,
|
|
|
|
search: variantSearch,
|
2022-06-21 09:36:55 +00:00
|
|
|
result: variantSearchOpts,
|
2021-03-23 17:13:39 +00:00
|
|
|
} = useOrderVariantSearch({
|
2021-12-23 15:00:37 +00:00
|
|
|
variables: {
|
|
|
|
...DEFAULT_INITIAL_SEARCH_DATA,
|
|
|
|
channel: order.channel.slug,
|
2022-03-30 07:01:14 +00:00
|
|
|
address: getVariantSearchAddress(order),
|
|
|
|
isPublished: true,
|
2022-06-21 09:36:55 +00:00
|
|
|
stockAvailability: StockAvailability.IN_STOCK,
|
|
|
|
},
|
2021-03-23 17:13:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const {
|
|
|
|
loadMore: loadMoreCustomers,
|
|
|
|
search: searchUsers,
|
2022-06-21 09:36:55 +00:00
|
|
|
result: users,
|
2021-03-23 17:13:39 +00:00
|
|
|
} = useCustomerSearch({
|
2022-06-21 09:36:55 +00:00
|
|
|
variables: DEFAULT_INITIAL_SEARCH_DATA,
|
2021-03-23 17:13:39 +00:00
|
|
|
});
|
|
|
|
|
2021-05-06 11:38:15 +00:00
|
|
|
const {
|
|
|
|
data: customerAddresses,
|
2022-06-21 09:36:55 +00:00
|
|
|
loading: customerAddressesLoading,
|
2021-05-06 11:38:15 +00:00
|
|
|
} = useCustomerAddressesQuery({
|
|
|
|
variables: {
|
2022-06-21 09:36:55 +00:00
|
|
|
id: order?.user?.id,
|
2021-05-06 11:38:15 +00:00
|
|
|
},
|
2022-06-21 09:36:55 +00:00
|
|
|
skip: !order?.user?.id || !isAnyAddressEditModalOpen(params.action),
|
2021-05-06 11:38:15 +00:00
|
|
|
});
|
|
|
|
|
2021-03-23 17:13:39 +00:00
|
|
|
const intl = useIntl();
|
|
|
|
|
2021-05-06 11:38:15 +00:00
|
|
|
const handleCustomerChange = async ({
|
|
|
|
user,
|
|
|
|
userEmail,
|
|
|
|
prevUser,
|
2022-06-21 09:36:55 +00:00
|
|
|
prevUserEmail,
|
2021-05-06 11:38:15 +00:00
|
|
|
}: CustomerEditData) => {
|
|
|
|
const sameUser = user && user === prevUser;
|
|
|
|
const sameUserEmail = userEmail && userEmail === prevUserEmail;
|
|
|
|
if (sameUser || sameUserEmail) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await orderDraftUpdate.mutate({
|
|
|
|
id,
|
|
|
|
input: {
|
|
|
|
user,
|
2022-06-21 09:36:55 +00:00
|
|
|
userEmail,
|
|
|
|
},
|
2021-05-06 11:38:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (result?.data?.draftOrderUpdate?.errors?.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const modalUri = prevUser ? "customer-change" : "edit-customer-addresses";
|
|
|
|
openModal(modalUri);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleCustomerChangeAction = (data: OrderCustomerChangeData) => {
|
|
|
|
if (data.changeActionOption === CustomerChangeActionEnum.CHANGE_ADDRESS) {
|
|
|
|
openModal("edit-customer-addresses");
|
|
|
|
} else {
|
|
|
|
closeModal();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-02 11:22:39 +00:00
|
|
|
const handleCustomerChangeAddresses = async (
|
2022-06-21 09:36:55 +00:00
|
|
|
data: Partial<OrderCustomerAddressesEditDialogOutput>,
|
2022-02-02 11:22:39 +00:00
|
|
|
): Promise<any> =>
|
|
|
|
orderDraftUpdate.mutate({
|
2021-05-06 11:38:15 +00:00
|
|
|
id,
|
2022-06-21 09:36:55 +00:00
|
|
|
input: data,
|
2021-05-06 11:38:15 +00:00
|
|
|
});
|
|
|
|
|
2022-04-26 13:32:09 +00:00
|
|
|
const handleOrderDraftCancel = async () => {
|
|
|
|
const errors = await extractMutationErrors(orderDraftCancel.mutate({ id }));
|
|
|
|
if (!errors.length) {
|
|
|
|
navigate(orderDraftListUrl());
|
|
|
|
}
|
|
|
|
return errors;
|
|
|
|
};
|
|
|
|
|
2022-08-10 09:11:32 +00:00
|
|
|
const errors = orderDraftFinalize.opts.data?.draftOrderComplete.errors || [];
|
|
|
|
|
2021-03-23 17:13:39 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<WindowTitle
|
|
|
|
title={intl.formatMessage(
|
|
|
|
{
|
2022-05-05 07:54:28 +00:00
|
|
|
id: "TLNf6K",
|
2021-03-23 17:13:39 +00:00
|
|
|
defaultMessage: "Draft Order #{orderNumber}",
|
2022-06-21 09:36:55 +00:00
|
|
|
description: "window title",
|
2021-03-23 17:13:39 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-21 09:36:55 +00:00
|
|
|
orderNumber: getStringOrPlaceholder(data?.order?.number),
|
|
|
|
},
|
2021-03-23 17:13:39 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<OrderDiscountProvider order={order}>
|
|
|
|
<OrderLineDiscountProvider order={order}>
|
|
|
|
<OrderDraftPage
|
|
|
|
disabled={loading}
|
2022-08-10 09:11:32 +00:00
|
|
|
errors={errors}
|
2021-03-23 17:13:39 +00:00
|
|
|
onNoteAdd={variables =>
|
2022-02-01 09:58:06 +00:00
|
|
|
extractMutationErrors(
|
|
|
|
orderAddNote.mutate({
|
|
|
|
input: variables,
|
2022-06-21 09:36:55 +00:00
|
|
|
order: id,
|
|
|
|
}),
|
2022-02-01 09:58:06 +00:00
|
|
|
)
|
2021-03-23 17:13:39 +00:00
|
|
|
}
|
2021-05-12 09:11:17 +00:00
|
|
|
users={mapEdgesToItems(users?.data?.search)}
|
2021-04-02 04:28:28 +00:00
|
|
|
hasMore={users?.data?.search?.pageInfo?.hasNextPage || false}
|
2021-03-23 17:13:39 +00:00
|
|
|
onFetchMore={loadMoreCustomers}
|
|
|
|
fetchUsers={searchUsers}
|
|
|
|
loading={users.loading}
|
|
|
|
usersLoading={users.loading}
|
2021-05-06 11:38:15 +00:00
|
|
|
onCustomerEdit={handleCustomerChange}
|
2021-03-23 17:13:39 +00:00
|
|
|
onDraftFinalize={() => orderDraftFinalize.mutate({ id })}
|
|
|
|
onDraftRemove={() => openModal("cancel")}
|
|
|
|
onOrderLineAdd={() => openModal("add-order-line")}
|
|
|
|
order={order}
|
2022-08-10 09:11:32 +00:00
|
|
|
channelUsabilityData={channelUsabilityData}
|
2021-03-23 17:13:39 +00:00
|
|
|
onProductClick={id => () =>
|
|
|
|
navigate(productUrl(encodeURIComponent(id)))}
|
|
|
|
onBillingAddressEdit={() => openModal("edit-billing-address")}
|
|
|
|
onShippingAddressEdit={() => openModal("edit-shipping-address")}
|
|
|
|
onShippingMethodEdit={() => openModal("edit-shipping")}
|
|
|
|
onOrderLineRemove={id => orderLineDelete.mutate({ id })}
|
|
|
|
onOrderLineChange={(id, data) =>
|
|
|
|
orderLineUpdate.mutate({
|
|
|
|
id,
|
2022-06-21 09:36:55 +00:00
|
|
|
input: data,
|
2021-03-23 17:13:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
saveButtonBarState="default"
|
|
|
|
onProfileView={() => navigate(customerUrl(order.user.id))}
|
|
|
|
/>
|
|
|
|
</OrderLineDiscountProvider>
|
|
|
|
</OrderDiscountProvider>
|
|
|
|
<OrderDraftCancelDialog
|
|
|
|
confirmButtonState={orderDraftCancel.opts.status}
|
|
|
|
errors={orderDraftCancel.opts.data?.draftOrderDelete.errors || []}
|
|
|
|
onClose={closeModal}
|
2022-04-26 13:32:09 +00:00
|
|
|
onConfirm={handleOrderDraftCancel}
|
2021-03-23 17:13:39 +00:00
|
|
|
open={params.action === "cancel"}
|
|
|
|
orderNumber={getStringOrPlaceholder(order?.number)}
|
|
|
|
/>
|
|
|
|
<OrderShippingMethodEditDialog
|
|
|
|
confirmButtonState={orderShippingMethodUpdate.opts.status}
|
|
|
|
errors={
|
|
|
|
orderShippingMethodUpdate.opts.data?.orderUpdateShipping.errors || []
|
|
|
|
}
|
|
|
|
open={params.action === "edit-shipping"}
|
|
|
|
shippingMethod={order?.shippingMethod?.id}
|
2022-01-24 13:50:17 +00:00
|
|
|
shippingMethods={order?.shippingMethods}
|
2021-03-23 17:13:39 +00:00
|
|
|
onClose={closeModal}
|
|
|
|
onSubmit={variables =>
|
|
|
|
orderShippingMethodUpdate.mutate({
|
|
|
|
id,
|
|
|
|
input: {
|
2022-06-21 09:36:55 +00:00
|
|
|
shippingMethod: variables.shippingMethod,
|
|
|
|
},
|
2021-03-23 17:13:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<OrderProductAddDialog
|
|
|
|
confirmButtonState={orderLinesAdd.opts.status}
|
|
|
|
errors={orderLinesAdd.opts.data?.orderLinesCreate.errors || []}
|
|
|
|
loading={variantSearchOpts.loading}
|
|
|
|
open={params.action === "add-order-line"}
|
|
|
|
hasMore={variantSearchOpts.data?.search.pageInfo.hasNextPage}
|
2021-05-10 15:25:54 +00:00
|
|
|
products={mapEdgesToItems(variantSearchOpts?.data?.search)}
|
2021-03-23 17:13:39 +00:00
|
|
|
selectedChannelId={order?.channel?.id}
|
|
|
|
onClose={closeModal}
|
|
|
|
onFetch={variantSearch}
|
|
|
|
onFetchMore={loadMore}
|
|
|
|
onSubmit={variants =>
|
2022-02-01 09:58:06 +00:00
|
|
|
extractMutationErrors(
|
|
|
|
orderLinesAdd.mutate({
|
|
|
|
id,
|
|
|
|
input: variants.map(variant => ({
|
|
|
|
quantity: 1,
|
2022-06-21 09:36:55 +00:00
|
|
|
variantId: variant.id,
|
|
|
|
})),
|
|
|
|
}),
|
2022-02-01 09:58:06 +00:00
|
|
|
)
|
2021-03-23 17:13:39 +00:00
|
|
|
}
|
|
|
|
/>
|
2021-05-06 11:38:15 +00:00
|
|
|
<OrderCustomerChangeDialog
|
|
|
|
open={params.action === "customer-change"}
|
|
|
|
onClose={closeModal}
|
|
|
|
onConfirm={handleCustomerChangeAction}
|
|
|
|
/>
|
2022-02-02 11:22:39 +00:00
|
|
|
<OrderAddressFields
|
|
|
|
action={params?.action}
|
|
|
|
orderShippingAddress={order?.shippingAddress}
|
|
|
|
orderBillingAddress={order?.billingAddress}
|
|
|
|
customerAddressesLoading={customerAddressesLoading}
|
|
|
|
isDraft
|
2021-05-06 11:38:15 +00:00
|
|
|
countries={data?.shop?.countries}
|
2022-02-02 11:22:39 +00:00
|
|
|
customer={customerAddresses?.user}
|
2021-05-06 11:38:15 +00:00
|
|
|
onClose={closeModal}
|
2022-02-02 11:22:39 +00:00
|
|
|
onConfirm={handleCustomerChangeAddresses}
|
|
|
|
confirmButtonState={orderDraftUpdate.opts.status}
|
|
|
|
errors={orderDraftUpdate.opts.data?.draftOrderUpdate.errors}
|
2021-05-06 11:38:15 +00:00
|
|
|
/>
|
2021-03-23 17:13:39 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default OrderDraftDetails;
|