Skip to content

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

Add order variant search

dominik-zeglen4dc652f6a29f
diff --git a/saleor/static/dashboard-next/orders/components/OrderDetailsPage/OrderDetailsPage.tsx b/saleor/static/dashboard-next/orders/components/OrderDetailsPage/OrderDetailsPage.tsx
index 9689c2d397..a6941f27cf 100644
--- a/saleor/static/dashboard-next/orders/components/OrderDetailsPage/OrderDetailsPage.tsx
+++ b/saleor/static/dashboard-next/orders/components/OrderDetailsPage/OrderDetailsPage.tsx
@@ -26,7 +26,9 @@ import OrderPaymentDialog, {
FormData as OrderPaymentFormData
} from "../OrderPaymentDialog";
import OrderPaymentReleaseDialog from "../OrderPaymentReleaseDialog";
-import OrderProductAddDialog from "../OrderProductAddDialog";
+import OrderProductAddDialog, {
+ FormData as ProductAddFormData
+} from "../OrderProductAddDialog";
import OrderShippingMethodEditDialog from "../OrderShippingMethodEditDialog";
import OrderSummary from "../OrderSummary";
@@ -132,7 +134,7 @@ interface OrderDetailsPageProps {
id: string;
name: string;
sku: string;
- stockAllocated;
+ stockQuantity: number;
}>;
variantsLoading?: boolean;
fetchUsers?(value: string);
@@ -144,6 +146,7 @@ interface OrderDetailsPageProps {
onOrderFulfill(data: OrderFulfillFormData);
onOrderLineChange?(id: string): (value: string) => () => void;
onOrderLineRemove?(id: string): () => void;
+ onProductAdd(data: ProductAddFormData);
onProductClick?(id: string);
onPackingSlipClick?(id: string);
onPaymentCapture(data: OrderPaymentFormData);
@@ -257,6 +260,7 @@ class OrderDetailsPageComponent extends React.Component<
onPaymentCapture,
onPaymentRefund,
onPaymentRelease,
+ onProductAdd,
onProductClick
} = this.props;
const {
@@ -342,31 +346,16 @@ class OrderDetailsPageComponent extends React.Component<
onClose={this.togglePaymentRefundDialog}
onSubmit={onPaymentRefund}
/>
+ <OrderProductAddDialog
+ loading={variantsLoading}
+ open={openedOrderProductAddDialog}
+ variants={variants}
+ fetchVariants={fetchVariants}
+ onClose={this.toggleOrderProductAddDialog}
+ onSubmit={onProductAdd}
+ />
{order && (
<>
- <Form
- initial={{
- quantity: 1,
- variant: {
- label: "",
- value: ""
- }
- }}
- >
- {({ data, change, submit }) => (
- <OrderProductAddDialog
- loading={variantsLoading}
- open={openedOrderProductAddDialog}
- quantity={data.quantity}
- variant={data.variant}
- variants={variants}
- fetchVariants={fetchVariants}
- onChange={change}
- onClose={this.toggleOrderProductAddDialog}
- onConfirm={submit}
- />
- )}
- </Form>
<Form
initial={{
shippingMethod: {
diff --git a/saleor/static/dashboard-next/orders/components/OrderProductAddDialog/OrderProductAddDialog.tsx b/saleor/static/dashboard-next/orders/components/OrderProductAddDialog/OrderProductAddDialog.tsx
index 472929a027..f76d8ba44f 100644
--- a/saleor/static/dashboard-next/orders/components/OrderProductAddDialog/OrderProductAddDialog.tsx
+++ b/saleor/static/dashboard-next/orders/components/OrderProductAddDialog/OrderProductAddDialog.tsx
@@ -4,31 +4,38 @@ import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent";
import DialogTitle from "@material-ui/core/DialogTitle";
import InputAdornment from "@material-ui/core/InputAdornment";
-import { withStyles } from "@material-ui/core/styles";
+import { withStyles, WithStyles } from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";
import * as React from "react";
+import Form from "../../../components/Form";
import { SingleAutocompleteSelectField } from "../../../components/SingleAutocompleteSelectField";
import i18n from "../../../i18n";
-interface OrderProductAddDialogProps {
- open: boolean;
- variant?: {
+export interface FormData {
+ quantity: number;
+ variant: {
label: string;
value: string;
};
- quantity?: number;
+}
+
+interface OrderProductAddDialogProps {
+ open: boolean;
variants?: Array<{
id: string;
name: string;
sku: string;
- stockAllocated;
+ stockQuantity: number;
}>;
- loading?: boolean;
- fetchVariants(value: string);
- onChange(event: React.ChangeEvent<any>);
- onClose?();
- onConfirm?(event: React.FormEvent<any>);
+ loading: boolean;
+ fetchVariants: (value: string) => void;
+ onClose: () => void;
+ onSubmit: (data: FormData) => void;
+}
+
+interface OrderProductAddDialogState {
+ maxQuantity: number;
}
const decorate = withStyles(
@@ -53,75 +60,98 @@ const decorate = withStyles(
}),
{ name: "OrderProductAddDialog" }
);
+const initialForm: FormData = {
+ quantity: 0,
+ variant: {
+ label: "",
+ value: ""
+ }
+};
const OrderProductAddDialog = decorate<OrderProductAddDialogProps>(
- ({
- classes,
- open,
- loading,
- variant,
- variants,
- quantity,
- fetchVariants,
- onChange,
- onClose,
- onConfirm
- }) => {
- const choices =
- !loading && variants
- ? variants.map(v => ({
- label: `${v.sku} ${v.name}`,
- value: v.id
- }))
- : [];
- const maxQuantity =
- !loading && variant && variant.value && variants
- ? variants.filter(v => v.id === variant.value)[0].stockAllocated
- : 0;
- return (
- <Dialog open={open} classes={{ paper: classes.dialog }}>
- <DialogTitle>{i18n.t("Add product")}</DialogTitle>
- <DialogContent className={classes.root}>
- <div className={classes.select}>
- <SingleAutocompleteSelectField
- name="variant"
- value={variant}
- choices={choices}
- onChange={onChange}
- fetchChoices={fetchVariants}
- loading={loading}
- />
- </div>
- <div>
- <TextField
- type="number"
- inputProps={{
- max: maxQuantity,
- style: { textAlign: "right", width: "4rem" }
- }}
- value={quantity}
- onChange={onChange}
- name="quantity"
- error={quantity > maxQuantity}
- InputProps={{
- endAdornment: (
- <InputAdornment position="end">
- {`/ ${maxQuantity}`}
- </InputAdornment>
- )
- }}
- />
- </div>
- </DialogContent>
- <DialogActions>
- <Button onClick={onClose}>
- {i18n.t("Cancel", { context: "button" })}
- </Button>
- <Button color="primary" variant="raised" onClick={onConfirm}>
- {i18n.t("Confirm", { context: "button" })}
- </Button>
- </DialogActions>
- </Dialog>
- );
+ class OrderProductAddDialogComponent extends React.Component<
+ OrderProductAddDialogProps &
+ WithStyles<"dialog" | "root" | "select" | "textRight">
+ > {
+ state: OrderProductAddDialogState = { maxQuantity: 0 };
+ render() {
+ const {
+ classes,
+ open,
+ loading,
+ variants,
+ fetchVariants,
+ onClose,
+ onSubmit
+ } = this.props;
+ return (
+ <Dialog open={open} classes={{ paper: classes.dialog }}>
+ <Form initial={initialForm} onSubmit={onSubmit}>
+ {({ data, change }) => {
+ const choices =
+ !loading && variants
+ ? variants.map(v => ({
+ label: `${v.sku} ${v.name}`,
+ value: v.id
+ }))
+ : [];
+ const handleSelect = (event: React.ChangeEvent<any>) => {
+ this.setState({
+ maxQuantity: variants.filter(
+ v => v.id === event.target.value.value
+ )[0].stockQuantity
+ });
+ change(event);
+ };
+ return (
+ <>
+ <DialogTitle>{i18n.t("Add product")}</DialogTitle>
+ <DialogContent className={classes.root}>
+ <div className={classes.select}>
+ <SingleAutocompleteSelectField
+ name="variant"
+ value={data.variant}
+ choices={choices}
+ onChange={handleSelect}
+ fetchChoices={fetchVariants}
+ loading={loading}
+ />
+ </div>
+ <div>
+ <TextField
+ type="number"
+ inputProps={{
+ max: this.state.maxQuantity,
+ style: { textAlign: "right", width: "4rem" }
+ }}
+ value={data.quantity}
+ onChange={change}
+ name="quantity"
+ error={data.quantity > this.state.maxQuantity}
+ InputProps={{
+ endAdornment: (
+ <InputAdornment position="end">
+ {`/ ${this.state.maxQuantity}`}
+ </InputAdornment>
+ )
+ }}
+ />
+ </div>
+ </DialogContent>
+ <DialogActions>
+ <Button onClick={onClose}>
+ {i18n.t("Cancel", { context: "button" })}
+ </Button>
+ <Button color="primary" variant="raised" type="submit">
+ {i18n.t("Confirm", { context: "button" })}
+ </Button>
+ </DialogActions>
+ </>
+ );
+ }}
+ </Form>
+ </Dialog>
+ );
+ }
}
);
export default OrderProductAddDialog;
diff --git a/saleor/static/dashboard-next/orders/containers/OrderVariantSearch.tsx b/saleor/static/dashboard-next/orders/containers/OrderVariantSearch.tsx
new file mode 100644
index 0000000000..dc9f29e004
--- /dev/null
+++ b/saleor/static/dashboard-next/orders/containers/OrderVariantSearch.tsx
@@ -0,0 +1,52 @@
+import * as React from "react";
+import { QueryResult } from "react-apollo";
+import { TypedOrderVariantSearch } from "../queries";
+import {
+ OrderVariantSearch,
+ OrderVariantSearchVariables
+} from "../types/OrderVariantSearch";
+
+interface OrderVariantSearchProviderProps {
+ children:
+ | ((
+ props: {
+ variants: {
+ search: (query: string) => void;
+ searchOpts: QueryResult<
+ OrderVariantSearch,
+ OrderVariantSearchVariables
+ >;
+ };
+ }
+ ) => React.ReactElement<any>)
+ | React.ReactNode;
+}
+interface OrderVariantSearchProviderState {
+ query: string;
+}
+
+export class OrderVariantSearchProvider extends React.Component<
+ OrderVariantSearchProviderProps,
+ OrderVariantSearchProviderState
+> {
+ state: OrderVariantSearchProviderState = { query: "" };
+
+ search = (query: string) => this.setState({ query });
+
+ render() {
+ const { children } = this.props;
+ if (typeof children === "function") {
+ return (
+ <TypedOrderVariantSearch
+ variables={{ search: this.state.query }}
+ skip={!this.state.query}
+ >
+ {searchOpts =>
+ children({ variants: { search: this.search, searchOpts } })
+ }
+ </TypedOrderVariantSearch>
+ );
+ }
+ return this.props.children;
+ }
+}
diff --git a/saleor/static/dashboard-next/orders/queries.ts b/saleor/static/dashboard-next/orders/queries.ts
index 7cf0dbcb83..7de9fbf76b 100644
--- a/saleor/static/dashboard-next/orders/queries.ts
+++ b/saleor/static/dashboard-next/orders/queries.ts
@@ -4,6 +4,10 @@ import { TypedQuery } from "../queries";
import { OrderDetails, OrderDetailsVariables } from "./types/OrderDetails";
import { OrderList, OrderListVariables } from "./types/OrderList";
import { OrderShippingMethods } from "./types/OrderShippingMethods";
+import {
+ OrderVariantSearch,
+ OrderVariantSearchVariables
+} from "./types/OrderVariantSearch";
export const orderListQuery = gql`
query OrderList($first: Int, $after: String, $last: Int, $before: String) {
@@ -200,3 +204,30 @@ export const TypedOrderShippingMethodsQuery = TypedQuery<
OrderShippingMethods,
{}
>(orderShippingMethodsQuery);
+
+export const orderVariantSearchQuery = gql`
+ query OrderVariantSearch($search: String!) {
+ products(query: $search, first: 20) {
+ edges {
+ node {
+ id
+ name
+ variants {
+ edges {
+ node {
+ id
+ name
+ sku
+ stockQuantity
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+`;
+export const TypedOrderVariantSearch = TypedQuery<
+ OrderVariantSearch,
+ OrderVariantSearchVariables
+>(orderVariantSearchQuery);
diff --git a/saleor/static/dashboard-next/orders/types/OrderVariantSearch.ts b/saleor/static/dashboard-next/orders/types/OrderVariantSearch.ts
new file mode 100644
index 0000000000..30feda412c
--- /dev/null
+++ b/saleor/static/dashboard-next/orders/types/OrderVariantSearch.ts
@@ -0,0 +1,49 @@
+/* tslint:disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL query operation: OrderVariantSearch
+// ====================================================
+
+export interface OrderVariantSearch_products_edges_node_variants_edges_node {
+ __typename: "ProductVariant";
+ id: string;
+ name: string;
+ sku: string;
+ stockQuantity: number;
+}
+
+export interface OrderVariantSearch_products_edges_node_variants_edges {
+ __typename: "ProductVariantCountableEdge";
+ node: OrderVariantSearch_products_edges_node_variants_edges_node;
+}
+
+export interface OrderVariantSearch_products_edges_node_variants {
+ __typename: "ProductVariantCountableConnection";
+ edges: OrderVariantSearch_products_edges_node_variants_edges[];
+}
+
+export interface OrderVariantSearch_products_edges_node {
+ __typename: "Product";
+ id: string;
+ name: string;
+ variants: OrderVariantSearch_products_edges_node_variants | null;
+}
+
+export interface OrderVariantSearch_products_edges {
+ __typename: "ProductCountableEdge";
+ node: OrderVariantSearch_products_edges_node;
+}
+
+export interface OrderVariantSearch_products {
+ __typename: "ProductCountableConnection";
+ edges: OrderVariantSearch_products_edges[];
+}
+
+export interface OrderVariantSearch {
+ products: OrderVariantSearch_products | null;
+}
+
+export interface OrderVariantSearchVariables {
+ search: string;
+}
diff --git a/saleor/static/dashboard-next/orders/views/OrderDetails.tsx b/saleor/static/dashboard-next/orders/views/OrderDetails.tsx
index 0606a729c7..4811ecd981 100644
--- a/saleor/static/dashboard-next/orders/views/OrderDetails.tsx
+++ b/saleor/static/dashboard-next/orders/views/OrderDetails.tsx
@@ -15,6 +15,7 @@ import { Ø } from "../../misc";
import { productUrl } from "../../products";
import OrderDetailsPage from "../components/OrderDetailsPage";
import OrderOperations from "../containers/OrderOperations";
+import { OrderVariantSearchProvider } from "../containers/OrderVariantSearch";
import {
TypedOrderCancelMutation,
TypedOrderReleaseMutation
@@ -49,127 +50,165 @@ export const OrderDetails: React.StatelessComponent<OrderDetailsProps> = ({
<TypedOrderReleaseMutation variables={{ id }}>
{releasePayment => (
<TypedOrderShippingMethodsQuery>
- {({ data }) => {
- const handlePaymentCapture = (
- data: OrderCaptureMutation
- ) => {
- if (
- !Ø(() => data.orderCapture.errors.length)
- ) {
- pushMessage({
- text: i18n.t(
- "Payment succesfully captured",
- { context: "notification" }
- )
- });
- }
- };
- const handlePaymentRefund = (
- data: OrderRefundMutation
- ) => {
- if (
- !Ø(() => data.orderRefund.errors.length)
- ) {
- pushMessage({
- text: i18n.t(
- "Payment succesfully refunded",
- { context: "notification" }
- )
- });
- }
- };
- const handleFulfillmentCreate = (
- data: OrderCreateFulfillmentMutation
- ) => {
- if (
- !Ø(
- () => data.fulfillmentCreate.errors.length
- )
- ) {
- pushMessage({
- text: i18n.t(
- "Items succesfully fulfilled",
- { context: "notification" }
- )
- });
- }
- };
- return (
- <OrderOperations
- order={id}
- onError={undefined}
- onFulfillmentCreate={
- handleFulfillmentCreate
- }
- onPaymentCapture={handlePaymentCapture}
- onPaymentRefund={handlePaymentRefund}
- >
- {({
- orderCreateFulfillment,
- orderPaymentCapture,
- orderPaymentRefund
- }) => (
- <OrderDetailsPage
- onBack={() => navigate(orderListUrl)}
- order={order}
- shippingMethods={Ø(() =>
- ([] as Array<{
- id: string;
- name: string;
- }>).concat(
- ...data.shippingZones.edges.map(
- edge =>
- edge.node.shippingMethods.edges.map(
- edge => edge.node
- )
- )
+ {({ data: shippingMethodsData }) => (
+ <OrderVariantSearchProvider>
+ {({ variants: { search, searchOpts } }) => {
+ const handlePaymentCapture = (
+ data: OrderCaptureMutation
+ ) => {
+ if (
+ !Ø(
+ () => data.orderCapture.errors.length
+ )
+ ) {
+ pushMessage({
+ text: i18n.t(
+ "Payment succesfully captured",
+ { context: "notification" }
)
- )}
- user={user}
- onOrderCancel={cancelOrder}
- onOrderFulfill={variables =>
- orderCreateFulfillment.mutate({
- input: {
- ...variables,
- lines: Ø(
- () => order.lines.edges,
- []
- )
+ });
+ }
+ };
+ const handlePaymentRefund = (
+ data: OrderRefundMutation
+ ) => {
+ if (
+ !Ø(() => data.orderRefund.errors.length)
+ ) {
+ pushMessage({
+ text: i18n.t(
+ "Payment succesfully refunded",
+ { context: "notification" }
+ )
+ });
+ }
+ };
+ const handleFulfillmentCreate = (
+ data: OrderCreateFulfillmentMutation
+ ) => {
+ if (
+ !Ø(
+ () =>
+ data.fulfillmentCreate.errors.length
+ )
+ ) {
+ pushMessage({
+ text: i18n.t(
+ "Items succesfully fulfilled",
+ { context: "notification" }
+ )
+ });
+ }
+ };
+ return (
+ <OrderOperations
+ order={id}
+ onError={undefined}
+ onFulfillmentCreate={
+ handleFulfillmentCreate
+ }
+ onPaymentCapture={handlePaymentCapture}
+ onPaymentRefund={handlePaymentRefund}
+ >
+ {({
+ orderCreateFulfillment,
+ orderPaymentCapture,
+ orderPaymentRefund
+ }) => (
+ <OrderDetailsPage
+ fetchVariants={search}
+ variants={Ø(() =>
+ searchOpts.data.products.edges
.map(edge => edge.node)
- .filter(
- line =>
- line.quantityFulfilled <
- line.quantity
+ .map(product => ({
+ ...product,
+ variants: product.variants.edges.map(
+ edge => edge.node
+ )
+ }))
+ .map(product =>
+ product.variants.map(
+ variant => ({
+ ...variant,
+ name: `${product.name}(${
+ variant.name
+ })`
+ })
+ )
)
- .map((line, lineIndex) => ({
- orderLineId: line.id,
- quantity:
- variables.lines[lineIndex]
- })),
- order: order.id
+ .reduce(
+ (prev, curr) =>
+ prev.concat(curr),
+ []
+ )
+ )}
+ onBack={() =>
+ navigate(orderListUrl)
}
- })
- }
- onPaymentCapture={variables =>
- orderPaymentCapture.mutate({
- ...variables,
- id
- })
- }
- onPaymentRelease={releasePayment}
- onPaymentRefund={variables =>
- orderPaymentRefund.mutate({
- ...variables,
- id
- })
- }
- onProductClick={id => () =>
- navigate(productUrl(id))}
- />
- )}
- </OrderOperations>
- );
- }}
+ order={order}
+ shippingMethods={Ø(() =>
+ ([] 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: Ø(
+ () => 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>