Skip to content

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

Fix applicable shipping methods

dominik-zeglen3ec4df31f5e7
diff --git a/saleor/static/dashboard-next/orders/components/OrderDetailsPage/OrderDetailsPage.tsx b/saleor/static/dashboard-next/orders/components/OrderDetailsPage/OrderDetailsPage.tsx
index 88e271d67d..cf40e3783e 100644
--- a/saleor/static/dashboard-next/orders/components/OrderDetailsPage/OrderDetailsPage.tsx
+++ b/saleor/static/dashboard-next/orders/components/OrderDetailsPage/OrderDetailsPage.tsx
@@ -141,7 +141,6 @@ interface OrderDetailsPageProps {
variantsLoading?: boolean;
errors: UserError[];
fetchUsers?(value: string);
- fetchShippingMethods?(value: string);
fetchVariants?(value: string);
onBack();
onBillingAddressEdit(data: AddressTypeInput);
@@ -252,7 +251,6 @@ class OrderDetailsPageComponent extends React.Component<
users,
variants,
variantsLoading,
- fetchShippingMethods,
fetchUsers,
fetchVariants,
onBack,
@@ -366,10 +364,7 @@ class OrderDetailsPageComponent extends React.Component<
<>
<Form
initial={{
- shippingMethod: {
- label: order.shippingMethodName,
- value: order.shippingMethod && order.shippingMethod.id
- }
+ shippingMethod: maybe(() => order.shippingMethod.id, "")
}}
>
{({ change, data }) => (
@@ -377,7 +372,6 @@ class OrderDetailsPageComponent extends React.Component<
open={openedShippingMethodEditDialog}
shippingMethod={data.shippingMethod}
shippingMethods={shippingMethods}
- fetchShippingMethods={fetchShippingMethods}
onChange={change}
onClose={this.toggleShippingMethodEditDialog}
/>
diff --git a/saleor/static/dashboard-next/orders/components/OrderShippingMethodEditDialog/OrderShippingMethodEditDialog.tsx b/saleor/static/dashboard-next/orders/components/OrderShippingMethodEditDialog/OrderShippingMethodEditDialog.tsx
index ec9d509549..c45fb2014e 100644
--- a/saleor/static/dashboard-next/orders/components/OrderShippingMethodEditDialog/OrderShippingMethodEditDialog.tsx
+++ b/saleor/static/dashboard-next/orders/components/OrderShippingMethodEditDialog/OrderShippingMethodEditDialog.tsx
@@ -6,21 +6,16 @@ import DialogTitle from "@material-ui/core/DialogTitle";
import { withStyles } from "@material-ui/core/styles";
import * as React from "react";
-import { SingleAutocompleteSelectField } from "../../../components/SingleAutocompleteSelectField";
+import { SingleSelectField } from "../../../components/SingleSelectField";
import i18n from "../../../i18n";
interface OrderShippingMethodEditDialogProps {
open: boolean;
- shippingMethod?: {
- label: string;
- value: string;
- };
+ shippingMethod: string;
shippingMethods?: Array<{
id: string;
name: string;
}>;
- loading?: boolean;
- fetchShippingMethods(value: string);
onChange(event: React.ChangeEvent<any>);
onClose?();
onConfirm?(event: React.FormEvent<any>);
@@ -51,33 +46,28 @@ const OrderShippingMethodEditDialog = decorate<
({
classes,
open,
- loading,
shippingMethod,
shippingMethods,
- fetchShippingMethods,
onChange,
onClose,
onConfirm
}) => {
- const choices =
- !loading && shippingMethods
- ? shippingMethods.map(s => ({
- label: s.name,
- value: s.id
- }))
- : [];
+ const choices = shippingMethods
+ ? shippingMethods.map(s => ({
+ label: s.name,
+ value: s.id
+ }))
+ : [];
return (
<Dialog open={open} classes={{ paper: classes.dialog }}>
<DialogTitle>
{i18n.t("Edit shipping method", { context: "title" })}
</DialogTitle>
<DialogContent className={classes.root}>
- <SingleAutocompleteSelectField
+ <SingleSelectField
choices={choices}
- loading={loading}
- name="user"
+ name="shippingMethod"
value={shippingMethod}
- fetchChoices={fetchShippingMethods}
onChange={onChange}
/>
</DialogContent>
diff --git a/saleor/static/dashboard-next/orders/queries.ts b/saleor/static/dashboard-next/orders/queries.ts
index 133e279c0d..1f9702e49d 100644
--- a/saleor/static/dashboard-next/orders/queries.ts
+++ b/saleor/static/dashboard-next/orders/queries.ts
@@ -3,7 +3,6 @@ import gql from "graphql-tag";
import { TypedQuery } from "../queries";
import { OrderDetails, OrderDetailsVariables } from "./types/OrderDetails";
import { OrderList, OrderListVariables } from "./types/OrderList";
-import { OrderShippingMethods } from "./types/OrderShippingMethods";
import {
OrderVariantSearch,
OrderVariantSearchVariables
@@ -168,6 +167,10 @@ export const fragmentOrderDetails = gql`
id
email
}
+ availableShippingMethods {
+ id
+ name
+ }
}
`;
@@ -190,25 +193,6 @@ export const TypedOrderDetailsQuery = TypedQuery<
OrderDetailsVariables
>(orderDetailsQuery);
-export const orderShippingMethodsQuery = gql`
- query OrderShippingMethods {
- shippingZones {
- edges {
- node {
- shippingMethods {
- id
- name
- }
- }
- }
- }
- }
-`;
-export const TypedOrderShippingMethodsQuery = TypedQuery<
- OrderShippingMethods,
- {}
->(orderShippingMethodsQuery);
-
export const orderVariantSearchQuery = gql`
query OrderVariantSearch($search: String!) {
products(query: $search, first: 20) {
diff --git a/saleor/static/dashboard-next/orders/types/OrderCancel.ts b/saleor/static/dashboard-next/orders/types/OrderCancel.ts
index 494cbde2a8..1d8dfe8481 100644
--- a/saleor/static/dashboard-next/orders/types/OrderCancel.ts
+++ b/saleor/static/dashboard-next/orders/types/OrderCancel.ts
@@ -189,6 +189,12 @@ export interface OrderCancel_orderCancel_order_user {
email: string;
}
+export interface OrderCancel_orderCancel_order_availableShippingMethods {
+ __typename: "ShippingMethod";
+ id: string;
+ name: string;
+}
+
export interface OrderCancel_orderCancel_order {
__typename: "Order";
id: string;
@@ -209,6 +215,7 @@ export interface OrderCancel_orderCancel_order {
totalAuthorized: OrderCancel_orderCancel_order_totalAuthorized | null;
totalCaptured: OrderCancel_orderCancel_order_totalCaptured | null;
user: OrderCancel_orderCancel_order_user | null;
+ availableShippingMethods: (OrderCancel_orderCancel_order_availableShippingMethods | null)[] | null;
}
export interface OrderCancel_orderCancel {
diff --git a/saleor/static/dashboard-next/orders/types/OrderCapture.ts b/saleor/static/dashboard-next/orders/types/OrderCapture.ts
index 65310414b2..9058e97897 100644
--- a/saleor/static/dashboard-next/orders/types/OrderCapture.ts
+++ b/saleor/static/dashboard-next/orders/types/OrderCapture.ts
@@ -195,6 +195,12 @@ export interface OrderCapture_orderCapture_order_user {
email: string;
}
+export interface OrderCapture_orderCapture_order_availableShippingMethods {
+ __typename: "ShippingMethod";
+ id: string;
+ name: string;
+}
+
export interface OrderCapture_orderCapture_order {
__typename: "Order";
id: string;
@@ -215,6 +221,7 @@ export interface OrderCapture_orderCapture_order {
totalAuthorized: OrderCapture_orderCapture_order_totalAuthorized | null;
totalCaptured: OrderCapture_orderCapture_order_totalCaptured | null;
user: OrderCapture_orderCapture_order_user | null;
+ availableShippingMethods: (OrderCapture_orderCapture_order_availableShippingMethods | null)[] | null;
}
export interface OrderCapture_orderCapture {
diff --git a/saleor/static/dashboard-next/orders/types/OrderDetails.ts b/saleor/static/dashboard-next/orders/types/OrderDetails.ts
index 757388cf9f..76e0c89439 100644
--- a/saleor/static/dashboard-next/orders/types/OrderDetails.ts
+++ b/saleor/static/dashboard-next/orders/types/OrderDetails.ts
@@ -189,6 +189,12 @@ export interface OrderDetails_order_user {
email: string;
}
+export interface OrderDetails_order_availableShippingMethods {
+ __typename: "ShippingMethod";
+ id: string;
+ name: string;
+}
+
export interface OrderDetails_order {
__typename: "Order";
id: string;
@@ -209,6 +215,7 @@ export interface OrderDetails_order {
totalAuthorized: OrderDetails_order_totalAuthorized | null;
totalCaptured: OrderDetails_order_totalCaptured | null;
user: OrderDetails_order_user | null;
+ availableShippingMethods: (OrderDetails_order_availableShippingMethods | null)[] | null;
}
export interface OrderDetails_shop_countries {
diff --git a/saleor/static/dashboard-next/orders/types/OrderDetailsFragment.ts b/saleor/static/dashboard-next/orders/types/OrderDetailsFragment.ts
index bde9510031..8447cbedc6 100644
--- a/saleor/static/dashboard-next/orders/types/OrderDetailsFragment.ts
+++ b/saleor/static/dashboard-next/orders/types/OrderDetailsFragment.ts
@@ -189,6 +189,12 @@ export interface OrderDetailsFragment_user {
email: string;
}
+export interface OrderDetailsFragment_availableShippingMethods {
+ __typename: "ShippingMethod";
+ id: string;
+ name: string;
+}
+
export interface OrderDetailsFragment {
__typename: "Order";
id: string;
@@ -209,4 +215,5 @@ export interface OrderDetailsFragment {
totalAuthorized: OrderDetailsFragment_totalAuthorized | null;
totalCaptured: OrderDetailsFragment_totalCaptured | null;
user: OrderDetailsFragment_user | null;
+ availableShippingMethods: (OrderDetailsFragment_availableShippingMethods | null)[] | null;
}
diff --git a/saleor/static/dashboard-next/orders/types/OrderRefund.ts b/saleor/static/dashboard-next/orders/types/OrderRefund.ts
index d1328046e4..9e26aedfe0 100644
--- a/saleor/static/dashboard-next/orders/types/OrderRefund.ts
+++ b/saleor/static/dashboard-next/orders/types/OrderRefund.ts
@@ -195,6 +195,12 @@ export interface OrderRefund_orderRefund_order_user {
email: string;
}
+export interface OrderRefund_orderRefund_order_availableShippingMethods {
+ __typename: "ShippingMethod";
+ id: string;
+ name: string;
+}
+
export interface OrderRefund_orderRefund_order {
__typename: "Order";
id: string;
@@ -215,6 +221,7 @@ export interface OrderRefund_orderRefund_order {
totalAuthorized: OrderRefund_orderRefund_order_totalAuthorized | null;
totalCaptured: OrderRefund_orderRefund_order_totalCaptured | null;
user: OrderRefund_orderRefund_order_user | null;
+ availableShippingMethods: (OrderRefund_orderRefund_order_availableShippingMethods | null)[] | null;
}
export interface OrderRefund_orderRefund {
diff --git a/saleor/static/dashboard-next/orders/types/OrderRelease.ts b/saleor/static/dashboard-next/orders/types/OrderRelease.ts
index 101966b707..b6b8acdcca 100644
--- a/saleor/static/dashboard-next/orders/types/OrderRelease.ts
+++ b/saleor/static/dashboard-next/orders/types/OrderRelease.ts
@@ -189,6 +189,12 @@ export interface OrderRelease_orderRelease_order_user {
email: string;
}
+export interface OrderRelease_orderRelease_order_availableShippingMethods {
+ __typename: "ShippingMethod";
+ id: string;
+ name: string;
+}
+
export interface OrderRelease_orderRelease_order {
__typename: "Order";
id: string;
@@ -209,6 +215,7 @@ export interface OrderRelease_orderRelease_order {
totalAuthorized: OrderRelease_orderRelease_order_totalAuthorized | null;
totalCaptured: OrderRelease_orderRelease_order_totalCaptured | null;
user: OrderRelease_orderRelease_order_user | null;
+ availableShippingMethods: (OrderRelease_orderRelease_order_availableShippingMethods | null)[] | null;
}
export interface OrderRelease_orderRelease {
diff --git a/saleor/static/dashboard-next/orders/views/OrderDetails.tsx b/saleor/static/dashboard-next/orders/views/OrderDetails.tsx
index 411b88cc9b..c6b970a05a 100644
--- a/saleor/static/dashboard-next/orders/views/OrderDetails.tsx
+++ b/saleor/static/dashboard-next/orders/views/OrderDetails.tsx
@@ -16,10 +16,7 @@ import { productUrl } from "../../products";
import OrderDetailsPage from "../components/OrderDetailsPage";
import OrderOperations from "../containers/OrderOperations";
import { OrderVariantSearchProvider } from "../containers/OrderVariantSearch";
-import {
- TypedOrderDetailsQuery,
- TypedOrderShippingMethodsQuery
-} from "../queries";
+import { TypedOrderDetailsQuery } from "../queries";
import { OrderAddNote } from "../types/OrderAddNote";
import { OrderUpdate } from "../types/OrderUpdate";
@@ -43,234 +40,200 @@ export const OrderDetails: React.StatelessComponent<OrderDetailsProps> = ({
return (
<UserContext.Consumer>
{({ user }) => (
- <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"
+ <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"
+ })
+ });
+ };
+ const handleNoteAdd = (data: OrderAddNote) => {
+ if (!maybe(() => data.orderAddNote.errors.length)) {
+ pushMessage({
+ text: i18n.t("Note succesfully added", {
+ context: "notification"
+ })
+ });
+ }
+ };
+ const handleUpdate = (data: OrderUpdate) => {
+ if (!maybe(() => data.orderUpdate.errors.length)) {
+ pushMessage({
+ text: i18n.t("Order succesfully updated", {
+ context: "notification"
+ })
+ });
+ }
+ };
+ return (
+ <OrderOperations
+ order={id}
+ onError={undefined}
+ onFulfillmentCreate={handleFulfillmentCreate}
+ onNoteAdd={handleNoteAdd}
+ onOrderCancel={handleOrderCancel}
+ onOrderRelease={handleOrderRelease}
+ onPaymentCapture={handlePaymentCapture}
+ onPaymentRefund={handlePaymentRefund}
+ onUpdate={handleUpdate}
+ >
+ {({
+ errors,
+ orderAddNote,
+ orderCancel,
+ orderCreateFulfillment,
+ orderPaymentCapture,
+ orderPaymentRefund,
+ orderRelease,
+ orderUpdate
+ }) => (
+ <OrderDetailsPage
+ errors={errors}
+ onNoteAdd={variables =>
+ orderAddNote.mutate({
+ input: variables,
+ order: id
})
- });
- }
- };
- const handleOrderCancel = () => {
- pushMessage({
- text: i18n.t("Order succesfully cancelled", {
- context: "notification"
- })
- });
- };
- const handleOrderRelease = () => {
- pushMessage({
- text: i18n.t(
- "Order payment succesfully released",
- {
- context: "notification"
- }
- )
- });
- };
- const handleNoteAdd = (data: OrderAddNote) => {
- if (
- !maybe(() => data.orderAddNote.errors.length)
- ) {
- pushMessage({
- text: i18n.t("Note succesfully added", {
- context: "notification"
- })
- });
- }
- };
- const handleUpdate = (data: OrderUpdate) => {
- if (
- !maybe(() => data.orderUpdate.errors.length)
- ) {
- pushMessage({
- text: i18n.t("Order succesfully updated", {
- context: "notification"
- })
- });
- }
- };
- return (
- <OrderOperations
- order={id}
- onError={undefined}
- onFulfillmentCreate={handleFulfillmentCreate}
- onNoteAdd={handleNoteAdd}
- onOrderCancel={handleOrderCancel}
- onOrderRelease={handleOrderRelease}
- onPaymentCapture={handlePaymentCapture}
- onPaymentRefund={handlePaymentRefund}
- onUpdate={handleUpdate}
- >
- {({
- errors,
- orderAddNote,
- orderCancel,
- orderCreateFulfillment,
- orderPaymentCapture,
- orderPaymentRefund,
- orderRelease,
- orderUpdate
- }) => (
- <OrderDetailsPage
- errors={errors}
- onNoteAdd={variables =>
- orderAddNote.mutate({
- input: variables,
- order: id
- })
- }
- 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}
- countries={maybe(
- () => data.shop.countries,
+ }
+ 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),
[]
- ).map(country => ({
- code: country.code,
- label: country.country
- }))}
- shippingMethods={maybe(() =>
- ([] as Array<{
- id: string;
- name: string;
- }>).concat(
- ...shippingMethodsData.shippingZones.edges.map(
- edge =>
- edge.node.shippingMethods.edges.map(
- edge => edge.node
- )
+ )
+ )}
+ onBack={() => navigate(orderListUrl)}
+ order={order}
+ countries={maybe(
+ () => data.shop.countries,
+ []
+ ).map(country => ({
+ code: country.code,
+ label: country.country
+ }))}
+ shippingMethods={maybe(
+ () => data.order.availableShippingMethods,
+ []
+ )}
+ user={user}
+ onOrderCancel={() => orderCancel.mutate({ id })}
+ onOrderFulfill={variables =>
+ orderCreateFulfillment.mutate({
+ input: {
+ ...variables,
+ lines: maybe(() => order.lines.edges, [])
+ .map(edge => edge.node)
+ .filter(
+ line =>
+ line.quantityFulfilled <
+ line.quantity
)
- )
- )}
- user={user}
- onOrderCancel={() =>
- orderCancel.mutate({ id })
- }
- 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={() =>
- orderRelease.mutate({ id })
- }
- onPaymentRefund={variables =>
- orderPaymentRefund.mutate({
- ...variables,
- id
- })
+ .map((line, lineIndex) => ({
+ orderLineId: line.id,
+ quantity: variables.lines[lineIndex]
+ })),
+ order: order.id
}
- onProductAdd={() => undefined}
- onProductClick={id => () =>
- navigate(productUrl(id))}
- onBillingAddressEdit={variables =>
- orderUpdate.mutate({
- id,
- input: {
- billingAddress: variables
- }
- })
+ })
+ }
+ onPaymentCapture={variables =>
+ orderPaymentCapture.mutate({
+ ...variables,
+ id
+ })
+ }
+ onPaymentRelease={() =>
+ orderRelease.mutate({ id })
+ }
+ onPaymentRefund={variables =>
+ orderPaymentRefund.mutate({
+ ...variables,
+ id
+ })
+ }
+ onProductAdd={() => undefined}
+ onProductClick={id => () =>
+ navigate(productUrl(id))}
+ onBillingAddressEdit={variables =>
+ orderUpdate.mutate({
+ id,
+ input: {
+ billingAddress: variables
}
- onShippingAddressEdit={variables =>
- orderUpdate.mutate({
- id,
- input: {
- shippingAddress: variables
- }
- })
+ })
+ }
+ onShippingAddressEdit={variables =>
+ orderUpdate.mutate({
+ id,
+ input: {
+ shippingAddress: variables
}
- />
- )}
- </OrderOperations>
- );
- }}
- </OrderVariantSearchProvider>
- )}
- </TypedOrderShippingMethodsQuery>
+ })
+ }
+ />
+ )}
+ </OrderOperations>
+ );
+ }}
+ </OrderVariantSearchProvider>
)}
</UserContext.Consumer>
);