Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move customer dialog paths to qs
dominik-zeglenba08469a9b1c
diff --git a/saleor/static/dashboard-next/customers/index.tsx b/saleor/static/dashboard-next/customers/index.tsxindex ca5e451559..b097b13642 100644--- a/saleor/static/dashboard-next/customers/index.tsx+++ b/saleor/static/dashboard-next/customers/index.tsx@@ -7,8 +7,10 @@ import i18n from "../i18n";import {customerAddPath,customerAddressesPath,+ CustomerAddressesUrlQueryParams,customerListPath,- customerPath+ customerPath,+ CustomerUrlQueryParams} from "./urls";import CustomerAddressesViewComponent from "./views/CustomerAddresses";import CustomerCreateView from "./views/CustomerCreate";@@ -33,18 +35,34 @@ interface CustomerDetailsRouteParams {}const CustomerDetailsView: React.StatelessComponent<RouteComponentProps<CustomerDetailsRouteParams>-> = ({ match }) => (- <CustomerDetailsViewComponent id={decodeURIComponent(match.params.id)} />-);+> = ({ location, match }) => {+ const qs = parseQs(location.search.substr(1));+ const params: CustomerUrlQueryParams = qs;++ return (+ <CustomerDetailsViewComponent+ id={decodeURIComponent(match.params.id)}+ params={params}+ />+ );+};interface CustomerAddressesRouteParams {id: string;}const CustomerAddressesView: React.StatelessComponent<RouteComponentProps<CustomerAddressesRouteParams>-> = ({ match }) => (- <CustomerAddressesViewComponent id={decodeURIComponent(match.params.id)} />-);+> = ({ match }) => {+ const qs = parseQs(location.search.substr(1));+ const params: CustomerAddressesUrlQueryParams = qs;++ return (+ <CustomerAddressesViewComponent+ id={decodeURIComponent(match.params.id)}+ params={params}+ />+ );+};export const CustomerSection: React.StatelessComponent<{}> = () => (<>diff --git a/saleor/static/dashboard-next/customers/urls.ts b/saleor/static/dashboard-next/customers/urls.tsindex 515daacdf9..5cc8373195 100644--- a/saleor/static/dashboard-next/customers/urls.ts+++ b/saleor/static/dashboard-next/customers/urls.ts@@ -1,48 +1,30 @@+import { stringify as stringifyQs } from "qs";import * as urlJoin from "url-join";+import { Dialog, SingleAction } from "../types";+export const customerSection = "/customers/";export const customerListPath = customerSection;export const customerListUrl = customerListPath;export const customerPath = (id: string) => urlJoin(customerSection, id);-export const customerUrl = (id: string) => customerPath(encodeURIComponent(id));+export type CustomerUrlDialog = "remove";+export type CustomerUrlQueryParams = Dialog<CustomerUrlDialog>;+export const customerUrl = (id: string, params?: CustomerUrlQueryParams) =>+ customerPath(encodeURIComponent(id)) + "?" + stringifyQs(params);export const customerAddPath = urlJoin(customerSection, "add");export const customerAddUrl = customerAddPath;-export const customerRemovePath = (id: string) =>- urlJoin(customerPath(id), "remove");-export const customerRemoveUrl = (id: string) =>- customerRemovePath(encodeURIComponent(id));-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)- );+export type CustomerAddressesUrlDialog = "add" | "edit" | "remove";+export type CustomerAddressesUrlQueryParams = Dialog<+ CustomerAddressesUrlDialog+> &+ SingleAction;+export const customerAddressesUrl = (+ id: string,+ params?: CustomerAddressesUrlQueryParams+) => customerAddressesPath(encodeURIComponent(id)) + "?" + stringifyQs(params);diff --git a/saleor/static/dashboard-next/customers/views/CustomerAddresses.tsx b/saleor/static/dashboard-next/customers/views/CustomerAddresses.tsxindex ff59a633d1..75ac803fee 100644--- a/saleor/static/dashboard-next/customers/views/CustomerAddresses.tsx+++ b/saleor/static/dashboard-next/customers/views/CustomerAddresses.tsx@@ -1,6 +1,5 @@import DialogContentText from "@material-ui/core/DialogContentText";import * as React from "react";-import { Route, Switch } from "react-router-dom";import ActionDialog from "../../components/ActionDialog";import { WindowTitle } from "../../components/WindowTitle";@@ -23,26 +22,28 @@ import { RemoveCustomerAddress } from "../types/RemoveCustomerAddress";import { SetCustomerDefaultAddress } from "../types/SetCustomerDefaultAddress";import { UpdateCustomerAddress } from "../types/UpdateCustomerAddress";import {- customerAddressAddPath,- customerAddressAddUrl,customerAddressesUrl,- customerAddressPath,- customerAddressRemovePath,- customerAddressRemoveUrl,- customerAddressUrl,+ CustomerAddressesUrlDialog,+ CustomerAddressesUrlQueryParams,customerUrl} from "../urls";interface CustomerAddressesProps {id: string;+ params: CustomerAddressesUrlQueryParams;}-const CustomerAddresses: React.FC<CustomerAddressesProps> = ({ id }) => {+const CustomerAddresses: React.FC<CustomerAddressesProps> = ({+ id,+ params+}) => {const navigate = useNavigator();const notify = useNotifier();const shop = useShop();const closeModal = () => navigate(customerAddressesUrl(id), true);+ const openModal = (action: CustomerAddressesUrlDialog, addressId?: string) =>+ navigate(customerAddressesUrl(id, { action, id: addressId }));const handleSetAddressAsDefault = (data: SetCustomerDefaultAddress) => {if (data.addressSetDefault.errors.length === 0) {@@ -140,15 +141,11 @@ const CustomerAddresses: React.FC<CustomerAddressesProps> = ({ id }) => {<CustomerAddressListPagecustomer={maybe(() => customerData.data.user)}disabled={customerData.loading}- onAdd={() => navigate(customerAddressAddUrl(id))}+ onAdd={() => openModal("add")}onBack={() => navigate(customerUrl(id))}- onEdit={addressId =>- navigate(customerAddressUrl(id, addressId))- }+ onEdit={addressId => openModal("edit", addressId)}onRemove={addressId =>- navigate(- customerAddressRemoveUrl(id, addressId)- )+ openModal("remove", addressId)}onSetAsDefault={(addressId, type) =>setCustomerDefaultAddress({@@ -156,124 +153,87 @@ const CustomerAddresses: React.FC<CustomerAddressesProps> = ({ id }) => {})}/>- <Switch>- <Route- path={customerAddressAddPath(":customerId")}- render={({ match }) => (- <CustomerAddressDialog- address={undefined}- confirmButtonState={- createAddressTransitionState- }- countries={maybe(- () =>- shop.countries.map(country => ({- code: country.code,- label: country.country- })),- []- )}- errors={maybe(- () =>- createCustomerAddressOpts.data- .addressCreate.errors,- []- )}- open={!!match}- variant="create"- onClose={closeModal}- onConfirm={formData =>- createCustomerAddress({- variables: {- id,- input: {- ...formData,- country: formData.country.value- }- }- })- }- />- )}- />- <Route- path={customerAddressRemovePath(- ":customerId",- ":addressId"- )}- render={({ match }) => (- <ActionDialog- open={!!match}- variant="delete"- title={i18n.t("Remove Address")}- confirmButtonState={- removeAddressTransitionState- }- 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={- updateAddressTransitionState+ <CustomerAddressDialog+ address={undefined}+ confirmButtonState={createAddressTransitionState}+ countries={maybe(+ () =>+ shop.countries.map(country => ({+ code: country.code,+ label: country.country+ })),+ []+ )}+ errors={maybe(+ () =>+ createCustomerAddressOpts.data.addressCreate+ .errors,+ []+ )}+ open={params.action === "add"}+ variant="create"+ onClose={closeModal}+ onConfirm={formData =>+ createCustomerAddress({+ variables: {+ id,+ input: {+ ...formData,+ country: formData.country.value}- 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- }- }- })+ }+ })+ }+ />+ <CustomerAddressDialog+ address={maybe(() =>+ customerData.data.user.addresses.find(+ addr => addr.id === params.id+ )+ )}+ confirmButtonState={updateAddressTransitionState}+ countries={[]}+ errors={maybe(+ () =>+ updateCustomerAddressOpts.data.addressUpdate+ .errors,+ []+ )}+ open={params.action === "edit"}+ variant="edit"+ onClose={closeModal}+ onConfirm={formData =>+ updateCustomerAddress({+ variables: {+ id: params.id,+ input: {+ ...formData,+ country: formData.country.value}- />+ }+ })+ }+ />+ <ActionDialog+ open={params.action === "remove"}+ variant="delete"+ title={i18n.t("Remove Address")}+ confirmButtonState={removeAddressTransitionState}+ onClose={closeModal}+ onConfirm={() =>+ removeCustomerAddress({+ variables: {+ id: params.id+ }+ })+ }+ >+ <DialogContentText>+ {i18n.t(+ "Are you sure you want to remove this address from users address book?")}- />- </Switch>+ </DialogContentText>+ </ActionDialog></>);}}diff --git a/saleor/static/dashboard-next/customers/views/CustomerDetails.tsx b/saleor/static/dashboard-next/customers/views/CustomerDetails.tsxindex 475bd31bf1..c00958a24a 100644--- a/saleor/static/dashboard-next/customers/views/CustomerDetails.tsx+++ b/saleor/static/dashboard-next/customers/views/CustomerDetails.tsx@@ -1,11 +1,10 @@import DialogContentText from "@material-ui/core/DialogContentText";import * as React from "react";-import { Route } from "react-router-dom";import ActionDialog from "../../components/ActionDialog";-import Messages from "../../components/messages";-import Navigator from "../../components/Navigator";import { WindowTitle } from "../../components/WindowTitle";+import useNavigator from "../../hooks/useNavigator";+import useNotifier from "../../hooks/useNotifier";import i18n from "../../i18n";import { getMutationState, maybe } from "../../misc";import { orderUrl } from "../../orders/urls";@@ -20,154 +19,142 @@ import { UpdateCustomer } from "../types/UpdateCustomer";import {customerAddressesUrl,customerListUrl,- customerRemovePath,- customerRemoveUrl,- customerUrl+ customerUrl,+ CustomerUrlQueryParams} from "../urls";interface CustomerDetailsViewProps {id: string;+ params: CustomerUrlQueryParams;}export const CustomerDetailsView: React.StatelessComponent<CustomerDetailsViewProps-> = ({ id }) => (- <Navigator>- {navigate => (- <Messages>- {pushMessage => {- const handleCustomerUpdateSuccess = (data: UpdateCustomer) => {- if (data.customerUpdate.errors.length === 0) {- pushMessage({- text: i18n.t("Customer updated", {- context: "notification"- })- });- }- };- const handleCustomerRemoveSuccess = (data: RemoveCustomer) => {- if (data.customerDelete.errors.length === 0) {- pushMessage({- text: i18n.t("Customer removed", {- context: "notification"- })- });- navigate(customerListUrl);- }- };- return (- <TypedRemoveCustomerMutation+> = ({ id, params }) => {+ const navigate = useNavigator();+ const notify = useNotifier();++ const handleCustomerUpdateSuccess = (data: UpdateCustomer) => {+ if (data.customerUpdate.errors.length === 0) {+ notify({+ text: i18n.t("Customer updated", {+ context: "notification"+ })+ });+ }+ };+ const handleCustomerRemoveSuccess = (data: RemoveCustomer) => {+ if (data.customerDelete.errors.length === 0) {+ notify({+ text: i18n.t("Customer removed", {+ context: "notification"+ })+ });+ navigate(customerListUrl);+ }+ };+ return (+ <TypedRemoveCustomerMutation+ variables={{ id }}+ onCompleted={handleCustomerRemoveSuccess}+ >+ {(removeCustomer, removeCustomerOpts) => (+ <TypedUpdateCustomerMutation onCompleted={handleCustomerUpdateSuccess}>+ {(updateCustomer, updateCustomerOpts) => (+ <TypedCustomerDetailsQuery+ displayLoadervariables={{ id }}- onCompleted={handleCustomerRemoveSuccess}+ require={["user"]}>- {(removeCustomer, removeCustomerOpts) => (- <TypedUpdateCustomerMutation- onCompleted={handleCustomerUpdateSuccess}- >- {(updateCustomer, updateCustomerOpts) => (- <TypedCustomerDetailsQuery- displayLoader- variables={{ id }}- require={["user"]}+ {customerDetails => {+ const formTransitionState = getMutationState(+ updateCustomerOpts.called,+ updateCustomerOpts.loading,+ maybe(() => updateCustomerOpts.data.customerUpdate.errors)+ );+ const removeTransitionState = getMutationState(+ removeCustomerOpts.called,+ removeCustomerOpts.loading,+ maybe(() => removeCustomerOpts.data.customerDelete.errors)+ );++ return (+ <>+ <WindowTitle+ title={maybe(() => customerDetails.data.user.email)}+ />+ <CustomerDetailsPage+ customer={customerDetails.data.user}+ disabled={+ customerDetails.loading ||+ updateCustomerOpts.loading ||+ removeCustomerOpts.loading+ }+ errors={maybe(+ () => updateCustomerOpts.data.customerUpdate.errors+ )}+ saveButtonBar={formTransitionState}+ onAddressManageClick={() =>+ navigate(customerAddressesUrl(id))+ }+ onBack={() => navigate(customerListUrl)}+ onRowClick={id => navigate(orderUrl(id))}+ onSubmit={formData =>+ updateCustomer({+ variables: {+ id,+ input: {+ email: formData.email,+ firstName: formData.firstName,+ isActive: formData.isActive,+ lastName: formData.lastName,+ note: formData.note+ }+ }+ })+ }+ onDelete={() =>+ navigate(+ customerUrl(id, {+ action: "remove"+ })+ )+ }+ onViewAllOrdersClick={() => undefined} // TODO: add filters to order #3172+ />+ <ActionDialog+ confirmButtonState={removeTransitionState}+ onClose={() => navigate(customerUrl(id), true)}+ onConfirm={() => removeCustomer()}+ title={i18n.t("Remove customer", {+ context: "modal title"+ })}+ variant="delete"+ open={params.action === "remove"}>- {customerDetails => {- const formTransitionState = getMutationState(- updateCustomerOpts.called,- updateCustomerOpts.loading,- maybe(- () => updateCustomerOpts.data.customerUpdate.errors+ <DialogContentText+ dangerouslySetInnerHTML={{+ __html: i18n.t(+ "Are you sure you want to remove <strong>{{ email }}</strong>?",+ {+ context: "modal content",+ email: maybe(+ () => customerDetails.data.user.email,+ "..."+ )+ })- );- const removeTransitionState = getMutationState(- removeCustomerOpts.called,- removeCustomerOpts.loading,- maybe(- () => removeCustomerOpts.data.customerDelete.errors- )- );-- return (- <>- <WindowTitle- title={maybe(- () => customerDetails.data.user.email- )}- />- <CustomerDetailsPage- customer={customerDetails.data.user}- disabled={- customerDetails.loading ||- updateCustomerOpts.loading ||- removeCustomerOpts.loading- }- errors={maybe(- () =>- updateCustomerOpts.data.customerUpdate.errors- )}- saveButtonBar={formTransitionState}- onAddressManageClick={() =>- navigate(customerAddressesUrl(id))- }- onBack={() => navigate(customerListUrl)}- onRowClick={id => navigate(orderUrl(id))}- onSubmit={formData =>- updateCustomer({- variables: {- id,- input: {- email: formData.email,- firstName: formData.firstName,- isActive: formData.isActive,- lastName: formData.lastName,- note: formData.note- }- }- })- }- onDelete={() => navigate(customerRemoveUrl(id))}- onViewAllOrdersClick={() => undefined} // TODO: add filters to order #3172- />- <Route exact path={customerRemovePath(":id")}>- {({ match }) => (- <ActionDialog- confirmButtonState={removeTransitionState}- onClose={() => navigate(customerUrl(id))}- onConfirm={() => removeCustomer()}- title={i18n.t("Remove customer", {- context: "modal title"- })}- variant="delete"- open={!!match}- >- <DialogContentText- dangerouslySetInnerHTML={{- __html: i18n.t(- "Are you sure you want to remove <strong>{{ email }}</strong>?",- {- context: "modal content",- email: maybe(- () =>- customerDetails.data.user.email- )- }- )- }}- />- </ActionDialog>- )}- </Route>- </>- );- }}- </TypedCustomerDetailsQuery>- )}- </TypedUpdateCustomerMutation>- )}- </TypedRemoveCustomerMutation>- );- }}- </Messages>- )}- </Navigator>-);+ }}+ />+ </ActionDialog>+ </>+ );+ }}+ </TypedCustomerDetailsQuery>+ )}+ </TypedUpdateCustomerMutation>+ )}+ </TypedRemoveCustomerMutation>+ );+};export default CustomerDetailsView;diff --git a/saleor/static/dashboard-next/types.ts b/saleor/static/dashboard-next/types.tsindex 343e66082c..38e0729ac5 100644--- a/saleor/static/dashboard-next/types.ts+++ b/saleor/static/dashboard-next/types.ts@@ -40,3 +40,6 @@ export type Dialog<TDialog extends string> = Partial<{export type ActiveTab<TTab extends string> = Partial<{activeTab: TTab;}>;+export type SingleAction = Partial<{+ id: string;+}>;