Skip to content

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

Add address create, update and remove dialogs

dominik-zeglend08afa31c294
diff --git a/saleor/static/dashboard-next/customers/components/CustomerAddressDialog/CustomerAddressDialog.tsx b/saleor/static/dashboard-next/customers/components/CustomerAddressDialog/CustomerAddressDialog.tsx
index 6e88d70faf..04588ddbb8 100644
--- a/saleor/static/dashboard-next/customers/components/CustomerAddressDialog/CustomerAddressDialog.tsx
+++ b/saleor/static/dashboard-next/customers/components/CustomerAddressDialog/CustomerAddressDialog.tsx
@@ -13,6 +13,7 @@ import ConfirmButton, {
} from "../../../components/ConfirmButton";
import Form from "../../../components/Form";
import i18n from "../../../i18n";
+import { maybe } from "../../../misc";
import { UserError } from "../../../types";
import { AddressTypeInput } from "../../types";
import { CustomerAddresses_user_addresses } from "../../types/CustomerAddresses";
@@ -50,11 +51,20 @@ const CustomerAddressDialog = withStyles(styles, {})(
onConfirm
}: CustomerAddressDialogProps & WithStyles<typeof styles>) => {
const initialForm: AddressTypeInput = {
- ...address,
+ city: maybe(() => address.city, ""),
+ cityArea: maybe(() => address.cityArea, ""),
+ companyName: maybe(() => address.companyName, ""),
country: {
- label: address.country.country,
- value: address.country.code
- }
+ label: maybe(() => address.country.country),
+ value: maybe(() => address.country.code)
+ },
+ countryArea: maybe(() => address.countryArea, ""),
+ firstName: maybe(() => address.firstName, ""),
+ lastName: maybe(() => address.lastName, ""),
+ phone: maybe(() => address.phone, ""),
+ postalCode: maybe(() => address.postalCode, ""),
+ streetAddress1: maybe(() => address.streetAddress1, ""),
+ streetAddress2: maybe(() => address.streetAddress2, "")
};
return (
<Dialog
diff --git a/saleor/static/dashboard-next/customers/components/CustomerAddressListPage/CustomerAddressListPage.tsx b/saleor/static/dashboard-next/customers/components/CustomerAddressListPage/CustomerAddressListPage.tsx
index 85b7c8e87f..fbeec0be13 100644
--- a/saleor/static/dashboard-next/customers/components/CustomerAddressListPage/CustomerAddressListPage.tsx
+++ b/saleor/static/dashboard-next/customers/components/CustomerAddressListPage/CustomerAddressListPage.tsx
@@ -30,10 +30,10 @@ export interface CustomerAddressListPageProps {
const styles = (theme: Theme) =>
createStyles({
root: {
- columnGap: theme.spacing.unit * 3,
+ columnGap: theme.spacing.unit * 3 + "px",
display: "grid",
gridTemplateColumns: "repeat(3, 1fr)",
- rowGap: theme.spacing.unit * 3
+ rowGap: theme.spacing.unit * 3 + "px"
}
});
@@ -91,6 +91,7 @@ const CustomerAddressListPage = withStyles(styles, {
onEdit={() => onEdit(address.id)}
onRemove={() => onRemove(address.id)}
onSetAsDefault={type => onSetAsDefault(address.id, type)}
+ key={maybe(() => address.id, "skeleton")}
/>
)
)}
diff --git a/saleor/static/dashboard-next/customers/mutations.ts b/saleor/static/dashboard-next/customers/mutations.ts
index 0f00d102e5..420afb9617 100644
--- a/saleor/static/dashboard-next/customers/mutations.ts
+++ b/saleor/static/dashboard-next/customers/mutations.ts
@@ -1,15 +1,24 @@
import gql from "graphql-tag";
import { TypedMutation } from "../mutations";
+import { fragmentAddress } from "../orders/queries";
import { customerAddressesFragment, customerDetailsFragment } from "./queries";
import {
CreateCustomer,
CreateCustomerVariables
} from "./types/CreateCustomer";
+import {
+ CreateCustomerAddress,
+ CreateCustomerAddressVariables
+} from "./types/CreateCustomerAddress";
import {
RemoveCustomer,
RemoveCustomerVariables
} from "./types/RemoveCustomer";
+import {
+ RemoveCustomerAddress,
+ RemoveCustomerAddressVariables
+} from "./types/RemoveCustomerAddress";
import {
SetCustomerDefaultAddress,
SetCustomerDefaultAddressVariables
@@ -18,6 +27,10 @@ import {
UpdateCustomer,
UpdateCustomerVariables
} from "./types/UpdateCustomer";
+import {
+ UpdateCustomerAddress,
+ UpdateCustomerAddressVariables
+} from "./types/UpdateCustomerAddress";
const updateCustomer = gql`
${customerDetailsFragment}
@@ -89,3 +102,59 @@ export const TypedSetCustomerDefaultAddressMutation = TypedMutation<
SetCustomerDefaultAddress,
SetCustomerDefaultAddressVariables
>(setCustomerDefaultAddress);
+
+const createCustomerAddress = gql`
+ ${fragmentAddress}
+ mutation CreateCustomerAddress($id: ID!, $input: AddressInput!) {
+ addressCreate(userId: $id, input: $input) {
+ errors {
+ field
+ message
+ }
+ address {
+ ...AddressFragment
+ }
+ user {
+ ...CustomerAddressesFragment
+ }
+ }
+ }
+`;
+export const TypedCreateCustomerAddressMutation = TypedMutation<
+ CreateCustomerAddress,
+ CreateCustomerAddressVariables
+>(createCustomerAddress);
+
+const updateCustomerAddress = gql`
+ ${fragmentAddress}
+ mutation UpdateCustomerAddress($id: ID!, $input: AddressInput!) {
+ addressUpdate(id: $id, input: $input) {
+ errors {
+ field
+ message
+ }
+ address {
+ ...AddressFragment
+ }
+ }
+ }
+`;
+export const TypedUpdateCustomerAddressMutation = TypedMutation<
+ UpdateCustomerAddress,
+ UpdateCustomerAddressVariables
+>(updateCustomerAddress);
+
+const removeCustomerAddress = gql`
+ mutation RemoveCustomerAddress($id: ID!) {
+ addressDelete(id: $id) {
+ errors {
+ field
+ message
+ }
+ }
+ }
+`;
+export const TypedRemoveCustomerAddressMutation = TypedMutation<
+ RemoveCustomerAddress,
+ RemoveCustomerAddressVariables
+>(removeCustomerAddress);
diff --git a/saleor/static/dashboard-next/customers/types/CreateCustomerAddress.ts b/saleor/static/dashboard-next/customers/types/CreateCustomerAddress.ts
new file mode 100644
index 0000000000..f49e42228b
--- /dev/null
+++ b/saleor/static/dashboard-next/customers/types/CreateCustomerAddress.ts
@@ -0,0 +1,96 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+import { AddressInput } from "./../../types/globalTypes";
+
+// ====================================================
+// GraphQL mutation operation: CreateCustomerAddress
+// ====================================================
+
+export interface CreateCustomerAddress_addressCreate_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface CreateCustomerAddress_addressCreate_address_country {
+ __typename: "CountryDisplay";
+ code: string;
+ country: string;
+}
+
+export interface CreateCustomerAddress_addressCreate_address {
+ __typename: "Address";
+ city: string;
+ cityArea: string;
+ companyName: string;
+ country: CreateCustomerAddress_addressCreate_address_country;
+ countryArea: string;
+ firstName: string;
+ id: string;
+ lastName: string;
+ phone: string | null;
+ postalCode: string;
+ streetAddress1: string;
+ streetAddress2: string;
+}
+
+export interface CreateCustomerAddress_addressCreate_user_addresses_country {
+ __typename: "CountryDisplay";
+ code: string;
+ country: string;
+}
+
+export interface CreateCustomerAddress_addressCreate_user_addresses {
+ __typename: "Address";
+ city: string;
+ cityArea: string;
+ companyName: string;
+ country: CreateCustomerAddress_addressCreate_user_addresses_country;
+ countryArea: string;
+ firstName: string;
+ id: string;
+ lastName: string;
+ phone: string | null;
+ postalCode: string;
+ streetAddress1: string;
+ streetAddress2: string;
+}
+
+export interface CreateCustomerAddress_addressCreate_user_defaultBillingAddress {
+ __typename: "Address";
+ id: string;
+}
+
+export interface CreateCustomerAddress_addressCreate_user_defaultShippingAddress {
+ __typename: "Address";
+ id: string;
+}
+
+export interface CreateCustomerAddress_addressCreate_user {
+ __typename: "User";
+ id: string;
+ email: string;
+ firstName: string;
+ lastName: string;
+ addresses: (CreateCustomerAddress_addressCreate_user_addresses | null)[] | null;
+ defaultBillingAddress: CreateCustomerAddress_addressCreate_user_defaultBillingAddress | null;
+ defaultShippingAddress: CreateCustomerAddress_addressCreate_user_defaultShippingAddress | null;
+}
+
+export interface CreateCustomerAddress_addressCreate {
+ __typename: "AddressCreate";
+ errors: CreateCustomerAddress_addressCreate_errors[] | null;
+ address: CreateCustomerAddress_addressCreate_address | null;
+ user: CreateCustomerAddress_addressCreate_user | null;
+}
+
+export interface CreateCustomerAddress {
+ addressCreate: CreateCustomerAddress_addressCreate | null;
+}
+
+export interface CreateCustomerAddressVariables {
+ id: string;
+ input: AddressInput;
+}
diff --git a/saleor/static/dashboard-next/customers/types/RemoveCustomerAddress.ts b/saleor/static/dashboard-next/customers/types/RemoveCustomerAddress.ts
new file mode 100644
index 0000000000..674ac638cc
--- /dev/null
+++ b/saleor/static/dashboard-next/customers/types/RemoveCustomerAddress.ts
@@ -0,0 +1,26 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL mutation operation: RemoveCustomerAddress
+// ====================================================
+
+export interface RemoveCustomerAddress_addressDelete_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface RemoveCustomerAddress_addressDelete {
+ __typename: "AddressDelete";
+ errors: RemoveCustomerAddress_addressDelete_errors[] | null;
+}
+
+export interface RemoveCustomerAddress {
+ addressDelete: RemoveCustomerAddress_addressDelete | null;
+}
+
+export interface RemoveCustomerAddressVariables {
+ id: string;
+}
diff --git a/saleor/static/dashboard-next/customers/types/UpdateCustomerAddress.ts b/saleor/static/dashboard-next/customers/types/UpdateCustomerAddress.ts
new file mode 100644
index 0000000000..4cf3ea5c3a
--- /dev/null
+++ b/saleor/static/dashboard-next/customers/types/UpdateCustomerAddress.ts
@@ -0,0 +1,52 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+import { AddressInput } from "./../../types/globalTypes";
+
+// ====================================================
+// GraphQL mutation operation: UpdateCustomerAddress
+// ====================================================
+
+export interface UpdateCustomerAddress_addressUpdate_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface UpdateCustomerAddress_addressUpdate_address_country {
+ __typename: "CountryDisplay";
+ code: string;
+ country: string;
+}
+
+export interface UpdateCustomerAddress_addressUpdate_address {
+ __typename: "Address";
+ city: string;
+ cityArea: string;
+ companyName: string;
+ country: UpdateCustomerAddress_addressUpdate_address_country;
+ countryArea: string;
+ firstName: string;
+ id: string;
+ lastName: string;
+ phone: string | null;
+ postalCode: string;
+ streetAddress1: string;
+ streetAddress2: string;
+}
+
+export interface UpdateCustomerAddress_addressUpdate {
+ __typename: "AddressUpdate";
+ errors: UpdateCustomerAddress_addressUpdate_errors[] | null;
+ address: UpdateCustomerAddress_addressUpdate_address | null;
+}
+
+export interface UpdateCustomerAddress {
+ addressUpdate: UpdateCustomerAddress_addressUpdate | null;
+}
+
+export interface UpdateCustomerAddressVariables {
+ id: string;
+ input: AddressInput;
+}
diff --git a/saleor/static/dashboard-next/customers/urls.ts b/saleor/static/dashboard-next/customers/urls.ts
index 0cc583db4c..515daacdf9 100644
--- a/saleor/static/dashboard-next/customers/urls.ts
+++ b/saleor/static/dashboard-next/customers/urls.ts
@@ -20,3 +20,29 @@ export const customerAddressesPath = (id: string) =>
urlJoin(customerPath(id), "addresses");
export const customerAddressesUrl = (id: string) =>
customerAddressesPath(encodeURIComponent(id));
+
+export const customerAddressPath = (customerId: string, addressId: string) =>
+ urlJoin(customerAddressesPath(customerId), addressId);
+export const customerAddressUrl = (customerId: string, addressId: string) =>
+ customerAddressPath(
+ encodeURIComponent(customerId),
+ encodeURIComponent(addressId)
+ );
+
+export const customerAddressAddPath = (customerId: string) =>
+ urlJoin(customerAddressesPath(customerId), "add");
+export const customerAddressAddUrl = (customerId: string) =>
+ customerAddressAddPath(encodeURIComponent(customerId));
+
+export const customerAddressRemovePath = (
+ customerId: string,
+ addressId: string
+) => urlJoin(customerAddressPath(customerId, addressId), "remove");
+export const customerAddressRemoveUrl = (
+ customerId: string,
+ addressId: string
+) =>
+ customerAddressRemovePath(
+ encodeURIComponent(customerId),
+ encodeURIComponent(addressId)
+ );
diff --git a/saleor/static/dashboard-next/customers/views/CustomerAddresses.tsx b/saleor/static/dashboard-next/customers/views/CustomerAddresses.tsx
index 36e78c6018..0fa63ae0fe 100644
--- a/saleor/static/dashboard-next/customers/views/CustomerAddresses.tsx
+++ b/saleor/static/dashboard-next/customers/views/CustomerAddresses.tsx
@@ -1,16 +1,37 @@
+import DialogContentText from "@material-ui/core/DialogContentText";
import * as React from "react";
+import { Route, Switch } from "react-router-dom";
-import Messages from "../../components/messages";
-import Navigator from "../../components/Navigator";
+import ActionDialog from "../../components/ActionDialog";
import { WindowTitle } from "../../components/WindowTitle";
import useNavigator from "../../hooks/useNavigator";
import useNotifier from "../../hooks/useNotifier";
+import useShop from "../../hooks/useShop";
import i18n from "../../i18n";
import { maybe } from "../../misc";
+import CustomerAddressDialog from "../components/CustomerAddressDialog";
import CustomerAddressListPage from "../components/CustomerAddressListPage";
-import { TypedSetCustomerDefaultAddressMutation } from "../mutations";
+import {
+ TypedCreateCustomerAddressMutation,
+ TypedRemoveCustomerAddressMutation,
+ TypedSetCustomerDefaultAddressMutation,
+ TypedUpdateCustomerAddressMutation
+} from "../mutations";
import { TypedCustomerAddressesQuery } from "../queries";
-import { customerUrl } from "../urls";
+import { CreateCustomerAddress } from "../types/CreateCustomerAddress";
+import { RemoveCustomerAddress } from "../types/RemoveCustomerAddress";
+import { SetCustomerDefaultAddress } from "../types/SetCustomerDefaultAddress";
+import { UpdateCustomerAddress } from "../types/UpdateCustomerAddress";
+import {
+ customerAddressAddPath,
+ customerAddressAddUrl,
+ customerAddressesUrl,
+ customerAddressPath,
+ customerAddressRemovePath,
+ customerAddressRemoveUrl,
+ customerAddressUrl,
+ customerUrl
+} from "../urls";
interface CustomerAddressesProps {
id: string;
@@ -19,36 +40,217 @@ interface CustomerAddressesProps {
const CustomerAddresses: React.FC<CustomerAddressesProps> = ({ id }) => {
const navigate = useNavigator();
const notify = useNotifier();
+ const shop = useShop();
+
+ const closeModal = () => navigate(customerAddressesUrl(id), true);
+
+ const handleSetAddressAsDefault = (data: SetCustomerDefaultAddress) => {
+ if (data.customerSetDefaultAddress.errors.length === 0) {
+ closeModal();
+ notify({
+ text: i18n.t("Set address as default", {
+ context: "notification"
+ })
+ });
+ }
+ };
+
+ const handleAddressCreate = (data: CreateCustomerAddress) => {
+ if (data.addressCreate.errors.length === 0) {
+ closeModal();
+ }
+ };
+
+ const handleAddressUpdate = (data: UpdateCustomerAddress) => {
+ if (data.addressUpdate.errors.length === 0) {
+ closeModal();
+ notify({
+ text: i18n.t("Updated address", {
+ context: "notification"
+ })
+ });
+ }
+ };
+
+ const handleAddressRemove = (data: RemoveCustomerAddress) => {
+ if (data.addressDelete.errors.length === 0) {
+ closeModal();
+ notify({
+ text: i18n.t("Removed address", {
+ context: "notification"
+ })
+ });
+ }
+ };
return (
- <TypedSetCustomerDefaultAddressMutation>
+ <TypedSetCustomerDefaultAddressMutation
+ onCompleted={handleSetAddressAsDefault}
+ >
{(setCustomerDefaultAddress, setCustomerDefaultAddressOpts) => (
- <TypedCustomerAddressesQuery variables={{ id }}>
- {customerData => {
- return (
- <>
- <WindowTitle
- title={maybe(() => customerData.data.user.firstName)}
- />
- <CustomerAddressListPage
- customer={maybe(() => customerData.data.user)}
- disabled={customerData.loading}
- onAdd={() => undefined}
- onBack={() =>
- navigate(customerUrl(customerData.data.user.id))
- }
- onEdit={() => undefined}
- onRemove={() => undefined}
- onSetAsDefault={(id, type) =>
- setCustomerDefaultAddress({
- variables: { id, type }
- })
- }
- />
- </>
- );
- }}
- </TypedCustomerAddressesQuery>
+ <TypedCreateCustomerAddressMutation onCompleted={handleAddressCreate}>
+ {(createCustomerAddress, createCustomerAddressOpts) => (
+ <TypedUpdateCustomerAddressMutation
+ onCompleted={handleAddressUpdate}
+ >
+ {(updateCustomerAddress, updateCustomerAddressOpts) => (
+ <TypedRemoveCustomerAddressMutation
+ onCompleted={handleAddressRemove}
+ >
+ {(removeCustomerAddress, removeCustomerAddressOpts) => (
+ <TypedCustomerAddressesQuery variables={{ id }}>
+ {customerData => {
+ return (
+ <>
+ <WindowTitle
+ title={maybe(() =>
+ i18n.t("{{ firstName }} {{ lastName }}", {
+ firstName: customerData.data.user.firstName,
+ lastName: customerData.data.user.lastName
+ })
+ )}
+ />
+ <CustomerAddressListPage
+ customer={maybe(() => customerData.data.user)}
+ disabled={customerData.loading}
+ onAdd={() => navigate(customerAddressAddUrl(id))}
+ onBack={() => navigate(customerUrl(id))}
+ onEdit={addressId =>
+ navigate(customerAddressUrl(id, addressId))
+ }
+ onRemove={addressId =>
+ navigate(
+ customerAddressRemoveUrl(id, addressId)
+ )
+ }
+ onSetAsDefault={(addressId, type) =>
+ setCustomerDefaultAddress({
+ variables: { id: addressId, type }
+ })
+ }
+ />
+ <Switch>
+ <Route
+ path={customerAddressAddPath(":customerId")}
+ render={({ match }) => (
+ <CustomerAddressDialog
+ address={undefined}
+ confirmButtonState={"default"}
+ countries={maybe(
+ () =>
+ shop.countries.map(country => ({
+ code: country.code,
+ label: country.country
+ })),
+ []
+ )}
+ errors={maybe(
+ () =>
+ updateCustomerAddressOpts.data
+ .addressUpdate.errors,
+ []
+ )}
+ open={!!match}
+ variant="create"
+ onClose={closeModal}
+ onConfirm={formData =>
+ createCustomerAddress({
+ variables: {
+ input: {
+ ...formData,
+ country: formData.country.value,
+ userId: id
+ }
+ }
+ })
+ }
+ />
+ )}
+ />
+ <Route
+ path={customerAddressRemovePath(
+ ":customerId",
+ ":addressId"
+ )}
+ render={({ match }) => (
+ <ActionDialog
+ open={!!match}
+ variant="delete"
+ title={i18n.t("Remove Address")}
+ confirmButtonState={"default"}
+ onClose={closeModal}
+ onConfirm={() =>
+ removeCustomerAddress({
+ variables: {
+ id: decodeURIComponent(
+ match.params.addressId
+ )
+ }
+ })
+ }
+ >
+ <DialogContentText>
+ {i18n.t(
+ "Are you sure you want to remove this address from users address book?"
+ )}
+ </DialogContentText>
+ </ActionDialog>
+ )}
+ />
+ <Route
+ path={customerAddressPath(
+ ":customerId",
+ ":addressId"
+ )}
+ render={({ match }) => (
+ <CustomerAddressDialog
+ address={maybe(() =>
+ customerData.data.user.addresses.find(
+ addr =>
+ addr.id ===
+ decodeURIComponent(
+ match.params.addressId
+ )
+ )
+ )}
+ confirmButtonState={"default"}
+ countries={[]}
+ errors={maybe(
+ () =>
+ updateCustomerAddressOpts.data
+ .addressUpdate.errors,
+ []
+ )}
+ open={!!match}
+ variant="edit"
+ onClose={closeModal}
+ onConfirm={formData =>
+ updateCustomerAddress({
+ variables: {
+ id: decodeURIComponent(
+ match.params.addressId
+ ),
+ input: {
+ ...formData,
+ country: formData.country.value
+ }
+ }
+ })
+ }
+ />
+ )}
+ />
+ </Switch>
+ </>
+ );
+ }}
+ </TypedCustomerAddressesQuery>
+ )}
+ </TypedRemoveCustomerAddressMutation>
+ )}
+ </TypedUpdateCustomerAddressMutation>
+ )}
+ </TypedCreateCustomerAddressMutation>
)}
</TypedSetCustomerDefaultAddressMutation>
);
diff --git a/saleor/static/dashboard-next/hooks/useShop.ts b/saleor/static/dashboard-next/hooks/useShop.ts
new file mode 100644
index 0000000000..315f934f1e
--- /dev/null
+++ b/saleor/static/dashboard-next/hooks/useShop.ts
@@ -0,0 +1,8 @@
+import { useContext } from "react";
+
+import { shopContext } from "../components/Shop";
+
+function useShop() {
+ return useContext(shopContext);
+}
+export default useShop;