Skip to content

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

Use autocomplete field in country choice

dominik-zeglencb4f6c883416
diff --git a/saleor/static/dashboard-next/components/AddressEdit/AddressEdit.tsx b/saleor/static/dashboard-next/components/AddressEdit/AddressEdit.tsx
index 464142a46e..7435909db5 100644
--- a/saleor/static/dashboard-next/components/AddressEdit/AddressEdit.tsx
+++ b/saleor/static/dashboard-next/components/AddressEdit/AddressEdit.tsx
@@ -11,7 +11,7 @@ import { AddressTypeInput } from "../../customers/types";
import i18n from "../../i18n";
import { maybe } from "../../misc";
import FormSpacer from "../FormSpacer";
-import SingleSelectField from "../SingleSelectField";
+import SingleAutocompleteSelectField from "../SingleAutocompleteSelectField";
const styles = (theme: Theme) =>
createStyles({
@@ -149,10 +149,10 @@ const AddressEdit = withStyles(styles, { name: "AddressEdit" })(
<FormSpacer />
<div className={classes.root}>
<div>
- <SingleSelectField
+ <SingleAutocompleteSelectField
disabled={disabled}
error={!!errors.country}
- hint={errors.country}
+ helperText={errors.country}
label={i18n.t("Country")}
name="country"
onChange={onChange}
diff --git a/saleor/static/dashboard-next/components/Debounce.tsx b/saleor/static/dashboard-next/components/Debounce.tsx
index 13c0460b29..533c52aba1 100644
--- a/saleor/static/dashboard-next/components/Debounce.tsx
+++ b/saleor/static/dashboard-next/components/Debounce.tsx
@@ -17,6 +17,10 @@ export class Debounce<T> extends React.Component<DebounceProps<T>> {
this.timer = setTimeout(() => debounceFn(...args), time || 200);
};
+ componentWillUnmount() {
+ clearTimeout(this.timer);
+ }
+
render() {
return this.props.children(this.handleDebounce);
}
diff --git a/saleor/static/dashboard-next/components/SingleAutocompleteSelectField/SingleAutocompleteSelectField.tsx b/saleor/static/dashboard-next/components/SingleAutocompleteSelectField/SingleAutocompleteSelectField.tsx
index 95f1f9a26b..da021685dc 100644
--- a/saleor/static/dashboard-next/components/SingleAutocompleteSelectField/SingleAutocompleteSelectField.tsx
+++ b/saleor/static/dashboard-next/components/SingleAutocompleteSelectField/SingleAutocompleteSelectField.tsx
@@ -32,6 +32,7 @@ const styles = (theme: Theme) =>
});
interface SingleAutocompleteSelectFieldProps extends WithStyles<typeof styles> {
+ error?: boolean;
name: string;
choices: Array<{
label: string;
@@ -70,6 +71,7 @@ const SingleAutocompleteSelectFieldComponent = withStyles(styles, {
classes,
custom,
disabled,
+ error,
helperText,
label,
loading,
@@ -116,6 +118,7 @@ const SingleAutocompleteSelectFieldComponent = withStyles(styles, {
onClick={disabled ? undefined : toggleMenu}
/>
),
+ error,
id: undefined
}}
disabled={disabled}
@@ -177,21 +180,22 @@ export class SingleAutocompleteSelectField extends React.Component<
> {
state = { choices: this.props.choices };
- handleInputChange = (value: string) => {
- this.setState({
- choices: this.props.choices.sort((a, b) => {
- const ratingA = compareTwoStrings(value || "", a.label);
- const ratingB = compareTwoStrings(value || "", b.label);
- if (ratingA > ratingB) {
- return -1;
- }
- if (ratingA < ratingB) {
- return 1;
- }
- return 0;
- })
- });
- };
+ handleInputChange = (value: string) =>
+ this.setState((_, props) => ({
+ choices: props.choices
+ .sort((a, b) => {
+ const ratingA = compareTwoStrings(value || "", a.label);
+ const ratingB = compareTwoStrings(value || "", b.label);
+ if (ratingA > ratingB) {
+ return -1;
+ }
+ if (ratingA < ratingB) {
+ return 1;
+ }
+ return 0;
+ })
+ .slice(0, 5)
+ }));
render() {
if (!!this.props.fetchChoices) {
diff --git a/saleor/static/dashboard-next/customers/components/CustomerCreateAddress/CustomerCreateAddress.tsx b/saleor/static/dashboard-next/customers/components/CustomerCreateAddress/CustomerCreateAddress.tsx
index 258e9dc3af..538dbdf181 100644
--- a/saleor/static/dashboard-next/customers/components/CustomerCreateAddress/CustomerCreateAddress.tsx
+++ b/saleor/static/dashboard-next/customers/components/CustomerCreateAddress/CustomerCreateAddress.tsx
@@ -1,5 +1,6 @@
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
+import { createStyles, WithStyles, withStyles } from "@material-ui/core/styles";
import Typography from "@material-ui/core/Typography";
import * as React from "react";
@@ -10,7 +11,13 @@ import i18n from "../../../i18n";
import { AddressTypeInput } from "../../types";
import { CustomerCreateData_shop_countries } from "../../types/CustomerCreateData";
-export interface CustomerCreateAddressProps {
+const styles = createStyles({
+ overflow: {
+ overflow: "visible"
+ }
+});
+
+export interface CustomerCreateAddressProps extends WithStyles<typeof styles> {
countries: CustomerCreateData_shop_countries[];
data: AddressTypeInput;
disabled: boolean;
@@ -18,26 +25,37 @@ export interface CustomerCreateAddressProps {
onChange(event: React.ChangeEvent<any>);
}
-const CustomerCreateAddress: React.StatelessComponent<
- CustomerCreateAddressProps
-> = ({ countries, data, disabled, errors, onChange }) => (
- <Card>
- <CardTitle title={i18n.t("Primary address")} />
- <CardContent>
- <Typography>{i18n.t("The primary address of this customer.")}</Typography>
- <FormSpacer />
- <AddressEdit
- countries={countries.map(country => ({
- code: country.code,
- label: country.country
- }))}
- data={data}
- disabled={disabled}
- errors={errors}
- onChange={onChange}
- />
- </CardContent>
- </Card>
+const CustomerCreateAddress = withStyles(styles, {
+ name: "CustomerCreateAddress"
+})(
+ ({
+ classes,
+ countries,
+ data,
+ disabled,
+ errors,
+ onChange
+ }: CustomerCreateAddressProps) => (
+ <Card className={classes.overflow}>
+ <CardTitle title={i18n.t("Primary address")} />
+ <CardContent className={classes.overflow}>
+ <Typography>
+ {i18n.t("The primary address of this customer.")}
+ </Typography>
+ <FormSpacer />
+ <AddressEdit
+ countries={countries.map(country => ({
+ code: country.code,
+ label: country.country
+ }))}
+ data={data}
+ disabled={disabled}
+ errors={errors}
+ onChange={onChange}
+ />
+ </CardContent>
+ </Card>
+ )
);
CustomerCreateAddress.displayName = "CustomerCreateAddress";
export default CustomerCreateAddress;
diff --git a/saleor/static/dashboard-next/customers/components/CustomerCreatePage/CustomerCreatePage.tsx b/saleor/static/dashboard-next/customers/components/CustomerCreatePage/CustomerCreatePage.tsx
index 9fbd6099bf..cde41f677c 100644
--- a/saleor/static/dashboard-next/customers/components/CustomerCreatePage/CustomerCreatePage.tsx
+++ b/saleor/static/dashboard-next/customers/components/CustomerCreatePage/CustomerCreatePage.tsx
@@ -29,7 +29,10 @@ const initialForm: CustomerCreatePageFormData = {
city: "",
cityArea: "",
companyName: "",
- country: "",
+ country: {
+ label: "",
+ value: ""
+ },
countryArea: "",
email: "",
firstName: "",
diff --git a/saleor/static/dashboard-next/customers/types.ts b/saleor/static/dashboard-next/customers/types.ts
index fa3a76f4ed..1eee8acacf 100644
--- a/saleor/static/dashboard-next/customers/types.ts
+++ b/saleor/static/dashboard-next/customers/types.ts
@@ -2,7 +2,10 @@ export interface AddressTypeInput {
city: string;
cityArea?: string;
companyName?: string;
- country: string;
+ country: {
+ label: string;
+ value: string;
+ };
countryArea?: string;
firstName: string;
lastName: string;
diff --git a/saleor/static/dashboard-next/customers/views/CustomerCreate.tsx b/saleor/static/dashboard-next/customers/views/CustomerCreate.tsx
index 7850b2c603..40885b7c9c 100644
--- a/saleor/static/dashboard-next/customers/views/CustomerCreate.tsx
+++ b/saleor/static/dashboard-next/customers/views/CustomerCreate.tsx
@@ -66,8 +66,14 @@ export const CustomerCreate: React.StatelessComponent<{}> = () => (
createCustomer({
variables: {
input: {
- defaultBillingAddress: address,
- defaultShippingAddress: address,
+ defaultBillingAddress: {
+ ...address,
+ country: address.country.value
+ },
+ defaultShippingAddress: {
+ ...address,
+ country: address.country.value
+ },
email: formData.email,
note: formData.note,
sendPasswordEmail: true
diff --git a/saleor/static/dashboard-next/misc.ts b/saleor/static/dashboard-next/misc.ts
index 5cf1cb7a8c..9ffd62f497 100644
--- a/saleor/static/dashboard-next/misc.ts
+++ b/saleor/static/dashboard-next/misc.ts
@@ -72,7 +72,10 @@ export const transformAddressToForm = (data: AddressType) => ({
city: maybe(() => data.city, ""),
cityArea: maybe(() => data.cityArea, ""),
companyName: maybe(() => data.companyName, ""),
- country: maybe(() => data.country.code, ""),
+ country: {
+ label: maybe(() => data.country.country, ""),
+ value: maybe(() => data.country.code, "")
+ },
countryArea: maybe(() => data.countryArea, ""),
firstName: maybe(() => data.firstName, ""),
lastName: maybe(() => data.lastName, ""),
diff --git a/saleor/static/dashboard-next/orders/components/OrderAddressEditDialog/OrderAddressEditDialog.tsx b/saleor/static/dashboard-next/orders/components/OrderAddressEditDialog/OrderAddressEditDialog.tsx
index a08d9b4134..9df505084c 100644
--- a/saleor/static/dashboard-next/orders/components/OrderAddressEditDialog/OrderAddressEditDialog.tsx
+++ b/saleor/static/dashboard-next/orders/components/OrderAddressEditDialog/OrderAddressEditDialog.tsx
@@ -3,6 +3,7 @@ import Dialog from "@material-ui/core/Dialog";
import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent";
import DialogTitle from "@material-ui/core/DialogTitle";
+import { createStyles, withStyles, WithStyles } from "@material-ui/core/styles";
import * as React from "react";
import AddressEdit from "../../../components/AddressEdit/AddressEdit";
@@ -14,7 +15,13 @@ import { AddressTypeInput } from "../../../customers/types";
import i18n from "../../../i18n";
import { UserError } from "../../../types";
-interface OrderAddressEditDialogProps {
+const styles = createStyles({
+ overflow: {
+ overflowY: "visible"
+ }
+});
+
+interface OrderAddressEditDialogProps extends WithStyles<typeof styles> {
confirmButtonState: ConfirmButtonTransitionState;
address: AddressTypeInput;
open: boolean;
@@ -28,53 +35,56 @@ interface OrderAddressEditDialogProps {
onConfirm(data: AddressTypeInput);
}
-const OrderAddressEditDialog: React.StatelessComponent<
- OrderAddressEditDialogProps
-> = ({
- address,
- confirmButtonState,
- open,
- errors,
- variant,
- countries,
- onClose,
- onConfirm
-}) => (
- <Dialog open={open}>
- <Form initial={address} errors={errors} onSubmit={onConfirm}>
- {({ change, data, errors, submit }) => (
- <>
- <DialogTitle>
- {variant === "billing"
- ? i18n.t("Edit billing address", { context: "title" })
- : i18n.t("Edit shipping address", { context: "title" })}
- </DialogTitle>
- <DialogContent>
- <AddressEdit
- countries={countries}
- data={data}
- errors={errors}
- onChange={change}
- />
- </DialogContent>
- <DialogActions>
- <Button onClick={onClose}>
- {i18n.t("Cancel", { context: "button" })}
- </Button>
- <ConfirmButton
- transitionState={confirmButtonState}
- color="primary"
- variant="contained"
- onClick={submit}
- type="submit"
- >
- {i18n.t("Confirm", { context: "button" })}
- </ConfirmButton>
- </DialogActions>
- </>
- )}
- </Form>
- </Dialog>
+const OrderAddressEditDialog = withStyles(styles, {
+ name: "OrderAddressEditDialog"
+})(
+ ({
+ address,
+ classes,
+ confirmButtonState,
+ open,
+ errors,
+ variant,
+ countries,
+ onClose,
+ onConfirm
+ }: OrderAddressEditDialogProps) => (
+ <Dialog open={open} classes={{ paper: classes.overflow }}>
+ <Form initial={address} errors={errors} onSubmit={onConfirm}>
+ {({ change, data, errors, submit }) => (
+ <>
+ <DialogTitle>
+ {variant === "billing"
+ ? i18n.t("Edit billing address", { context: "title" })
+ : i18n.t("Edit shipping address", { context: "title" })}
+ </DialogTitle>
+ <DialogContent className={classes.overflow}>
+ <AddressEdit
+ countries={countries}
+ data={data}
+ errors={errors}
+ onChange={change}
+ />
+ </DialogContent>
+ <DialogActions>
+ <Button onClick={onClose}>
+ {i18n.t("Cancel", { context: "button" })}
+ </Button>
+ <ConfirmButton
+ transitionState={confirmButtonState}
+ color="primary"
+ variant="contained"
+ onClick={submit}
+ type="submit"
+ >
+ {i18n.t("Confirm", { context: "button" })}
+ </ConfirmButton>
+ </DialogActions>
+ </>
+ )}
+ </Form>
+ </Dialog>
+ )
);
OrderAddressEditDialog.displayName = "OrderAddressEditDialog";
export default OrderAddressEditDialog;
diff --git a/saleor/static/dashboard-next/orders/views/OrderDetails/index.tsx b/saleor/static/dashboard-next/orders/views/OrderDetails/index.tsx
index e78e356731..b4daad9d49 100644
--- a/saleor/static/dashboard-next/orders/views/OrderDetails/index.tsx
+++ b/saleor/static/dashboard-next/orders/views/OrderDetails/index.tsx
@@ -796,7 +796,10 @@ export const OrderDetails: React.StatelessComponent<OrderDetailsProps> = ({
orderUpdate.mutate({
id,
input: {
- shippingAddress: variables
+ shippingAddress: {
+ ...variables,
+ country: variables.country.value
+ }
}
})
}
@@ -839,7 +842,10 @@ export const OrderDetails: React.StatelessComponent<OrderDetailsProps> = ({
orderUpdate.mutate({
id,
input: {
- billingAddress: variables
+ billingAddress: {
+ ...variables,
+ country: variables.country.value
+ }
}
})
}