Skip to content

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

Move product type dialog paths to qs

dominik-zeglen1bd05d2e14d0
diff --git a/saleor/static/dashboard-next/productTypes/index.tsx b/saleor/static/dashboard-next/productTypes/index.tsx
index bd18c97d99..7a7e809fc9 100644
--- a/saleor/static/dashboard-next/productTypes/index.tsx
+++ b/saleor/static/dashboard-next/productTypes/index.tsx
@@ -7,7 +7,8 @@ import i18n from "../i18n";
import {
productTypeAddPath,
productTypeListPath,
- productTypePath
+ productTypePath,
+ ProductTypeUrlQueryParams
} from "./urls";
import ProductTypeCreate from "./views/ProductTypeCreate";
import ProductTypeListComponent, {
@@ -31,9 +32,17 @@ interface ProductTypeUpdateRouteParams {
}
const ProductTypeUpdate: React.StatelessComponent<
RouteComponentProps<ProductTypeUpdateRouteParams>
-> = ({ match }) => (
- <ProductTypeUpdateComponent id={decodeURIComponent(match.params.id)} />
-);
+> = ({ match }) => {
+ const qs = parseQs(location.search.substr(1));
+ const params: ProductTypeUrlQueryParams = qs;
+
+ return (
+ <ProductTypeUpdateComponent
+ id={decodeURIComponent(match.params.id)}
+ params={params}
+ />
+ );
+};
export const ProductTypeRouter: React.StatelessComponent<
RouteComponentProps<any>
diff --git a/saleor/static/dashboard-next/productTypes/urls.ts b/saleor/static/dashboard-next/productTypes/urls.ts
index 52c78fb5a9..e300bfb0ed 100644
--- a/saleor/static/dashboard-next/productTypes/urls.ts
+++ b/saleor/static/dashboard-next/productTypes/urls.ts
@@ -1,5 +1,9 @@
+import { stringify as stringifyQs } from "qs";
import * as urlJoin from "url-join";
+import { Dialog, SingleAction } from "../types";
+import { AttributeTypeEnum } from "../types/globalTypes";
+
const productTypeSection = "/product-types/";
export const productTypeListPath = productTypeSection;
@@ -9,5 +13,15 @@ export const productTypeAddPath = urlJoin(productTypeSection, "add");
export const productTypeAddUrl = productTypeAddPath;
export const productTypePath = (id: string) => urlJoin(productTypeSection, id);
-export const productTypeUrl = (id: string) =>
- productTypePath(encodeURIComponent(id));
+export type ProductTypeUrlDialog =
+ | "add-attribute"
+ | "edit-attribute"
+ | "remove";
+export type ProductTypeUrlQueryParams = Dialog<ProductTypeUrlDialog> &
+ SingleAction & {
+ type?: AttributeTypeEnum;
+ };
+export const productTypeUrl = (
+ id: string,
+ params?: ProductTypeUrlQueryParams
+) => productTypePath(encodeURIComponent(id)) + "?" + stringifyQs(params);
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx
index e97c36c37d..55dd1c43f8 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.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 { AttributeTypeEnum } from "../../../types/globalTypes";
@@ -22,381 +21,330 @@ import { AttributeDelete } from "../../types/AttributeDelete";
import { AttributeUpdate } from "../../types/AttributeUpdate";
import { ProductTypeDelete } from "../../types/ProductTypeDelete";
import { ProductTypeUpdate as ProductTypeUpdateMutation } from "../../types/ProductTypeUpdate";
-import { productTypeListUrl, productTypeUrl } from "../../urls";
-import { ProductTypeUpdateErrors } from "./errors";
import {
- addAttributePath,
- addAttributeUrl,
- editAttributePath,
- editAttributeUrl,
- productTypeRemovePath,
- productTypeRemoveUrl
-} from "./urls";
+ productTypeListUrl,
+ productTypeUrl,
+ ProductTypeUrlQueryParams
+} from "../../urls";
+import { ProductTypeUpdateErrors } from "./errors";
interface ProductTypeUpdateProps {
id: string;
+ params: ProductTypeUrlQueryParams;
}
export const ProductTypeUpdate: React.StatelessComponent<
ProductTypeUpdateProps
-> = ({ id }) => (
- <Messages>
- {pushMessage => (
- <Navigator>
- {navigate => (
- <ProductTypeUpdateErrors>
- {({ errors, set: setErrors }) => (
- <TypedProductTypeDetailsQuery
- displayLoader
- variables={{ id }}
- require={["productType"]}
+> = ({ id, params }) => {
+ const navigate = useNavigator();
+ const notify = useNotifier();
+
+ return (
+ <ProductTypeUpdateErrors>
+ {({ errors, set: setErrors }) => (
+ <TypedProductTypeDetailsQuery
+ displayLoader
+ variables={{ id }}
+ require={["productType"]}
+ >
+ {({ data, loading: dataLoading }) => {
+ const closeModal = () => {
+ navigate(productTypeUrl(id), true);
+ setErrors.addAttributeErrors([]);
+ setErrors.editAttributeErrors([]);
+ };
+ const handleAttributeCreateSuccess = (data: AttributeCreate) => {
+ if (data.attributeCreate.errors.length === 0) {
+ notify({
+ text: i18n.t("Attribute created", {
+ context: "notification"
+ })
+ });
+ closeModal();
+ } else if (
+ data.attributeCreate.errors !== null &&
+ data.attributeCreate.errors.length > 0
+ ) {
+ setErrors.addAttributeErrors(data.attributeCreate.errors);
+ }
+ };
+ const handleAttributeDeleteSuccess = (data: AttributeDelete) => {
+ if (!data.attributeDelete.errors) {
+ notify({
+ text: i18n.t("Attribute deleted", {
+ context: "notification"
+ })
+ });
+ }
+ };
+ const handleAttributeUpdateSuccess = (data: AttributeUpdate) => {
+ if (data.attributeUpdate.errors.length === 0) {
+ notify({
+ text: i18n.t("Attribute updated", {
+ context: "notification"
+ })
+ });
+ closeModal();
+ } else if (
+ data.attributeUpdate.errors !== null &&
+ data.attributeUpdate.errors.length > 0
+ ) {
+ setErrors.editAttributeErrors(data.attributeUpdate.errors);
+ }
+ };
+ const handleProductTypeDeleteSuccess = (
+ deleteData: ProductTypeDelete
+ ) => {
+ if (deleteData.productTypeDelete.errors.length === 0) {
+ notify({
+ text: i18n.t("Product type deleted", {
+ context: "notification"
+ })
+ });
+ navigate(productTypeListUrl);
+ }
+ };
+ const handleProductTypeUpdateSuccess = (
+ updateData: ProductTypeUpdateMutation
+ ) => {
+ if (
+ !updateData.productTypeUpdate.errors ||
+ updateData.productTypeUpdate.errors.length === 0
+ ) {
+ notify({
+ text: i18n.t("Product type updated", {
+ context: "notification"
+ })
+ });
+ } else if (
+ updateData.productTypeUpdate.errors !== null &&
+ updateData.productTypeUpdate.errors.length > 0
+ ) {
+ setErrors.formErrors(updateData.productTypeUpdate.errors);
+ }
+ };
+
+ return (
+ <ProductTypeOperations
+ onAttributeCreate={handleAttributeCreateSuccess}
+ onAttributeDelete={handleAttributeDeleteSuccess}
+ onAttributeUpdate={handleAttributeUpdateSuccess}
+ onProductTypeDelete={handleProductTypeDeleteSuccess}
+ onProductTypeUpdate={handleProductTypeUpdateSuccess}
>
- {({ data, loading: dataLoading }) => {
- const closeModal = () => {
- navigate(productTypeUrl(id), true);
- setErrors.addAttributeErrors([]);
- setErrors.editAttributeErrors([]);
- };
- const handleAttributeCreateSuccess = (
- data: AttributeCreate
- ) => {
- if (data.attributeCreate.errors.length === 0) {
- pushMessage({
- text: i18n.t("Attribute created", {
- context: "notification"
- })
- });
- closeModal();
- } else if (
- data.attributeCreate.errors !== null &&
- data.attributeCreate.errors.length > 0
- ) {
- setErrors.addAttributeErrors(data.attributeCreate.errors);
- }
- };
- const handleAttributeDeleteSuccess = (
- data: AttributeDelete
- ) => {
- if (!data.attributeDelete.errors) {
- pushMessage({
- text: i18n.t("Attribute deleted", {
- context: "notification"
- })
- });
- }
- };
- const handleAttributeUpdateSuccess = (
- data: AttributeUpdate
+ {({
+ attributeCreate,
+ deleteAttribute,
+ deleteProductType,
+ updateAttribute,
+ updateProductType
+ }) => {
+ const handleProductTypeDelete = () =>
+ deleteProductType.mutate({ id });
+ const handleProductTypeUpdate = (
+ formData: ProductTypeForm
) => {
- if (data.attributeUpdate.errors.length === 0) {
- pushMessage({
- text: i18n.t("Attribute updated", {
- context: "notification"
- })
- });
- closeModal();
- } else if (
- data.attributeUpdate.errors !== null &&
- data.attributeUpdate.errors.length > 0
- ) {
- setErrors.editAttributeErrors(
- data.attributeUpdate.errors
- );
- }
+ updateProductType.mutate({
+ id,
+ input: {
+ hasVariants: formData.hasVariants,
+ isShippingRequired: formData.isShippingRequired,
+ name: formData.name,
+ productAttributes: formData.productAttributes.map(
+ choice => choice.value
+ ),
+ taxRate: formData.taxRate,
+ variantAttributes: formData.variantAttributes.map(
+ choice => choice.value
+ ),
+ weight: formData.weight
+ }
+ });
};
- const handleProductTypeDeleteSuccess = (
- deleteData: ProductTypeDelete
+ const handleAttributeCreate = (
+ data: AttributeForm,
+ type: AttributeTypeEnum
+ ) =>
+ attributeCreate.mutate({
+ id,
+ input: {
+ name: data.name,
+ values: data.values.map(value => ({
+ name: value.label
+ }))
+ },
+ type
+ });
+ const handleAttributeDelete = (
+ id: string,
+ event: React.MouseEvent<any>
) => {
- if (deleteData.productTypeDelete.errors.length === 0) {
- pushMessage({
- text: i18n.t("Product type deleted", {
- context: "notification"
- })
- });
- navigate(productTypeListUrl);
- }
+ event.stopPropagation();
+ deleteAttribute.mutate({ id });
};
- const handleProductTypeUpdateSuccess = (
- updateData: ProductTypeUpdateMutation
+ const handleAttributeUpdate = (
+ id: string,
+ formData: AttributeForm
) => {
- if (
- !updateData.productTypeUpdate.errors ||
- updateData.productTypeUpdate.errors.length === 0
- ) {
- pushMessage({
- text: i18n.t("Product type updated", {
- context: "notification"
- })
- });
- } else if (
- updateData.productTypeUpdate.errors !== null &&
- updateData.productTypeUpdate.errors.length > 0
- ) {
- setErrors.formErrors(updateData.productTypeUpdate.errors);
- }
+ const attribute = data.productType.variantAttributes
+ .concat(data.productType.productAttributes)
+ .filter(attribute => attribute.id === id)[0];
+ updateAttribute.mutate({
+ id,
+ input: {
+ addValues: formData.values
+ .filter(
+ value =>
+ !attribute.values
+ .map(value => value.id)
+ .includes(value.value)
+ )
+ .map(value => ({
+ name: value.label
+ })),
+ name: formData.name,
+ removeValues: attribute.values
+ .filter(
+ value =>
+ !formData.values
+ .map(value => value.value)
+ .includes(value.id)
+ )
+ .map(value => value.id)
+ }
+ });
};
+ const loading = updateProductType.opts.loading || dataLoading;
+ const deleteTransactionState = getMutationState(
+ deleteProductType.opts.called,
+ deleteProductType.opts.loading,
+ maybe(
+ () => deleteProductType.opts.data.productTypeDelete.errors
+ )
+ );
+
+ const attribute = maybe(() =>
+ data.productType.productAttributes
+ .concat(data.productType.variantAttributes)
+ .find(attribute => attribute.id === params.id)
+ );
return (
- <ProductTypeOperations
- onAttributeCreate={handleAttributeCreateSuccess}
- onAttributeDelete={handleAttributeDeleteSuccess}
- onAttributeUpdate={handleAttributeUpdateSuccess}
- onProductTypeDelete={handleProductTypeDeleteSuccess}
- onProductTypeUpdate={handleProductTypeUpdateSuccess}
- >
- {({
- attributeCreate,
- deleteAttribute,
- deleteProductType,
- updateAttribute,
- updateProductType
- }) => {
- const handleProductTypeDelete = () =>
- deleteProductType.mutate({ id });
- const handleProductTypeUpdate = (
- formData: ProductTypeForm
- ) => {
- updateProductType.mutate({
- id,
- input: {
- hasVariants: formData.hasVariants,
- isShippingRequired: formData.isShippingRequired,
- name: formData.name,
- productAttributes: formData.productAttributes.map(
- choice => choice.value
- ),
- taxRate: formData.taxRate,
- variantAttributes: formData.variantAttributes.map(
- choice => choice.value
- ),
- weight: formData.weight
- }
- });
- };
- const handleAttributeCreate = (
- data: AttributeForm,
- type: AttributeTypeEnum
- ) =>
- attributeCreate.mutate({
- id,
- input: {
- name: data.name,
- values: data.values.map(value => ({
- name: value.label
- }))
- },
- type
- });
- const handleAttributeDelete = (
- id: string,
- event: React.MouseEvent<any>
- ) => {
- event.stopPropagation();
- deleteAttribute.mutate({ id });
- };
- const handleAttributeUpdate = (
- id: string,
- formData: AttributeForm
- ) => {
- const attribute = data.productType.variantAttributes
- .concat(data.productType.productAttributes)
- .filter(attribute => attribute.id === id)[0];
- updateAttribute.mutate({
- id,
- input: {
- addValues: formData.values
- .filter(
- value =>
- !attribute.values
- .map(value => value.id)
- .includes(value.value)
- )
- .map(value => ({
- name: value.label
- })),
- name: formData.name,
- removeValues: attribute.values
- .filter(
- value =>
- !formData.values
- .map(value => value.value)
- .includes(value.id)
- )
- .map(value => value.id)
- }
- });
- };
- const loading =
- updateProductType.opts.loading || dataLoading;
- const deleteTransactionState = getMutationState(
- deleteProductType.opts.called,
- deleteProductType.opts.loading,
- maybe(
- () =>
- deleteProductType.opts.data.productTypeDelete
- .errors
+ <>
+ <WindowTitle title={maybe(() => data.productType.name)} />
+ <ProductTypeDetailsPage
+ defaultWeightUnit={maybe(
+ () => data.shop.defaultWeightUnit
+ )}
+ disabled={loading}
+ errors={errors.formErrors}
+ pageTitle={maybe(() => data.productType.name)}
+ productType={maybe(() => data.productType)}
+ saveButtonBarState={loading ? "loading" : "default"}
+ onAttributeAdd={type =>
+ navigate(
+ productTypeUrl(id, {
+ action: "add-attribute",
+ type
+ })
)
- );
- return (
- <>
- <WindowTitle
- title={maybe(() => data.productType.name)}
- />
- <ProductTypeDetailsPage
- defaultWeightUnit={maybe(
- () => data.shop.defaultWeightUnit
- )}
- disabled={loading}
- errors={errors.formErrors}
- pageTitle={maybe(() => data.productType.name)}
- productType={maybe(() => data.productType)}
- saveButtonBarState={
- loading ? "loading" : "default"
- }
- onAttributeAdd={type =>
- navigate(
- addAttributeUrl(encodeURIComponent(id), type)
- )
- }
- onAttributeDelete={handleAttributeDelete}
- onAttributeUpdate={attributeId =>
- navigate(
- editAttributeUrl(
- encodeURIComponent(id),
- encodeURIComponent(attributeId)
- )
+ }
+ onAttributeDelete={handleAttributeDelete}
+ onAttributeUpdate={attributeId =>
+ navigate(
+ productTypeUrl(id, {
+ action: "edit-attribute",
+ id: attributeId
+ })
+ )
+ }
+ onBack={() => navigate(productTypeListUrl)}
+ onDelete={() =>
+ navigate(
+ productTypeUrl(id, {
+ action: "remove"
+ })
+ )
+ }
+ onSubmit={handleProductTypeUpdate}
+ />
+ {!dataLoading && (
+ <>
+ {Object.keys(AttributeTypeEnum).map(key => (
+ <ProductTypeAttributeEditDialog
+ disabled={attributeCreate.opts.loading}
+ errors={errors.addAttributeErrors}
+ name=""
+ values={[]}
+ onClose={closeModal}
+ onConfirm={data =>
+ handleAttributeCreate(
+ data,
+ AttributeTypeEnum[key]
)
}
- onBack={() => navigate(productTypeListUrl)}
- onDelete={() =>
- navigate(productTypeRemoveUrl(id))
+ opened={
+ params.action === "add-attribute" &&
+ params.type === AttributeTypeEnum[key]
}
- onSubmit={handleProductTypeUpdate}
+ title={i18n.t("Add Attribute", {
+ context: "modal title"
+ })}
+ key={key}
/>
- {!dataLoading && (
- <>
- {Object.keys(AttributeTypeEnum).map(key => (
- <Route
- exact
- path={addAttributePath(
- encodeURIComponent(id),
- AttributeTypeEnum[key]
- )}
- key={key}
- >
- {({ match }) => (
- <ProductTypeAttributeEditDialog
- disabled={attributeCreate.opts.loading}
- errors={errors.addAttributeErrors}
- name=""
- values={[]}
- onClose={closeModal}
- onConfirm={data =>
- handleAttributeCreate(
- data,
- AttributeTypeEnum[key]
- )
- }
- opened={!!match}
- title={i18n.t("Add Attribute", {
- context: "modal title"
- })}
- />
- )}
- </Route>
- ))}
- <Route
- exact
- path={editAttributePath(
- ":productTypeId",
- ":id"
- )}
- >
- {({ match }) => {
- const attribute = maybe(
- () =>
- data.productType.productAttributes
- .concat(
- data.productType.variantAttributes
- )
- .filter(
- attribute =>
- attribute.id ===
- decodeURIComponent(
- match.params.id
- )
- )[0]
- );
- return (
- <ProductTypeAttributeEditDialog
- disabled={updateAttribute.opts.loading}
- errors={errors.editAttributeErrors}
- name={maybe(() => attribute.name)}
- values={maybe(() =>
- attribute.values.map(value => ({
- label: value.name,
- value: value.id
- }))
- )}
- onClose={closeModal}
- onConfirm={data =>
- handleAttributeUpdate(
- decodeURIComponent(match.params.id),
- data
- )
- }
- opened={!!match}
- title={i18n.t("Edit Attribute", {
- context: "modal title"
- })}
- />
- );
- }}
- </Route>
- <Route
- path={productTypeRemovePath(":id")}
- render={({ match }) => (
- <ActionDialog
- confirmButtonState={
- deleteTransactionState
- }
- open={!!match}
- onClose={() =>
- navigate(productTypeUrl(id))
- }
- onConfirm={handleProductTypeDelete}
- title={i18n.t("Remove product type")}
- variant="delete"
- >
- <DialogContentText
- dangerouslySetInnerHTML={{
- __html: i18n.t(
- "Are you sure you want to remove <strong>{{ name }}</strong>?",
- {
- name: maybe(
- () => data.productType.name
- )
- }
- )
- }}
- />
- </ActionDialog>
- )}
- />
- </>
+ ))}
+ <ProductTypeAttributeEditDialog
+ disabled={updateAttribute.opts.loading}
+ errors={errors.editAttributeErrors}
+ name={maybe(() => attribute.name)}
+ values={maybe(() =>
+ attribute.values.map(value => ({
+ label: value.name,
+ value: value.id
+ }))
)}
- </>
- );
- }}
- </ProductTypeOperations>
+ onClose={closeModal}
+ onConfirm={data =>
+ handleAttributeUpdate(params.id, data)
+ }
+ opened={params.action === "edit-attribute"}
+ title={i18n.t("Edit Attribute", {
+ context: "modal title"
+ })}
+ />
+ <ActionDialog
+ confirmButtonState={deleteTransactionState}
+ open={params.action === "remove"}
+ onClose={() => navigate(productTypeUrl(id))}
+ onConfirm={handleProductTypeDelete}
+ title={i18n.t("Remove product type")}
+ variant="delete"
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ name }}</strong>?",
+ {
+ name: maybe(
+ () => data.productType.name,
+ "..."
+ )
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ </>
+ )}
+ </>
);
}}
- </TypedProductTypeDetailsQuery>
- )}
- </ProductTypeUpdateErrors>
- )}
- </Navigator>
- )}
- </Messages>
-);
+ </ProductTypeOperations>
+ );
+ }}
+ </TypedProductTypeDetailsQuery>
+ )}
+ </ProductTypeUpdateErrors>
+ );
+};
export default ProductTypeUpdate;
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/urls.ts b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/urls.ts
index 8e28c58efa..e69de29bb2 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/urls.ts
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/urls.ts
@@ -1,28 +0,0 @@
-import * as urlJoin from "url-join";
-
-import { AttributeTypeEnum } from "../../../types/globalTypes";
-import { productTypePath } from "../../urls";
-
-export const addAttributePath = (
- productTypeId: string,
- type: AttributeTypeEnum
-) =>
- type === AttributeTypeEnum.PRODUCT
- ? urlJoin(productTypePath(productTypeId), "attribute/product/add")
- : urlJoin(productTypePath(productTypeId), "attribute/variant/add");
-export const addAttributeUrl = (
- productTypeId: string,
- type: AttributeTypeEnum
-) => addAttributePath(encodeURIComponent(productTypeId), type);
-
-export const editAttributePath = (productTypeId: string, attributeId: string) =>
- urlJoin(productTypePath(productTypeId), "attribute", attributeId);
-export const editAttributeUrl = (productTypeId: string, attributeId: string) =>
- editAttributePath(
- encodeURIComponent(productTypeId),
- encodeURIComponent(attributeId)
- );
-export const productTypeRemovePath = (id: string) =>
- urlJoin(productTypePath(id), "remove");
-export const productTypeRemoveUrl = (id: string) =>
- productTypeRemovePath(encodeURIComponent(id));