Skip to content

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

Add customer create view

dominik-zeglen9ec9ae170831
diff --git a/saleor/static/dashboard-next/customers/components/CustomerCreateAddress/CustomerCreateAddress.tsx b/saleor/static/dashboard-next/customers/components/CustomerCreateAddress/CustomerCreateAddress.tsx
index cd7fd2454f..258e9dc3af 100644
--- a/saleor/static/dashboard-next/customers/components/CustomerCreateAddress/CustomerCreateAddress.tsx
+++ b/saleor/static/dashboard-next/customers/components/CustomerCreateAddress/CustomerCreateAddress.tsx
@@ -8,8 +8,10 @@ import CardTitle from "../../../components/CardTitle";
import { FormSpacer } from "../../../components/FormSpacer";
import i18n from "../../../i18n";
import { AddressTypeInput } from "../../types";
+import { CustomerCreateData_shop_countries } from "../../types/CustomerCreateData";
export interface CustomerCreateAddressProps {
+ countries: CustomerCreateData_shop_countries[];
data: AddressTypeInput;
disabled: boolean;
errors: { [T in keyof AddressTypeInput]?: string };
@@ -18,13 +20,17 @@ export interface CustomerCreateAddressProps {
const CustomerCreateAddress: React.StatelessComponent<
CustomerCreateAddressProps
-> = ({ data, disabled, errors, onChange }) => (
+> = ({ 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}
diff --git a/saleor/static/dashboard-next/customers/components/CustomerCreateNote/CustomerCreateNote.tsx b/saleor/static/dashboard-next/customers/components/CustomerCreateNote/CustomerCreateNote.tsx
index 69b8a1c673..cab3691547 100644
--- a/saleor/static/dashboard-next/customers/components/CustomerCreateNote/CustomerCreateNote.tsx
+++ b/saleor/static/dashboard-next/customers/components/CustomerCreateNote/CustomerCreateNote.tsx
@@ -36,7 +36,7 @@ const CustomerCreateNote: React.StatelessComponent<CustomerCreateNoteProps> = ({
fullWidth
multiline
name="note"
- label={i18n.t("E-mail")}
+ label={i18n.t("Note")}
value={data.note}
onChange={onChange}
/>
diff --git a/saleor/static/dashboard-next/customers/components/CustomerCreatePage/CustomerCreatePage.tsx b/saleor/static/dashboard-next/customers/components/CustomerCreatePage/CustomerCreatePage.tsx
index 079db334eb..2052499ba4 100644
--- a/saleor/static/dashboard-next/customers/components/CustomerCreatePage/CustomerCreatePage.tsx
+++ b/saleor/static/dashboard-next/customers/components/CustomerCreatePage/CustomerCreatePage.tsx
@@ -1,6 +1,7 @@
import { withStyles } from "@material-ui/core/styles";
import * as React from "react";
+import { UserError } from "../../..";
import { CardSpacer } from "../../../components/CardSpacer";
import Container from "../../../components/Container";
import Form from "../../../components/Form";
@@ -8,6 +9,7 @@ import PageHeader from "../../../components/PageHeader";
import SaveButtonBar from "../../../components/SaveButtonBar";
import i18n from "../../../i18n";
import { AddressTypeInput } from "../../types";
+import { CustomerCreateData_shop_countries } from "../../types/CustomerCreateData";
import CustomerCreateAddress from "../CustomerCreateAddress/CustomerCreateAddress";
import CustomerCreateDetails from "../CustomerCreateDetails";
import CustomerCreateNote from "../CustomerCreateNote/CustomerCreateNote";
@@ -18,8 +20,11 @@ export interface CustomerCreatePageFormData extends AddressTypeInput {
}
export interface CustomerCreatePageProps {
+ countries: CustomerCreateData_shop_countries[];
disabled: boolean;
+ errors: UserError[];
onBack: () => void;
+ onSubmit: (data: CustomerCreatePageFormData) => void;
}
const initialForm: CustomerCreatePageFormData = {
@@ -46,8 +51,8 @@ const decorate = withStyles(theme => ({
}
}));
const CustomerCreatePage = decorate<CustomerCreatePageProps>(
- ({ classes, disabled, onBack }) => (
- <Form initial={initialForm}>
+ ({ classes, countries, disabled, errors, onBack, onSubmit }) => (
+ <Form initial={initialForm} onSubmit={onSubmit} errors={errors}>
{({ change, data, errors: formErrors, hasChanged, submit }) => (
<Container width="md">
<PageHeader title={i18n.t("Add customer")} onBack={onBack} />
@@ -61,6 +66,7 @@ const CustomerCreatePage = decorate<CustomerCreatePageProps>(
/>
<CardSpacer />
<CustomerCreateAddress
+ countries={countries}
data={data}
disabled={disabled}
errors={formErrors}
diff --git a/saleor/static/dashboard-next/customers/index.tsx b/saleor/static/dashboard-next/customers/index.tsx
index 3a5dc5619a..d0cd1db0a5 100644
--- a/saleor/static/dashboard-next/customers/index.tsx
+++ b/saleor/static/dashboard-next/customers/index.tsx
@@ -2,8 +2,8 @@ import { parse as parseQs } from "qs";
import * as React from "react";
import { Route, RouteComponentProps, Switch } from "react-router-dom";
-import { customerListUrl, customerUrl } from "./urls";
-// import CustomerCreateView from "./views/CustomerCreate";
+import { customerAddUrl, customerListUrl, customerUrl } from "./urls";
+import CustomerCreateView from "./views/CustomerCreate";
import CustomerDetailsViewComponent from "./views/CustomerDetails";
import CustomerListViewComponent from "./views/CustomerList";
@@ -30,7 +30,7 @@ const CustomerDetailsView: React.StatelessComponent<
export const CustomerSection: React.StatelessComponent<{}> = () => (
<Switch>
<Route exact path={customerListUrl} component={CustomerListView} />
- {/* <Route exact path={customerUrl("add")} component={CustomerDetailsView} /> */}
+ <Route exact path={customerAddUrl} component={CustomerCreateView} />
<Route path={customerUrl(":id")} component={CustomerDetailsView} />
</Switch>
);
diff --git a/saleor/static/dashboard-next/customers/mutations.ts b/saleor/static/dashboard-next/customers/mutations.ts
index 846c3db4fd..d8904c2dfa 100644
--- a/saleor/static/dashboard-next/customers/mutations.ts
+++ b/saleor/static/dashboard-next/customers/mutations.ts
@@ -2,6 +2,10 @@ import gql from "graphql-tag";
import { TypedMutation } from "../mutations";
import { customerDetailsFragment } from "./queries";
+import {
+ CreateCustomer,
+ CreateCustomerVariables
+} from "./types/CreateCustomer";
import {
UpdateCustomer,
UpdateCustomerVariables
@@ -25,3 +29,21 @@ export const TypedUpdateCustomerMutation = TypedMutation<
UpdateCustomer,
UpdateCustomerVariables
>(updateCustomer);
+
+const createCustomer = gql`
+ mutation CreateCustomer($input: UserCreateInput!) {
+ customerCreate(input: $input) {
+ errors {
+ field
+ message
+ }
+ user {
+ id
+ }
+ }
+ }
+`;
+export const TypedCreateCustomerMutation = TypedMutation<
+ CreateCustomer,
+ CreateCustomerVariables
+>(createCustomer);
diff --git a/saleor/static/dashboard-next/customers/queries.ts b/saleor/static/dashboard-next/customers/queries.ts
index d7f6b28eff..5d90b2a48e 100644
--- a/saleor/static/dashboard-next/customers/queries.ts
+++ b/saleor/static/dashboard-next/customers/queries.ts
@@ -2,6 +2,7 @@ import gql from "graphql-tag";
import { fragmentAddress } from "../orders/queries";
import { TypedQuery } from "../queries";
+import { CustomerCreateData } from "./types/CustomerCreateData";
import {
CustomerDetails,
CustomerDetailsVariables
@@ -100,3 +101,17 @@ export const TypedCustomerDetailsQuery = TypedQuery<
CustomerDetails,
CustomerDetailsVariables
>(customerDetails);
+
+const customerCreateData = gql`
+ query CustomerCreateData {
+ shop {
+ countries {
+ code
+ country
+ }
+ }
+ }
+`;
+export const TypedCustomerCreateDataQuery = TypedQuery<CustomerCreateData, {}>(
+ customerCreateData
+);
diff --git a/saleor/static/dashboard-next/customers/types/CreateCustomer.ts b/saleor/static/dashboard-next/customers/types/CreateCustomer.ts
new file mode 100644
index 0000000000..a1ce75a6e5
--- /dev/null
+++ b/saleor/static/dashboard-next/customers/types/CreateCustomer.ts
@@ -0,0 +1,33 @@
+/* tslint:disable */
+// This file was automatically generated and should not be edited.
+
+import { UserCreateInput } from "./../../types/globalTypes";
+
+// ====================================================
+// GraphQL mutation operation: CreateCustomer
+// ====================================================
+
+export interface CreateCustomer_customerCreate_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface CreateCustomer_customerCreate_user {
+ __typename: "User";
+ id: string;
+}
+
+export interface CreateCustomer_customerCreate {
+ __typename: "CustomerCreate";
+ errors: (CreateCustomer_customerCreate_errors | null)[] | null;
+ user: CreateCustomer_customerCreate_user | null;
+}
+
+export interface CreateCustomer {
+ customerCreate: CreateCustomer_customerCreate | null;
+}
+
+export interface CreateCustomerVariables {
+ input: UserCreateInput;
+}
diff --git a/saleor/static/dashboard-next/customers/types/CustomerCreateData.ts b/saleor/static/dashboard-next/customers/types/CustomerCreateData.ts
new file mode 100644
index 0000000000..ef61764e73
--- /dev/null
+++ b/saleor/static/dashboard-next/customers/types/CustomerCreateData.ts
@@ -0,0 +1,21 @@
+/* tslint:disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL query operation: CustomerCreateData
+// ====================================================
+
+export interface CustomerCreateData_shop_countries {
+ __typename: "CountryDisplay";
+ code: string;
+ country: string;
+}
+
+export interface CustomerCreateData_shop {
+ __typename: "Shop";
+ countries: (CustomerCreateData_shop_countries | null)[];
+}
+
+export interface CustomerCreateData {
+ shop: CustomerCreateData_shop | null;
+}
diff --git a/saleor/static/dashboard-next/customers/views/CustomerCreate.tsx b/saleor/static/dashboard-next/customers/views/CustomerCreate.tsx
index a798f7473b..5d7872b432 100644
--- a/saleor/static/dashboard-next/customers/views/CustomerCreate.tsx
+++ b/saleor/static/dashboard-next/customers/views/CustomerCreate.tsx
@@ -1,5 +1,83 @@
-import * as React from 'react'
+import * as React from "react";
-import Navigator from '../../components/Navigator'
-import Messages from '../../components/messages'
-import
\ No newline at end of file
+import Messages from "../../components/messages";
+import Navigator from "../../components/Navigator";
+import i18n from "../../i18n";
+import { maybe } from "../../misc";
+import CustomerCreatePage from "../components/CustomerCreatePage";
+import { TypedCreateCustomerMutation } from "../mutations";
+import { TypedCustomerCreateDataQuery } from "../queries";
+import { CreateCustomer } from "../types/CreateCustomer";
+import { customerListUrl, customerUrl } from "../urls";
+
+export const CustomerCreate: React.StatelessComponent<{}> = () => (
+ <Navigator>
+ {navigate => (
+ <Messages>
+ {pushMessage => {
+ const handleCreateCustomerSuccess = (data: CreateCustomer) => {
+ if (
+ data.customerCreate.errors === null ||
+ data.customerCreate.errors.length === 0
+ ) {
+ pushMessage({
+ text: i18n.t("Customer created", {
+ context: "notification"
+ })
+ });
+ navigate(customerUrl(data.customerCreate.user.id));
+ }
+ };
+ return (
+ <TypedCustomerCreateDataQuery>
+ {({ data, loading }) => (
+ <TypedCreateCustomerMutation
+ onCompleted={handleCreateCustomerSuccess}
+ >
+ {(createCustomer, createCustomerOpts) => (
+ <CustomerCreatePage
+ countries={maybe(() => data.shop.countries, [])}
+ disabled={loading || createCustomerOpts.loading}
+ errors={maybe(
+ () => createCustomerOpts.data.customerCreate.errors,
+ []
+ )}
+ onBack={() => navigate(customerListUrl)}
+ onSubmit={formData => {
+ const address = {
+ city: formData.city,
+ cityArea: formData.cityArea,
+ companyName: formData.companyName,
+ country: formData.country,
+ countryArea: formData.countryArea,
+ firstName: formData.firstName,
+ lastName: formData.lastName,
+ phone: formData.phone,
+ postalCode: formData.postalCode,
+ streetAddress1: formData.streetAddress1,
+ streetAddress2: formData.streetAddress2
+ };
+ createCustomer({
+ variables: {
+ input: {
+ defaultBillingAddress: address,
+ defaultShippingAddress: address,
+ email: formData.email,
+ note: formData.note,
+ sendPasswordEmail: true
+ }
+ }
+ });
+ }}
+ />
+ )}
+ </TypedCreateCustomerMutation>
+ )}
+ </TypedCustomerCreateDataQuery>
+ );
+ }}
+ </Messages>
+ )}
+ </Navigator>
+);
+export default CustomerCreate;
diff --git a/saleor/static/dashboard-next/storybook/stories/customers/CustomerCreatePage.tsx b/saleor/static/dashboard-next/storybook/stories/customers/CustomerCreatePage.tsx
index ee6379561e..47bcccf719 100644
--- a/saleor/static/dashboard-next/storybook/stories/customers/CustomerCreatePage.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/customers/CustomerCreatePage.tsx
@@ -7,6 +7,10 @@ import CustomerCreatePage, {
import Decorator from "../../Decorator";
const props: CustomerCreatePageProps = {
+ countries: [
+ { __typename: "CountryDisplay", code: "UK", country: "United Kingdom" },
+ { __typename: "CountryDisplay", code: "PL", country: "Poland" }
+ ],
disabled: false,
onBack: () => undefined
};
diff --git a/saleor/static/dashboard-next/types/globalTypes.ts b/saleor/static/dashboard-next/types/globalTypes.ts
index f89212d4a8..f7ba5a4b01 100644
--- a/saleor/static/dashboard-next/types/globalTypes.ts
+++ b/saleor/static/dashboard-next/types/globalTypes.ts
@@ -281,6 +281,14 @@ export interface StaffInput {
permissions?: (string | null)[] | null;
}
+export interface UserCreateInput {
+ email?: string | null;
+ note?: string | null;
+ defaultBillingAddress?: AddressInput | null;
+ defaultShippingAddress?: AddressInput | null;
+ sendPasswordEmail?: boolean | null;
+}
+
//==============================================================
// END Enums and Input Objects
//==============================================================