Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Extract order details mutations into a separate container

dominik-zeglen502891cd41a6
diff --git a/saleor/static/dashboard-next/orders/containers/OrderCancel.tsx b/saleor/static/dashboard-next/orders/containers/OrderCancel.tsx
new file mode 100644
index 0000000000..5a00aa9156
--- /dev/null
+++ b/saleor/static/dashboard-next/orders/containers/OrderCancel.tsx
@@ -0,0 +1,37 @@
+import * as React from "react";
+
+import {
+ OrderCancelMutation,
+ OrderCancelMutationVariables
+} from "../../gql-types";
+import { TypedOrderCancelMutation } from "../mutations";
+
+import {
+ PartialMutationProviderProps,
+ PartialMutationProviderRenderProps
+} from "../..";
+
+interface OrderCancelMutationProviderProps
+ extends PartialMutationProviderProps<OrderCancelMutation> {
+ children: PartialMutationProviderRenderProps<
+ OrderCancelMutation,
+ OrderCancelMutationVariables
+ >;
+}
+
+const OrderCancelMutationProvider: React.StatelessComponent<
+ OrderCancelMutationProviderProps
+> = ({ children, onError, onSuccess }) => (
+ <TypedOrderCancelMutation onCompleted={onSuccess} onError={onError}>
+ {(mutate, { data, error, loading }) =>
+ children({
+ data,
+ error,
+ loading,
+ mutate
+ })
+ }
+ </TypedOrderCancelMutation>
+);
+
+export default OrderCancelMutationProvider;
diff --git a/saleor/static/dashboard-next/orders/containers/OrderOperations.tsx b/saleor/static/dashboard-next/orders/containers/OrderOperations.tsx
index 54a52b36ba..a2492d63ee 100644
--- a/saleor/static/dashboard-next/orders/containers/OrderOperations.tsx
+++ b/saleor/static/dashboard-next/orders/containers/OrderOperations.tsx
@@ -6,21 +6,31 @@ import {
PartialMutationProviderOutput
} from "../..";
import {
+ OrderCancelMutation,
+ OrderCancelMutationVariables,
OrderCaptureMutation,
OrderCaptureMutationVariables,
OrderCreateFulfillmentMutation,
OrderCreateFulfillmentMutationVariables,
OrderRefundMutation,
- OrderRefundMutationVariables
+ OrderRefundMutationVariables,
+ OrderReleaseMutation,
+ OrderReleaseMutationVariables
} from "../../gql-types";
import { maybe } from "../../misc";
+import OrderCancelMutationProvider from "./OrderCancel";
import OrderCreateFulfillmentProvider from "./OrderCreateFulfillment";
import OrderPaymentCaptureProvider from "./OrderPaymentCapture";
import OrderPaymentRefundProvider from "./OrderPaymentRefund";
+import OrderReleaseMutationProvider from "./OrderRelease";
interface OrderOperationsProps extends MutationProviderProps {
order: string;
children: MutationProviderRenderProps<{
+ orderCancel: PartialMutationProviderOutput<
+ OrderCancelMutation,
+ OrderCancelMutationVariables
+ >;
orderCreateFulfillment: PartialMutationProviderOutput<
OrderCreateFulfillmentMutation,
OrderCreateFulfillmentMutationVariables
@@ -33,8 +43,14 @@ interface OrderOperationsProps extends MutationProviderProps {
OrderRefundMutation,
OrderRefundMutationVariables
>;
+ orderRelease: PartialMutationProviderOutput<
+ OrderReleaseMutation,
+ OrderReleaseMutationVariables
+ >;
}>;
onFulfillmentCreate: (data: OrderCreateFulfillmentMutation) => void;
+ onOrderCancel: (data: OrderCancelMutation) => void;
+ onOrderRelease: (data: OrderReleaseMutation) => void;
onPaymentCapture: (data: OrderCaptureMutation) => void;
onPaymentRefund: (data: OrderRefundMutation) => void;
}
@@ -47,63 +63,88 @@ const OrderOperations: React.StatelessComponent<OrderOperationsProps> = ({
onPaymentCapture,
onPaymentRefund
}) => (
- <OrderPaymentCaptureProvider
- id={order}
- onError={onError}
- onSuccess={onPaymentCapture}
- >
- {paymentCapture => (
- <OrderPaymentRefundProvider
- id={order}
- onError={onError}
- onSuccess={onPaymentRefund}
- >
- {paymentRefund => (
- <OrderCreateFulfillmentProvider
+ <OrderReleaseMutationProvider>
+ {orderRelease => (
+ <OrderCancelMutationProvider>
+ {orderCancel => (
+ <OrderPaymentCaptureProvider
id={order}
onError={onError}
- onSuccess={onFulfillmentCreate}
+ onSuccess={onPaymentCapture}
>
- {createFulfillment =>
- children({
- errors: [
- ...maybe(
- () => createFulfillment.data.fulfillmentCreate.errors,
- [] as any
- ),
- ...maybe(
- () => paymentCapture.data.orderCapture.errors,
- [] as any
- ),
- ...maybe(() => paymentRefund.data.orderRefund.errors, [] as any)
- ],
- orderCreateFulfillment: {
- data: createFulfillment.data,
- loading: createFulfillment.loading,
- mutate: variables =>
- createFulfillment.mutate({
- variables: {
- ...variables,
- input: { ...variables.input, order }
- }
- })
- },
- orderPaymentCapture: {
- data: paymentCapture.data,
- loading: paymentCapture.loading,
- mutate: variables => paymentCapture.mutate({ variables })
- },
- orderPaymentRefund: {
- data: paymentRefund.data,
- loading: paymentRefund.loading,
- mutate: variables => paymentRefund.mutate({ variables })
- }
- })
- }
- </OrderCreateFulfillmentProvider>
+ {paymentCapture => (
+ <OrderPaymentRefundProvider
+ id={order}
+ onError={onError}
+ onSuccess={onPaymentRefund}
+ >
+ {paymentRefund => (
+ <OrderCreateFulfillmentProvider
+ id={order}
+ onError={onError}
+ onSuccess={onFulfillmentCreate}
+ >
+ {createFulfillment =>
+ children({
+ errors: [
+ ...maybe(
+ () =>
+ createFulfillment.data.fulfillmentCreate.errors,
+ []
+ ),
+ ...maybe(
+ () => paymentCapture.data.orderCapture.errors,
+ []
+ ),
+ ...maybe(
+ () => paymentRefund.data.orderRefund.errors,
+ []
+ )
+ ],
+ orderCancel: {
+ data: orderCancel.data,
+ loading: orderCancel.loading,
+ mutate: variables => orderCancel.mutate({ variables })
+ },
+ orderCreateFulfillment: {
+ data: createFulfillment.data,
+ loading: createFulfillment.loading,
+ mutate: variables =>
+ createFulfillment.mutate({
+ variables: {
+ ...variables,
+ input: { ...variables.input, order }
+ }
+ })
+ },
+ orderPaymentCapture: {
+ data: paymentCapture.data,
+ loading: paymentCapture.loading,
+ mutate: variables =>
+ paymentCapture.mutate({ variables })
+ },
+ orderPaymentRefund: {
+ data: paymentRefund.data,
+ loading: paymentRefund.loading,
+ mutate: variables =>
+ paymentRefund.mutate({ variables })
+ },
+ orderRelease: {
+ data: orderRelease.data,
+ loading: orderRelease.loading,
+ mutate: variables =>
+ orderRelease.mutate({ variables })
+ }
+ })
+ }
+ </OrderCreateFulfillmentProvider>
+ )}
+ </OrderPaymentRefundProvider>
+ )}
+ </OrderPaymentCaptureProvider>
)}
- </OrderPaymentRefundProvider>
+ </OrderCancelMutationProvider>
)}
- </OrderPaymentCaptureProvider>
+ </OrderReleaseMutationProvider>
);
export default OrderOperations;
diff --git a/saleor/static/dashboard-next/orders/containers/OrderRelease.tsx b/saleor/static/dashboard-next/orders/containers/OrderRelease.tsx
new file mode 100644
index 0000000000..b7440b8f55
--- /dev/null
+++ b/saleor/static/dashboard-next/orders/containers/OrderRelease.tsx
@@ -0,0 +1,37 @@
+import * as React from "react";
+
+import {
+ OrderReleaseMutation,
+ OrderReleaseMutationVariables
+} from "../../gql-types";
+import { TypedOrderReleaseMutation } from "../mutations";
+
+import {
+ PartialMutationProviderProps,
+ PartialMutationProviderRenderProps
+} from "../..";
+
+interface OrderReleaseMutationProviderProps
+ extends PartialMutationProviderProps<OrderReleaseMutation> {
+ children: PartialMutationProviderRenderProps<
+ OrderReleaseMutation,
+ OrderReleaseMutationVariables
+ >;
+}
+
+const OrderReleaseMutationProvider: React.StatelessComponent<
+ OrderReleaseMutationProviderProps
+> = ({ children, onError, onSuccess }) => (
+ <TypedOrderReleaseMutation onCompleted={onSuccess} onError={onError}>
+ {(mutate, { data, error, loading }) =>
+ children({
+ data,
+ error,
+ loading,
+ mutate
+ })
+ }
+ </TypedOrderReleaseMutation>
+);
+
+export default OrderReleaseMutationProvider;
diff --git a/saleor/static/dashboard-next/orders/views/OrderDetails.tsx b/saleor/static/dashboard-next/orders/views/OrderDetails.tsx
index 00dc30f5c8..97528e7f80 100644
--- a/saleor/static/dashboard-next/orders/views/OrderDetails.tsx
+++ b/saleor/static/dashboard-next/orders/views/OrderDetails.tsx
@@ -16,10 +16,6 @@ import { productUrl } from "../../products";
import OrderDetailsPage from "../components/OrderDetailsPage";
import OrderOperations from "../containers/OrderOperations";
import { OrderVariantSearchProvider } from "../containers/OrderVariantSearch";
-import {
- TypedOrderCancelMutation,
- TypedOrderReleaseMutation
-} from "../mutations";
import {
TypedOrderDetailsQuery,
TypedOrderShippingMethodsQuery
@@ -45,175 +41,177 @@ export const OrderDetails: React.StatelessComponent<OrderDetailsProps> = ({
return (
<UserContext.Consumer>
{({ user }) => (
- <TypedOrderCancelMutation variables={{ id }}>
- {cancelOrder => (
- <TypedOrderReleaseMutation variables={{ id }}>
- {releasePayment => (
- <TypedOrderShippingMethodsQuery>
- {({ data: shippingMethodsData }) => (
- <OrderVariantSearchProvider>
- {({ variants: { search, searchOpts } }) => {
- const handlePaymentCapture = (
- data: OrderCaptureMutation
- ) => {
- if (
- !maybe(
- () => data.orderCapture.errors.length
- )
- ) {
- pushMessage({
- text: i18n.t(
- "Payment succesfully captured",
- { context: "notification" }
- )
- });
- }
- };
- const handlePaymentRefund = (
- data: OrderRefundMutation
- ) => {
- if (
- !maybe(() => data.orderRefund.errors.length)
- ) {
- pushMessage({
- text: i18n.t(
- "Payment succesfully refunded",
- { context: "notification" }
+ <TypedOrderShippingMethodsQuery>
+ {({ data: shippingMethodsData }) => (
+ <OrderVariantSearchProvider>
+ {({ variants: { search, searchOpts } }) => {
+ const handlePaymentCapture = (
+ data: OrderCaptureMutation
+ ) => {
+ if (
+ !maybe(() => data.orderCapture.errors.length)
+ ) {
+ pushMessage({
+ text: i18n.t("Payment succesfully captured", {
+ context: "notification"
+ })
+ });
+ }
+ };
+ const handlePaymentRefund = (
+ data: OrderRefundMutation
+ ) => {
+ if (
+ !maybe(() => data.orderRefund.errors.length)
+ ) {
+ pushMessage({
+ text: i18n.t("Payment succesfully refunded", {
+ context: "notification"
+ })
+ });
+ }
+ };
+ const handleFulfillmentCreate = (
+ data: OrderCreateFulfillmentMutation
+ ) => {
+ if (
+ !maybe(
+ () => data.fulfillmentCreate.errors.length
+ )
+ ) {
+ pushMessage({
+ text: i18n.t("Items succesfully fulfilled", {
+ context: "notification"
+ })
+ });
+ }
+ };
+ const handleOrderCancel = () => {
+ pushMessage({
+ text: i18n.t("Order succesfully cancelled", {
+ context: "notification"
+ })
+ });
+ };
+ const handleOrderRelease = () => {
+ pushMessage({
+ text: i18n.t(
+ "Order payment succesfully released",
+ {
+ context: "notification"
+ }
+ )
+ });
+ };
+ return (
+ <OrderOperations
+ order={id}
+ onError={undefined}
+ onFulfillmentCreate={handleFulfillmentCreate}
+ onOrderCancel={handleOrderCancel}
+ onOrderRelease={handleOrderRelease}
+ onPaymentCapture={handlePaymentCapture}
+ onPaymentRefund={handlePaymentRefund}
+ >
+ {({
+ orderCreateFulfillment,
+ orderPaymentCapture,
+ orderPaymentRefund,
+ orderCancel,
+ orderRelease
+ }) => (
+ <OrderDetailsPage
+ fetchVariants={search}
+ variants={maybe(() =>
+ searchOpts.data.products.edges
+ .map(edge => edge.node)
+ .map(product => ({
+ ...product,
+ variants: product.variants.edges.map(
+ edge => edge.node
)
- });
- }
- };
- const handleFulfillmentCreate = (
- data: OrderCreateFulfillmentMutation
- ) => {
- if (
- !maybe(
- () =>
- data.fulfillmentCreate.errors.length
+ }))
+ .map(product =>
+ product.variants.map(variant => ({
+ ...variant,
+ name: `${product.name}(${
+ variant.name
+ })`
+ }))
+ )
+ .reduce(
+ (prev, curr) => prev.concat(curr),
+ []
+ )
+ )}
+ onBack={() => navigate(orderListUrl)}
+ order={order}
+ shippingMethods={maybe(() =>
+ ([] as Array<{
+ id: string;
+ name: string;
+ }>).concat(
+ ...shippingMethodsData.shippingZones.edges.map(
+ edge =>
+ edge.node.shippingMethods.edges.map(
+ edge => edge.node
+ )
)
- ) {
- pushMessage({
- text: i18n.t(
- "Items succesfully fulfilled",
- { context: "notification" }
+ )
+ )}
+ user={user}
+ onOrderCancel={() =>
+ orderCancel.mutate({ id })
+ }
+ onOrderFulfill={variables =>
+ orderCreateFulfillment.mutate({
+ input: {
+ ...variables,
+ lines: maybe(
+ () => order.lines.edges,
+ []
)
- });
- }
- };
- return (
- <OrderOperations
- order={id}
- onError={undefined}
- onFulfillmentCreate={
- handleFulfillmentCreate
+ .map(edge => edge.node)
+ .filter(
+ line =>
+ line.quantityFulfilled <
+ line.quantity
+ )
+ .map((line, lineIndex) => ({
+ orderLineId: line.id,
+ quantity:
+ variables.lines[lineIndex]
+ })),
+ order: order.id
}
- onPaymentCapture={handlePaymentCapture}
- onPaymentRefund={handlePaymentRefund}
- >
- {({
- orderCreateFulfillment,
- orderPaymentCapture,
- orderPaymentRefund
- }) => (
- <OrderDetailsPage
- fetchVariants={search}
- variants={maybe(() =>
- searchOpts.data.products.edges
- .map(edge => edge.node)
- .map(product => ({
- ...product,
- variants: product.variants.edges.map(
- edge => edge.node
- )
- }))
- .map(product =>
- product.variants.map(
- variant => ({
- ...variant,
- name: `${product.name}(${
- variant.name
- })`
- })
- )
- )
- .reduce(
- (prev, curr) =>
- prev.concat(curr),
- []
- )
- )}
- onBack={() =>
- navigate(orderListUrl)
- }
- order={order}
- shippingMethods={maybe(() =>
- ([] as Array<{
- id: string;
- name: string;
- }>).concat(
- ...shippingMethodsData.shippingZones.edges.map(
- edge =>
- edge.node.shippingMethods.edges.map(
- edge => edge.node
- )
- )
- )
- )}
- user={user}
- onOrderCancel={cancelOrder}
- onOrderFulfill={variables =>
- orderCreateFulfillment.mutate({
- input: {
- ...variables,
- lines: maybe(
- () => order.lines.edges,
- []
- )
- .map(edge => edge.node)
- .filter(
- line =>
- line.quantityFulfilled <
- line.quantity
- )
- .map((line, lineIndex) => ({
- orderLineId: line.id,
- quantity:
- variables.lines[
- lineIndex
- ]
- })),
- order: order.id
- }
- })
- }
- onPaymentCapture={variables =>
- orderPaymentCapture.mutate({
- ...variables,
- id
- })
- }
- onPaymentRelease={releasePayment}
- onPaymentRefund={variables =>
- orderPaymentRefund.mutate({
- ...variables,
- id
- })
- }
- onProductAdd={() => undefined}
- onProductClick={id => () =>
- navigate(productUrl(id))}
- />
- )}
- </OrderOperations>
- );
- }}
- </OrderVariantSearchProvider>
- )}
- </TypedOrderShippingMethodsQuery>
- )}
- </TypedOrderReleaseMutation>
+ })
+ }
+ onPaymentCapture={variables =>
+ orderPaymentCapture.mutate({
+ ...variables,
+ id
+ })
+ }
+ onPaymentRelease={() =>
+ orderRelease.mutate({ id })
+ }
+ onPaymentRefund={variables =>
+ orderPaymentRefund.mutate({
+ ...variables,
+ id
+ })
+ }
+ onProductAdd={() => undefined}
+ onProductClick={id => () =>
+ navigate(productUrl(id))}
+ />
+ )}
+ </OrderOperations>
+ );
+ }}
+ </OrderVariantSearchProvider>
)}
- </TypedOrderCancelMutation>
+ </TypedOrderShippingMethodsQuery>
)}
</UserContext.Consumer>
);