Skip to content

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

Move collection details dialog paths to qs

dominik-zeglen8c307b5201ea
diff --git a/saleor/static/dashboard-next/collections/index.tsx b/saleor/static/dashboard-next/collections/index.tsx
index 4bb45457e5..5b811df95e 100644
--- a/saleor/static/dashboard-next/collections/index.tsx
+++ b/saleor/static/dashboard-next/collections/index.tsx
@@ -4,11 +4,14 @@ import { Route, RouteComponentProps, Switch } from "react-router-dom";
import { WindowTitle } from "../components/WindowTitle";
import i18n from "../i18n";
-import { collectionAddPath, collectionListPath, collectionPath } from "./urls";
+import {
+ collectionAddPath,
+ collectionListPath,
+ collectionPath,
+ CollectionUrlQueryParams
+} from "./urls";
import CollectionCreate from "./views/CollectionCreate";
-import CollectionDetailsView, {
- CollectionDetailsQueryParams
-} from "./views/CollectionDetails";
+import CollectionDetailsView from "./views/CollectionDetails";
import CollectionListView, {
CollectionListQueryParams
} from "./views/CollectionList";
@@ -31,10 +34,7 @@ const CollectionDetails: React.StatelessComponent<
RouteComponentProps<CollectionDetailsRouteProps>
> = ({ location, match }) => {
const qs = parseQs(location.search.substr(1));
- const params: CollectionDetailsQueryParams = {
- after: qs.after,
- before: qs.before
- };
+ const params: CollectionUrlQueryParams = qs;
return (
<CollectionDetailsView
id={decodeURIComponent(match.params.id)}
diff --git a/saleor/static/dashboard-next/collections/urls.ts b/saleor/static/dashboard-next/collections/urls.ts
index 68ee8d2465..d18942425c 100644
--- a/saleor/static/dashboard-next/collections/urls.ts
+++ b/saleor/static/dashboard-next/collections/urls.ts
@@ -1,28 +1,18 @@
+import { stringify as stringifyQs } from "qs";
import * as urlJoin from "url-join";
+import { Dialog, Pagination } from "../types";
+
const collectionSectionUrl = "/collections/";
export const collectionListPath = collectionSectionUrl;
export const collectionListUrl = collectionSectionUrl;
export const collectionPath = (id: string) => urlJoin(collectionSectionUrl, id);
-export const collectionUrl = (id: string) =>
- collectionPath(encodeURIComponent(id));
+export type CollectionUrlDialog = "remove" | "removeImage" | "assign";
+export type CollectionUrlQueryParams = Pagination & Dialog<CollectionUrlDialog>;
+export const collectionUrl = (id: string, params?: CollectionUrlQueryParams) =>
+ collectionPath(encodeURIComponent(id)) + "?" + stringifyQs(params);
export const collectionAddPath = urlJoin(collectionSectionUrl, "add");
export const collectionAddUrl = collectionAddPath;
-
-export const collectionRemovePath = (id: string) =>
- urlJoin(collectionPath(id), "remove");
-export const collectionRemoveUrl = (id: string) =>
- collectionRemovePath(encodeURIComponent(id));
-
-export const collectionImageRemovePath = (id: string) =>
- urlJoin(collectionPath(id), "removeImage");
-export const collectionImageRemoveUrl = (id: string) =>
- collectionImageRemovePath(encodeURIComponent(id));
-
-export const collectionAddProductPath = (id: string) =>
- urlJoin(collectionPath(id), "add");
-export const collectionAddProductUrl = (id: string) =>
- collectionAddProductPath(encodeURIComponent(id));
diff --git a/saleor/static/dashboard-next/collections/views/CollectionDetails.tsx b/saleor/static/dashboard-next/collections/views/CollectionDetails.tsx
index 4a4064f250..8f381faee2 100644
--- a/saleor/static/dashboard-next/collections/views/CollectionDetails.tsx
+++ b/saleor/static/dashboard-next/collections/views/CollectionDetails.tsx
@@ -1,18 +1,16 @@
import DialogContentText from "@material-ui/core/DialogContentText";
import * as React from "react";
-import { Route } from "react-router-dom";
import ActionDialog from "../../components/ActionDialog";
import AssignProductDialog from "../../components/AssignProductDialog";
-import Messages from "../../components/messages";
-import Navigator from "../../components/Navigator";
import { createPaginationState, Paginator } from "../../components/Paginator";
import { WindowTitle } from "../../components/WindowTitle";
import { SearchProductsProvider } from "../../containers/SearchProducts";
+import useNavigator from "../../hooks/useNavigator";
+import useNotifier from "../../hooks/useNotifier";
import i18n from "../../i18n";
import { getMutationState, maybe } from "../../misc";
import { productUrl } from "../../products/urls";
-import { Pagination } from "../../types";
import { CollectionInput } from "../../types/globalTypes";
import CollectionDetailsPage, {
CollectionDetailsPageFormData
@@ -24,362 +22,310 @@ import { CollectionUpdate } from "../types/CollectionUpdate";
import { RemoveCollection } from "../types/RemoveCollection";
import { UnassignCollectionProduct } from "../types/UnassignCollectionProduct";
import {
- collectionAddProductPath,
- collectionAddProductUrl,
- collectionImageRemovePath,
- collectionImageRemoveUrl,
collectionListUrl,
- collectionRemovePath,
- collectionRemoveUrl,
- collectionUrl
+ collectionUrl,
+ CollectionUrlDialog,
+ CollectionUrlQueryParams
} from "../urls";
-export type CollectionDetailsQueryParams = Pagination;
-
interface CollectionDetailsProps {
id: string;
- params: CollectionDetailsQueryParams;
+ params: CollectionUrlQueryParams;
}
const PAGINATE_BY = 20;
export const CollectionDetails: React.StatelessComponent<
CollectionDetailsProps
-> = ({ id, params }) => (
- <Messages>
- {pushMessage => (
- <Navigator>
- {navigate => {
- const paginationState = createPaginationState(PAGINATE_BY, params);
+> = ({ id, params }) => {
+ const navigate = useNavigator();
+ const notify = useNotifier();
- return (
- <TypedCollectionDetailsQuery
- displayLoader
- variables={{ id, ...paginationState }}
- require={["collection"]}
- >
- {({ data, loading }) => {
- const handleCollectionUpdate = (data: CollectionUpdate) => {
- if (data.collectionUpdate.errors.length === 0) {
- pushMessage({
- text: i18n.t("Updated collection", {
- context: "notification"
- })
- });
- navigate(collectionUrl(id));
- } else {
- const backgroundImageError = data.collectionUpdate.errors.find(
- error =>
- error.field ===
- ("backgroundImage" as keyof CollectionInput)
- );
- if (backgroundImageError) {
- pushMessage({
- text: backgroundImageError.message
- });
- }
- }
- };
+ const closeModal = () =>
+ navigate(
+ collectionUrl(id, {
+ ...params,
+ action: undefined
+ }),
+ true
+ );
+ const openModal = (action: CollectionUrlDialog) =>
+ navigate(
+ collectionUrl(id, {
+ ...params,
+ action
+ }),
+ false
+ );
- const handleProductAssign = (data: CollectionAssignProduct) => {
- if (data.collectionAddProducts.errors.length === 0) {
- pushMessage({
- text: i18n.t("Added product to collection", {
- context: "notification"
- })
- });
- navigate(collectionUrl(id), true);
- }
- };
+ const paginationState = createPaginationState(PAGINATE_BY, params);
- const handleProductUnassign = (
- data: UnassignCollectionProduct
- ) => {
- if (data.collectionRemoveProducts.errors.length === 0) {
- pushMessage({
- text: i18n.t("Removed product from collection", {
- context: "notification"
- })
- });
- }
- };
+ return (
+ <TypedCollectionDetailsQuery
+ displayLoader
+ variables={{ id, ...paginationState }}
+ require={["collection"]}
+ >
+ {({ data, loading }) => {
+ const handleCollectionUpdate = (data: CollectionUpdate) => {
+ if (data.collectionUpdate.errors.length === 0) {
+ notify({
+ text: i18n.t("Updated collection", {
+ context: "notification"
+ })
+ });
+ navigate(collectionUrl(id));
+ } else {
+ const backgroundImageError = data.collectionUpdate.errors.find(
+ error =>
+ error.field === ("backgroundImage" as keyof CollectionInput)
+ );
+ if (backgroundImageError) {
+ notify({
+ text: backgroundImageError.message
+ });
+ }
+ }
+ };
- const handleCollectionRemove = (data: RemoveCollection) => {
- if (data.collectionDelete.errors.length === 0) {
- pushMessage({
- text: i18n.t("Removed collection", {
- context: "notification"
- })
- });
- navigate(collectionListUrl);
+ const handleProductAssign = (data: CollectionAssignProduct) => {
+ if (data.collectionAddProducts.errors.length === 0) {
+ notify({
+ text: i18n.t("Added product to collection", {
+ context: "notification"
+ })
+ });
+ navigate(collectionUrl(id), true);
+ }
+ };
+
+ const handleProductUnassign = (data: UnassignCollectionProduct) => {
+ if (data.collectionRemoveProducts.errors.length === 0) {
+ notify({
+ text: i18n.t("Removed product from collection", {
+ context: "notification"
+ })
+ });
+ }
+ };
+
+ const handleCollectionRemove = (data: RemoveCollection) => {
+ if (data.collectionDelete.errors.length === 0) {
+ notify({
+ text: i18n.t("Removed collection", {
+ context: "notification"
+ })
+ });
+ navigate(collectionListUrl);
+ }
+ };
+ return (
+ <CollectionOperations
+ onUpdate={handleCollectionUpdate}
+ onProductAssign={handleProductAssign}
+ onProductUnassign={handleProductUnassign}
+ onRemove={handleCollectionRemove}
+ >
+ {({
+ updateCollection,
+ updateCollectionWithHomepage,
+ assignProduct,
+ unassignProduct,
+ removeCollection
+ }) => {
+ const handleSubmit = (
+ formData: CollectionDetailsPageFormData
+ ) => {
+ const input = {
+ backgroundImageAlt: formData.backgroundImageAlt,
+ descriptionJson: JSON.stringify(formData.description),
+ isPublished: formData.isPublished,
+ name: formData.name,
+ seo: {
+ description: formData.seoDescription,
+ title: formData.seoTitle
}
};
- return (
- <CollectionOperations
- onUpdate={handleCollectionUpdate}
- onProductAssign={handleProductAssign}
- onProductUnassign={handleProductUnassign}
- onRemove={handleCollectionRemove}
- >
- {({
- updateCollection,
- updateCollectionWithHomepage,
- assignProduct,
- unassignProduct,
- removeCollection
- }) => {
- const handleSubmit = (
- formData: CollectionDetailsPageFormData
- ) => {
- const input = {
- backgroundImageAlt: formData.backgroundImageAlt,
- descriptionJson: JSON.stringify(formData.description),
- isPublished: formData.isPublished,
- name: formData.name,
- seo: {
- description: formData.seoDescription,
- title: formData.seoTitle
- }
- };
- const isFeatured = data.shop.homepageCollection
- ? data.shop.homepageCollection.id ===
- data.collection.id
- : false;
+ const isFeatured = data.shop.homepageCollection
+ ? data.shop.homepageCollection.id === data.collection.id
+ : false;
- if (formData.isFeatured !== isFeatured) {
- updateCollectionWithHomepage.mutate({
- homepageId: formData.isFeatured ? id : null,
- id,
- input
- });
- } else {
+ if (formData.isFeatured !== isFeatured) {
+ updateCollectionWithHomepage.mutate({
+ homepageId: formData.isFeatured ? id : null,
+ id,
+ input
+ });
+ } else {
+ updateCollection.mutate({
+ id,
+ input
+ });
+ }
+ };
+
+ const formTransitionState = getMutationState(
+ updateCollection.opts.called ||
+ updateCollectionWithHomepage.opts.called,
+ updateCollection.opts.loading ||
+ updateCollectionWithHomepage.opts.loading,
+ maybe(() => updateCollection.opts.data.collectionUpdate.errors),
+ maybe(
+ () =>
+ updateCollectionWithHomepage.opts.data.collectionUpdate
+ .errors
+ ),
+ maybe(
+ () =>
+ updateCollectionWithHomepage.opts.data
+ .homepageCollectionUpdate.errors
+ )
+ );
+ const assignTransitionState = getMutationState(
+ assignProduct.opts.called,
+ assignProduct.opts.loading,
+ maybe(
+ () => assignProduct.opts.data.collectionAddProducts.errors
+ )
+ );
+ const removeTransitionState = getMutationState(
+ removeCollection.opts.called,
+ removeCollection.opts.loading,
+ maybe(() => removeCollection.opts.data.collectionDelete.errors)
+ );
+ const imageRemoveTransitionState = getMutationState(
+ updateCollection.opts.called,
+ updateCollection.opts.loading,
+ maybe(() => updateCollection.opts.data.collectionUpdate.errors)
+ );
+
+ return (
+ <>
+ <WindowTitle title={maybe(() => data.collection.name)} />
+ <Paginator
+ pageInfo={maybe(() => data.collection.products.pageInfo)}
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({ loadNextPage, loadPreviousPage, pageInfo }) => (
+ <CollectionDetailsPage
+ onAdd={() => openModal("assign")}
+ onBack={() => navigate(collectionListUrl)}
+ disabled={loading}
+ collection={data.collection}
+ isFeatured={maybe(
+ () =>
+ data.shop.homepageCollection.id ===
+ data.collection.id,
+ false
+ )}
+ onCollectionRemove={() => openModal("remove")}
+ onImageDelete={() => openModal("removeImage")}
+ onImageUpload={file =>
updateCollection.mutate({
id,
- input
+ input: {
+ backgroundImage: file
+ }
+ })
+ }
+ onSubmit={handleSubmit}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ pageInfo={pageInfo}
+ onProductUnassign={(productId, event) => {
+ event.stopPropagation();
+ unassignProduct.mutate({
+ collectionId: id,
+ productId,
+ ...paginationState
});
+ }}
+ onRowClick={id => () => navigate(productUrl(id))}
+ saveButtonBarState={formTransitionState}
+ />
+ )}
+ </Paginator>
+ <SearchProductsProvider>
+ {(searchProducts, searchProductsOpts) => (
+ <AssignProductDialog
+ confirmButtonState={assignTransitionState}
+ open={params.action === "assign"}
+ onFetch={searchProducts}
+ loading={searchProductsOpts.loading}
+ onClose={() => navigate(collectionUrl(id), true, true)}
+ onSubmit={formData =>
+ assignProduct.mutate({
+ ...paginationState,
+ collectionId: id,
+ productIds: formData.products.map(
+ product => product.id
+ )
+ })
}
- };
-
- const formTransitionState = getMutationState(
- updateCollection.opts.called ||
- updateCollectionWithHomepage.opts.called,
- updateCollection.opts.loading ||
- updateCollectionWithHomepage.opts.loading,
- maybe(
- () =>
- updateCollection.opts.data.collectionUpdate.errors
- ),
- maybe(
- () =>
- updateCollectionWithHomepage.opts.data
- .collectionUpdate.errors
- ),
- maybe(
- () =>
- updateCollectionWithHomepage.opts.data
- .homepageCollectionUpdate.errors
- )
- );
- const assignTransitionState = getMutationState(
- assignProduct.opts.called,
- assignProduct.opts.loading,
- maybe(
- () =>
- assignProduct.opts.data.collectionAddProducts.errors
- )
- );
- const removeTransitionState = getMutationState(
- removeCollection.opts.called,
- removeCollection.opts.loading,
- maybe(
- () =>
- removeCollection.opts.data.collectionDelete.errors
- )
- );
- const imageRemoveTransitionState = getMutationState(
- updateCollection.opts.called,
- updateCollection.opts.loading,
- maybe(
- () =>
- updateCollection.opts.data.collectionUpdate.errors
+ products={maybe(() =>
+ searchProductsOpts.data.products.edges
+ .map(edge => edge.node)
+ .filter(suggestedProduct => suggestedProduct.id)
+ )}
+ />
+ )}
+ </SearchProductsProvider>
+ <ActionDialog
+ confirmButtonState={removeTransitionState}
+ onClose={closeModal}
+ onConfirm={() => removeCollection.mutate({ id })}
+ open={params.action === "remove"}
+ title={i18n.t("Remove collection", {
+ context: "modal title"
+ })}
+ variant="delete"
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ collectionName }}</strong>?",
+ {
+ collectionName: maybe(
+ () => data.collection.name,
+ "..."
+ ),
+ context: "modal"
+ }
)
- );
-
- return (
- <>
- <WindowTitle
- title={maybe(() => data.collection.name)}
- />
- <Paginator
- pageInfo={maybe(
- () => data.collection.products.pageInfo
- )}
- paginationState={paginationState}
- queryString={params}
- >
- {({ loadNextPage, loadPreviousPage, pageInfo }) => (
- <CollectionDetailsPage
- onAdd={() =>
- navigate(
- collectionAddProductUrl(id),
- false,
- true
- )
- }
- onBack={() => navigate(collectionListUrl)}
- disabled={loading}
- collection={data.collection}
- isFeatured={maybe(
- () =>
- data.shop.homepageCollection.id ===
- data.collection.id,
- false
- )}
- onCollectionRemove={() =>
- navigate(collectionRemoveUrl(id), false, true)
- }
- onImageDelete={() =>
- navigate(
- collectionImageRemoveUrl(id),
- false,
- true
- )
- }
- onImageUpload={file =>
- updateCollection.mutate({
- id,
- input: {
- backgroundImage: file
- }
- })
- }
- onSubmit={handleSubmit}
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- pageInfo={pageInfo}
- onProductUnassign={(productId, event) => {
- event.stopPropagation();
- unassignProduct.mutate({
- collectionId: id,
- productId,
- ...paginationState
- });
- }}
- onRowClick={id => () =>
- navigate(productUrl(id))}
- saveButtonBarState={formTransitionState}
- />
- )}
- </Paginator>
- <Route
- path={collectionAddProductPath(":id")}
- render={({ match }) => (
- <SearchProductsProvider>
- {(searchProducts, searchProductsOpts) => (
- <AssignProductDialog
- confirmButtonState={assignTransitionState}
- open={!!match}
- onFetch={searchProducts}
- loading={searchProductsOpts.loading}
- onClose={() =>
- navigate(collectionUrl(id), true, true)
- }
- onSubmit={formData =>
- assignProduct.mutate({
- ...paginationState,
- collectionId: id,
- productIds: formData.products.map(
- product => product.id
- )
- })
- }
- products={maybe(() =>
- searchProductsOpts.data.products.edges
- .map(edge => edge.node)
- .filter(
- suggestedProduct =>
- suggestedProduct.id
- )
- )}
- />
- )}
- </SearchProductsProvider>
- )}
- />
- <Route
- path={collectionRemovePath(":id")}
- render={({ match }) => (
- <ActionDialog
- confirmButtonState={removeTransitionState}
- onClose={() =>
- navigate(collectionUrl(id), true, true)
- }
- onConfirm={() =>
- removeCollection.mutate({ id })
- }
- open={!!match}
- title={i18n.t("Remove collection", {
- context: "modal title"
- })}
- variant="delete"
- >
- <DialogContentText
- dangerouslySetInnerHTML={{
- __html: i18n.t(
- "Are you sure you want to remove <strong>{{ collectionName }}</strong>?",
- {
- collectionName: maybe(
- () => data.collection.name
- ),
- context: "modal"
- }
- )
- }}
- />
- </ActionDialog>
- )}
- />
- <Route
- path={collectionImageRemovePath(":id")}
- render={({ match }) => (
- <ActionDialog
- confirmButtonState={imageRemoveTransitionState}
- onClose={() =>
- navigate(collectionUrl(id), true, true)
- }
- onConfirm={() =>
- updateCollection.mutate({
- id,
- input: {
- backgroundImage: null
- }
- })
- }
- open={!!match}
- title={i18n.t("Remove collection", {
- context: "modal title"
- })}
- variant="delete"
- >
- <DialogContentText>
- {i18n.t(
- "Are you sure you want to remove collection's image?"
- )}
- </DialogContentText>
- </ActionDialog>
- )}
- />
- </>
- );
- }}
- </CollectionOperations>
- );
- }}
- </TypedCollectionDetailsQuery>
- );
- }}
- </Navigator>
- )}
- </Messages>
-);
+ }}
+ />
+ </ActionDialog>
+ <ActionDialog
+ confirmButtonState={imageRemoveTransitionState}
+ onClose={closeModal}
+ onConfirm={() =>
+ updateCollection.mutate({
+ id,
+ input: {
+ backgroundImage: null
+ }
+ })
+ }
+ open={params.action === "removeImage"}
+ title={i18n.t("Remove image", {
+ context: "modal title"
+ })}
+ variant="delete"
+ >
+ <DialogContentText>
+ {i18n.t(
+ "Are you sure you want to remove collection's image?"
+ )}
+ </DialogContentText>
+ </ActionDialog>
+ </>
+ );
+ }}
+ </CollectionOperations>
+ );
+ }}
+ </TypedCollectionDetailsQuery>
+ );
+};
export default CollectionDetails;