Skip to content

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

Move category delete dialog path to qs

dominik-zeglen82b5ca75b465
diff --git a/saleor/static/dashboard-next/categories/index.tsx b/saleor/static/dashboard-next/categories/index.tsx
index 1aad88dd04..100593eccb 100644
--- a/saleor/static/dashboard-next/categories/index.tsx
+++ b/saleor/static/dashboard-next/categories/index.tsx
@@ -3,12 +3,14 @@ import * as React from "react";
import { Route, RouteComponentProps, Switch } from "react-router-dom";
import { WindowTitle } from "../components/WindowTitle";
import i18n from "../i18n";
-import { categoryAddPath, categoryListPath, categoryPath } from "./urls";
+import {
+ categoryAddPath,
+ categoryListPath,
+ categoryPath,
+ CategoryUrlQueryParams
+} from "./urls";
import { CategoryCreateView } from "./views/CategoryCreate";
-import CategoryDetailsView, {
- CategoryDetailsQueryParams,
- getActiveTab
-} from "./views/CategoryDetails";
+import CategoryDetailsView, { getActiveTab } from "./views/CategoryDetails";
import CategoryListComponent, {
CategoryListQueryParams
} from "./views/CategoryList";
@@ -20,10 +22,9 @@ const CategoryDetails: React.StatelessComponent<
RouteComponentProps<CategoryDetailsRouteParams>
> = ({ location, match }) => {
const qs = parseQs(location.search.substr(1));
- const params: CategoryDetailsQueryParams = {
- activeTab: getActiveTab(qs.activeTab),
- after: qs.after,
- before: qs.before
+ const params: CategoryUrlQueryParams = {
+ ...qs,
+ activeTab: getActiveTab(qs.activeTab)
};
return (
<CategoryDetailsView
diff --git a/saleor/static/dashboard-next/categories/urls.ts b/saleor/static/dashboard-next/categories/urls.ts
index 49af6028be..60e99eb72b 100644
--- a/saleor/static/dashboard-next/categories/urls.ts
+++ b/saleor/static/dashboard-next/categories/urls.ts
@@ -1,12 +1,20 @@
+import { stringify as stringifyQs } from "qs";
import * as urlJoin from "url-join";
+import { ActiveTab, Dialog, Pagination } from "../types";
+import { CategoryPageTab } from "./components/CategoryUpdatePage";
+
const categorySectionUrl = "/categories/";
export const categoryListPath = categorySectionUrl;
export const categoryListUrl = categorySectionUrl;
export const categoryPath = (id: string) => urlJoin(categorySectionUrl, id);
-export const categoryUrl = (id: string) => categoryPath(encodeURIComponent(id));
+export type CategoryUrlQueryParams = Dialog<"delete"> &
+ Pagination &
+ ActiveTab<CategoryPageTab>;
+export const categoryUrl = (id: string, params?: CategoryUrlQueryParams) =>
+ categoryPath(encodeURIComponent(id)) + "?" + stringifyQs(params);
export const categoryAddPath = (parentId?: string) => {
if (parentId) {
diff --git a/saleor/static/dashboard-next/categories/views/CategoryDetails.tsx b/saleor/static/dashboard-next/categories/views/CategoryDetails.tsx
new file mode 100644
index 0000000000..1db0f093fb
--- /dev/null
+++ b/saleor/static/dashboard-next/categories/views/CategoryDetails.tsx
@@ -0,0 +1,234 @@
+import DialogContentText from "@material-ui/core/DialogContentText";
+import { stringify as stringifyQs } from "qs";
+import * as React from "react";
+
+import ActionDialog from "../../components/ActionDialog";
+import { createPaginationState, Paginator } from "../../components/Paginator";
+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 { productAddUrl, productUrl } from "../../products/urls";
+import { CategoryInput } from "../../types/globalTypes";
+import {
+ CategoryPageTab,
+ CategoryUpdatePage
+} from "../components/CategoryUpdatePage/CategoryUpdatePage";
+import {
+ TypedCategoryDeleteMutation,
+ TypedCategoryUpdateMutation
+} from "../mutations";
+import { TypedCategoryDetailsQuery } from "../queries";
+import { CategoryDelete } from "../types/CategoryDelete";
+import { CategoryUpdate } from "../types/CategoryUpdate";
+import {
+ categoryAddUrl,
+ categoryListUrl,
+ categoryUrl,
+ CategoryUrlQueryParams
+} from "../urls";
+
+export interface CategoryDetailsProps {
+ params: CategoryUrlQueryParams;
+ id: string;
+}
+
+export function getActiveTab(tabName: string): CategoryPageTab {
+ return tabName === CategoryPageTab.products
+ ? CategoryPageTab.products
+ : CategoryPageTab.categories;
+}
+
+const PAGINATE_BY = 20;
+
+export const CategoryDetails: React.StatelessComponent<
+ CategoryDetailsProps
+> = ({ id, params }) => {
+ const navigate = useNavigator();
+ const notify = useNotifier();
+
+ const handleCategoryDelete = (data: CategoryDelete) => {
+ if (data.categoryDelete.errors.length === 0) {
+ notify({
+ text: i18n.t("Category deleted", {
+ context: "notification"
+ })
+ });
+ navigate(categoryListUrl);
+ }
+ };
+ const handleCategoryUpdate = (data: CategoryUpdate) => {
+ if (data.categoryUpdate.errors.length > 0) {
+ const backgroundImageError = data.categoryUpdate.errors.find(
+ error => error.field === ("backgroundImage" as keyof CategoryInput)
+ );
+ if (backgroundImageError) {
+ notify({
+ text: backgroundImageError.message
+ });
+ }
+ }
+ };
+
+ const changeTab = (tabName: CategoryPageTab) =>
+ navigate(
+ "?" +
+ stringifyQs({
+ activeTab: tabName
+ })
+ );
+
+ return (
+ <TypedCategoryDeleteMutation onCompleted={handleCategoryDelete}>
+ {(deleteCategory, deleteResult) => (
+ <TypedCategoryUpdateMutation onCompleted={handleCategoryUpdate}>
+ {(updateCategory, updateResult) => {
+ const paginationState = createPaginationState(PAGINATE_BY, params);
+ const formTransitionState = getMutationState(
+ updateResult.called,
+ updateResult.loading,
+ maybe(() => updateResult.data.categoryUpdate.errors)
+ );
+ const removeDialogTransitionState = getMutationState(
+ deleteResult.called,
+ deleteResult.loading,
+ maybe(() => deleteResult.data.categoryDelete.errors)
+ );
+ return (
+ <TypedCategoryDetailsQuery
+ displayLoader
+ variables={{ ...paginationState, id }}
+ require={["category"]}
+ >
+ {({ data, loading }) => (
+ <>
+ <WindowTitle title={maybe(() => data.category.name)} />
+ <Paginator
+ pageInfo={maybe(() => data.category.products.pageInfo)}
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({ loadNextPage, loadPreviousPage, pageInfo }) => (
+ <CategoryUpdatePage
+ changeTab={changeTab}
+ currentTab={params.activeTab}
+ category={maybe(() => data.category)}
+ disabled={loading}
+ errors={maybe(
+ () => updateResult.data.categoryUpdate.errors
+ )}
+ onAddCategory={() => navigate(categoryAddUrl(id))}
+ onAddProduct={() => navigate(productAddUrl)}
+ onBack={() =>
+ navigate(
+ maybe(
+ () => categoryUrl(data.category.parent.id),
+ categoryListUrl
+ )
+ )
+ }
+ onCategoryClick={id => () =>
+ navigate(categoryUrl(id))}
+ onDelete={() =>
+ navigate(
+ categoryUrl(id, {
+ ...params,
+ action: "delete"
+ })
+ )
+ }
+ onImageDelete={() =>
+ updateCategory({
+ variables: {
+ id,
+ input: {
+ backgroundImage: null
+ }
+ }
+ })
+ }
+ onImageUpload={file =>
+ updateCategory({
+ variables: {
+ id,
+ input: {
+ backgroundImage: file
+ }
+ }
+ })
+ }
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ pageInfo={pageInfo}
+ onProductClick={id => () => navigate(productUrl(id))}
+ onSubmit={formData =>
+ updateCategory({
+ variables: {
+ id,
+ input: {
+ backgroundImageAlt:
+ formData.backgroundImageAlt,
+ descriptionJson: JSON.stringify(
+ formData.description
+ ),
+ name: formData.name,
+ seo: {
+ description: formData.seoDescription,
+ title: formData.seoTitle
+ }
+ }
+ }
+ })
+ }
+ products={maybe(() =>
+ data.category.products.edges.map(edge => edge.node)
+ )}
+ saveButtonBarState={formTransitionState}
+ subcategories={maybe(() =>
+ data.category.children.edges.map(edge => edge.node)
+ )}
+ />
+ )}
+ </Paginator>
+ <ActionDialog
+ confirmButtonState={removeDialogTransitionState}
+ onClose={() =>
+ navigate(
+ categoryUrl(id, {
+ ...params,
+ action: undefined
+ }),
+ true
+ )
+ }
+ onConfirm={() => deleteCategory({ variables: { id } })}
+ open={params.action === "delete"}
+ title={i18n.t("Delete category", {
+ context: "modal title"
+ })}
+ variant="delete"
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ categoryName }}</strong>?",
+ {
+ categoryName: maybe(() => data.category.name),
+ context: "modal message"
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ </>
+ )}
+ </TypedCategoryDetailsQuery>
+ );
+ }}
+ </TypedCategoryUpdateMutation>
+ )}
+ </TypedCategoryDeleteMutation>
+ );
+};
+export default CategoryDetails;
diff --git a/saleor/static/dashboard-next/categories/views/CategoryDetails/index.tsx b/saleor/static/dashboard-next/categories/views/CategoryDetails/index.tsx
deleted file mode 100644
index bf20acc98a..0000000000
--- a/saleor/static/dashboard-next/categories/views/CategoryDetails/index.tsx
+++ /dev/null
@@ -1,266 +0,0 @@
-import DialogContentText from "@material-ui/core/DialogContentText";
-import { stringify as stringifyQs } from "qs";
-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 {
- createPaginationState,
- Paginator
-} from "../../../components/Paginator";
-import { WindowTitle } from "../../../components/WindowTitle";
-import i18n from "../../../i18n";
-import { getMutationState, maybe } from "../../../misc";
-import { productAddUrl, productUrl } from "../../../products/urls";
-import { CategoryInput } from "../../../types/globalTypes";
-import {
- CategoryPageTab,
- CategoryUpdatePage
-} from "../../components/CategoryUpdatePage/CategoryUpdatePage";
-import {
- TypedCategoryDeleteMutation,
- TypedCategoryUpdateMutation
-} from "../../mutations";
-import { TypedCategoryDetailsQuery } from "../../queries";
-import { CategoryDelete } from "../../types/CategoryDelete";
-import { CategoryUpdate } from "../../types/CategoryUpdate";
-import { categoryAddUrl, categoryListUrl, categoryUrl } from "../../urls";
-import { categoryDeletePath, categoryDeleteUrl } from "./urls";
-
-export type CategoryDetailsQueryParams = Partial<{
- activeTab: CategoryPageTab;
- after: string;
- before: string;
-}>;
-export interface CategoryDetailsProps {
- params: CategoryDetailsQueryParams;
- id: string;
-}
-
-export function getActiveTab(tabName: string): CategoryPageTab {
- return tabName === CategoryPageTab.products
- ? CategoryPageTab.products
- : CategoryPageTab.categories;
-}
-
-const PAGINATE_BY = 20;
-
-export const CategoryDetails: React.StatelessComponent<
- CategoryDetailsProps
-> = ({ id, params }) => (
- <Navigator>
- {navigate => (
- <Messages>
- {pushMessage => {
- const handleCategoryDelete = (data: CategoryDelete) => {
- if (data.categoryDelete.errors.length === 0) {
- pushMessage({
- text: i18n.t("Category deleted", {
- context: "notification"
- })
- });
- navigate(categoryListUrl);
- }
- };
- const handleCategoryUpdate = (data: CategoryUpdate) => {
- if (data.categoryUpdate.errors.length > 0) {
- const backgroundImageError = data.categoryUpdate.errors.find(
- error =>
- error.field === ("backgroundImage" as keyof CategoryInput)
- );
- if (backgroundImageError) {
- pushMessage({
- text: backgroundImageError.message
- });
- }
- }
- };
-
- const changeTab = (tabName: CategoryPageTab) =>
- navigate(
- "?" +
- stringifyQs({
- activeTab: tabName
- })
- );
-
- return (
- <TypedCategoryDeleteMutation onCompleted={handleCategoryDelete}>
- {(deleteCategory, deleteResult) => (
- <TypedCategoryUpdateMutation onCompleted={handleCategoryUpdate}>
- {(updateCategory, updateResult) => {
- const paginationState = createPaginationState(
- PAGINATE_BY,
- params
- );
- const formTransitionState = getMutationState(
- updateResult.called,
- updateResult.loading,
- maybe(() => updateResult.data.categoryUpdate.errors)
- );
- const removeDialogTransitionState = getMutationState(
- deleteResult.called,
- deleteResult.loading,
- maybe(() => deleteResult.data.categoryDelete.errors)
- );
- return (
- <TypedCategoryDetailsQuery
- displayLoader
- variables={{ ...paginationState, id }}
- require={["category"]}
- >
- {({ data, loading }) => (
- <>
- <WindowTitle
- title={maybe(() => data.category.name)}
- />
- <Paginator
- pageInfo={maybe(
- () => data.category.products.pageInfo
- )}
- paginationState={paginationState}
- queryString={params}
- >
- {({
- loadNextPage,
- loadPreviousPage,
- pageInfo
- }) => (
- <CategoryUpdatePage
- changeTab={changeTab}
- currentTab={params.activeTab}
- category={maybe(() => data.category)}
- disabled={loading}
- errors={maybe(
- () =>
- updateResult.data.categoryUpdate.errors
- )}
- onAddCategory={() =>
- navigate(categoryAddUrl(id))
- }
- onAddProduct={() => navigate(productAddUrl)}
- onBack={() =>
- navigate(
- maybe(
- () =>
- categoryUrl(data.category.parent.id),
- categoryListUrl
- )
- )
- }
- onCategoryClick={id => () =>
- navigate(categoryUrl(id))}
- onDelete={() =>
- navigate(categoryDeleteUrl(id))
- }
- onImageDelete={() =>
- updateCategory({
- variables: {
- id,
- input: {
- backgroundImage: null
- }
- }
- })
- }
- onImageUpload={file =>
- updateCategory({
- variables: {
- id,
- input: {
- backgroundImage: file
- }
- }
- })
- }
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- pageInfo={pageInfo}
- onProductClick={id => () =>
- navigate(productUrl(id))}
- onSubmit={formData =>
- updateCategory({
- variables: {
- id,
- input: {
- backgroundImageAlt:
- formData.backgroundImageAlt,
- descriptionJson: JSON.stringify(
- formData.description
- ),
- name: formData.name,
- seo: {
- description:
- formData.seoDescription,
- title: formData.seoTitle
- }
- }
- }
- })
- }
- products={maybe(() =>
- data.category.products.edges.map(
- edge => edge.node
- )
- )}
- saveButtonBarState={formTransitionState}
- subcategories={maybe(() =>
- data.category.children.edges.map(
- edge => edge.node
- )
- )}
- />
- )}
- </Paginator>
- <Route
- path={categoryDeletePath(":id")}
- render={({ match }) => (
- <ActionDialog
- confirmButtonState={
- removeDialogTransitionState
- }
- onClose={() =>
- navigate(categoryUrl(id), true)
- }
- onConfirm={() =>
- deleteCategory({ variables: { id } })
- }
- open={!!match}
- title={i18n.t("Delete category", {
- context: "modal title"
- })}
- variant="delete"
- >
- <DialogContentText
- dangerouslySetInnerHTML={{
- __html: i18n.t(
- "Are you sure you want to remove <strong>{{ categoryName }}</strong>?",
- {
- categoryName: maybe(
- () => data.category.name
- ),
- context: "modal message"
- }
- )
- }}
- />
- </ActionDialog>
- )}
- />
- </>
- )}
- </TypedCategoryDetailsQuery>
- );
- }}
- </TypedCategoryUpdateMutation>
- )}
- </TypedCategoryDeleteMutation>
- );
- }}
- </Messages>
- )}
- </Navigator>
-);
-export default CategoryDetails;
diff --git a/saleor/static/dashboard-next/categories/views/CategoryDetails/urls.ts b/saleor/static/dashboard-next/categories/views/CategoryDetails/urls.ts
deleted file mode 100644
index 83527ff9e1..0000000000
--- a/saleor/static/dashboard-next/categories/views/CategoryDetails/urls.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import * as urlJoin from "url-join";
-import { categoryPath } from "../../urls";
-
-export const categoryDeletePath = (id: string) =>
- urlJoin(categoryPath(id), "delete");
-export const categoryDeleteUrl = (id: string) =>
- categoryDeletePath(encodeURIComponent(id));
diff --git a/saleor/static/dashboard-next/types.ts b/saleor/static/dashboard-next/types.ts
index 0d645fe1bf..343e66082c 100644
--- a/saleor/static/dashboard-next/types.ts
+++ b/saleor/static/dashboard-next/types.ts
@@ -33,3 +33,10 @@ export type Pagination = Partial<{
after: string;
before: string;
}>;
+
+export type Dialog<TDialog extends string> = Partial<{
+ action: TDialog;
+}>;
+export type ActiveTab<TTab extends string> = Partial<{
+ activeTab: TTab;
+}>;