Skip to content

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

Simplify code

dominik-zeglen9411f109438c
diff --git a/saleor/static/dashboard-next/categories/index.tsx b/saleor/static/dashboard-next/categories/index.tsx
index c4bd014749..1373eed933 100644
--- a/saleor/static/dashboard-next/categories/index.tsx
+++ b/saleor/static/dashboard-next/categories/index.tsx
@@ -11,7 +11,7 @@ const CategoryPaginator: React.StatelessComponent<RouteComponentProps<any>> = ({
match
}) => {
const qs = parseQs(location.search.substr(1));
- return <CategoryDetails id={match.params.id} filters={qs} />;
+ return <CategoryDetails id={match.params.id} params={qs} />;
};
const SubcategoryCreateForm: React.StatelessComponent<
RouteComponentProps<any>
diff --git a/saleor/static/dashboard-next/categories/views/CategoryDetails.tsx b/saleor/static/dashboard-next/categories/views/CategoryDetails.tsx
index 6b69eb55bb..d4321b0a28 100644
--- a/saleor/static/dashboard-next/categories/views/CategoryDetails.tsx
+++ b/saleor/static/dashboard-next/categories/views/CategoryDetails.tsx
@@ -6,6 +6,11 @@ import ErrorMessageCard from "../../components/ErrorMessageCard";
import Navigator from "../../components/Navigator";
import { CategoryPropertiesQuery } from "../../gql-types";
import i18n from "../../i18n";
+import {
+ createPaginationData,
+ createPaginationState,
+ PageInfo
+} from "../../misc";
import { productAddUrl, productUrl } from "../../products";
import { categoryAddUrl, categoryEditUrl, categoryShowUrl } from "../index";
import {
@@ -65,92 +70,75 @@ interface CategoryPaginationProviderProps {
props: {
data: CategoryPropertiesQuery;
loading: boolean;
- fetchNextPage();
- fetchPreviousPage();
+ pageInfo: PageInfo;
+ loadNextPage: () => void;
+ loadPreviousPage: () => void;
}
) => React.ReactElement<any>)
| React.ReactNode;
id?: string;
+ navigate: (url: string, push: boolean) => void;
+ params: any;
}
const CategoryPaginationProvider: React.StatelessComponent<
CategoryPaginationProviderProps
-> = ({ children, id }) => (
- <TypedCategoryPropertiesQuery
- query={categoryPropertiesQuery}
- variables={{ id, first: 12 }}
- fetchPolicy="network-only"
- >
- {({ loading, error, data, fetchMore }) => {
- if (error) {
- return (
- <ErrorMessageCard
- message={i18n.t("Unable to find a matching category.")}
- />
+> = ({ children, id, navigate, params }) => {
+ const PAGINATE_BY = 20;
+ const paginationState = createPaginationState(PAGINATE_BY, params);
+ return (
+ <TypedCategoryPropertiesQuery
+ query={categoryPropertiesQuery}
+ variables={{ id, ...paginationState }}
+ fetchPolicy="network-only"
+ >
+ {({ loading, error, data }) => {
+ if (error) {
+ return (
+ <ErrorMessageCard
+ message={i18n.t("Unable to find a matching category.")}
+ />
+ );
+ }
+ const {
+ loadNextPage,
+ loadPreviousPage,
+ pageInfo
+ } = createPaginationData(
+ navigate,
+ paginationState,
+ categoryShowUrl(id),
+ data && data.category && data.category.products
+ ? data.category.products.pageInfo
+ : undefined,
+ loading
);
- }
- const { category } = data;
- const updatePageInfo = (
- results: CategoryPropertiesQuery,
- overrides: Partial<
- CategoryPropertiesQuery["category"]["products"]["pageInfo"]
- >
- ) => ({
- ...results,
- category: {
- ...results.category,
- products: {
- ...results.category.products,
- pageInfo: {
- ...results.category.products.pageInfo,
- ...overrides
- }
- }
+
+ if (typeof children === "function") {
+ return children({
+ data,
+ loadNextPage,
+ loadPreviousPage,
+ loading,
+ pageInfo
+ });
}
- });
- const fetchNextPage = () => {
- return fetchMore({
- updateQuery: (previousResult, { fetchMoreResult }) =>
- updatePageInfo(fetchMoreResult, {
- hasPreviousPage: true
- }),
- variables: {
- after: category.products.pageInfo.endCursor,
- first: 12
- }
- });
- };
- const fetchPreviousPage = () => {
- return fetchMore({
- updateQuery: (previousResult, { fetchMoreResult }) =>
- updatePageInfo(fetchMoreResult, {
- hasNextPage: true
- }),
- variables: {
- before: category.products.pageInfo.startCursor,
- first: undefined,
- last: 12
- }
- });
- };
- if (typeof children === "function") {
- return children({ data, loading, fetchNextPage, fetchPreviousPage });
- }
- if (React.Children.count(children) > 0) {
- return React.Children.only(children);
- }
- return null;
- }}
- </TypedCategoryPropertiesQuery>
-);
+ if (React.Children.count(children) > 0) {
+ return React.Children.only(children);
+ }
+ return null;
+ }}
+ </TypedCategoryPropertiesQuery>
+ );
+};
interface CategoryDetailsProps {
- filters: any;
+ params: any;
id: string;
}
const CategoryDetails: React.StatelessComponent<CategoryDetailsProps> = ({
- filters,
+ params,
id
}) => {
if (id) {
@@ -158,8 +146,18 @@ const CategoryDetails: React.StatelessComponent<CategoryDetailsProps> = ({
<Navigator>
{navigate => {
return (
- <CategoryPaginationProvider id={id}>
- {({ data, loading, fetchNextPage, fetchPreviousPage }) => {
+ <CategoryPaginationProvider
+ id={id}
+ params={params}
+ navigate={navigate}
+ >
+ {({
+ data,
+ loading,
+ loadNextPage,
+ loadPreviousPage,
+ pageInfo
+ }) => {
return (
<CategoryDeleteProvider category={data.category}>
{deleteCategory => (
@@ -189,13 +187,9 @@ const CategoryDetails: React.StatelessComponent<CategoryDetailsProps> = ({
onEdit={() => navigate(categoryEditUrl(id))}
onProductClick={(id: string) => () =>
navigate(productUrl(id))}
- pageInfo={
- data && data.category && data.category.products
- ? data.category.products.pageInfo
- : undefined
- }
- onNextPage={fetchNextPage}
- onPreviousPage={fetchPreviousPage}
+ pageInfo={pageInfo}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
/>
)}
</CategoryDeleteProvider>
@@ -231,8 +225,7 @@ const CategoryDetails: React.StatelessComponent<CategoryDetailsProps> = ({
onBack={() => window.history.back()}
onCategoryClick={(id: string) => () =>
navigate(categoryShowUrl(id))}
- onProductClick={(id: string) => () =>
- navigate(productUrl(id))}
+ onProductClick={(id: string) => () => navigate(productUrl(id))}
/>
)}
</Navigator>
diff --git a/saleor/static/dashboard-next/misc.ts b/saleor/static/dashboard-next/misc.ts
index ba9a674b3f..4f32ba9653 100644
--- a/saleor/static/dashboard-next/misc.ts
+++ b/saleor/static/dashboard-next/misc.ts
@@ -1,7 +1,86 @@
-import { parse, stringify } from "qs";
+import { stringify } from "querystring";
-export function createQueryString(currentQs, newData) {
- const currentData = parse(currentQs.substr(1));
- const data = stringify({ ...currentData, ...newData });
- return `?${data}`;
+export interface PageInfo {
+ endCursor: string;
+ hasNextPage: boolean;
+ hasPreviousPage: boolean;
+ startCursor: string;
+}
+interface PaginationState {
+ after?: string;
+ before?: string;
+ first?: number;
+ last?: number;
+}
+
+interface QueryString {
+ after?: string;
+ before?: string;
+}
+
+export function createPaginationState(
+ paginateBy: number,
+ queryString: QueryString
+): PaginationState {
+ return queryString && (queryString.before || queryString.after)
+ ? queryString.after
+ ? {
+ after: queryString.after,
+ first: paginateBy
+ }
+ : {
+ before: queryString.before,
+ last: paginateBy
+ }
+ : {
+ first: paginateBy
+ };
+}
+
+export function createPaginationData(
+ navigate: ((url: string, push: boolean) => void),
+ paginationState: PaginationState,
+ url: string,
+ pageInfo: PageInfo,
+ loading
+) {
+ const loadNextPage = () => {
+ if (loading) {
+ return;
+ }
+ return navigate(
+ url +
+ "?" +
+ stringify({
+ after: pageInfo.endCursor
+ }),
+ true
+ );
+ };
+ const loadPreviousPage = () => {
+ if (loading) {
+ return;
+ }
+ return navigate(
+ url +
+ "?" +
+ stringify({
+ before: pageInfo.startCursor
+ }),
+ true
+ );
+ };
+ const newPageInfo = pageInfo
+ ? {
+ ...pageInfo,
+ hasNextPage: !!paginationState.before || pageInfo.hasNextPage,
+ hasPreviousPage: !!paginationState.after || pageInfo.hasPreviousPage
+ }
+ : undefined;
+
+ return {
+ loadNextPage,
+ loadPreviousPage,
+ pageInfo: newPageInfo
+ };
}
diff --git a/saleor/static/dashboard-next/pages/views/PageList.tsx b/saleor/static/dashboard-next/pages/views/PageList.tsx
index 7545b98d7b..c600481aba 100644
--- a/saleor/static/dashboard-next/pages/views/PageList.tsx
+++ b/saleor/static/dashboard-next/pages/views/PageList.tsx
@@ -4,6 +4,7 @@ import * as React from "react";
import ErrorMessageCard from "../../components/ErrorMessageCard";
import Navigator from "../../components/Navigator";
import i18n from "../../i18n";
+import { createPaginationData, createPaginationState } from "../../misc";
import PageListPage from "../components/PageListPage/PageListPage";
import {
pageAddUrl,
@@ -31,20 +32,7 @@ export class PageList extends React.Component<PageListProps, PageListState> {
render() {
const { params } = this.props;
const PAGINATE_BY = 20;
- const paginationState =
- params && (params.before || params.after)
- ? params.after
- ? {
- after: params.after,
- first: PAGINATE_BY
- }
- : {
- before: params.before,
- last: PAGINATE_BY
- }
- : {
- first: PAGINATE_BY
- };
+ const paginationState = createPaginationState(PAGINATE_BY, params);
return (
<Navigator>
{navigate => (
@@ -62,52 +50,23 @@ export class PageList extends React.Component<PageListProps, PageListState> {
);
}
- const loadNextPage = () => {
- if (loading) {
- return;
- }
- return navigate(
- pageListUrl +
- "?" +
- stringify({
- after: data.pages.pageInfo.endCursor
- }),
- true
- );
- };
- const loadPreviousPage = () => {
- if (loading) {
- return;
- }
- return navigate(
- pageListUrl +
- "?" +
- stringify({
- before: data.pages.pageInfo.startCursor
- }),
- true
- );
- };
- const pageInfo =
- data && data.pages && data.pages.pageInfo
- ? {
- ...data.pages.pageInfo,
- hasNextPage:
- !!paginationState.before ||
- data.pages.pageInfo.hasNextPage,
- hasPreviousPage:
- !!paginationState.after ||
- data.pages.pageInfo.hasPreviousPage
- }
- : undefined;
+ const {
+ loadNextPage,
+ loadPreviousPage,
+ pageInfo
+ } = createPaginationData(
+ navigate,
+ paginationState,
+ pageListUrl,
+ data && data.pages ? data.pages.pageInfo : undefined,
+ loading
+ );
return (
<Navigator>
{navigate => {
const handleEditClick = (id: string) => () =>
navigate(pageEditUrl(id));
- const handleShowPageClick = (slug: string) => () =>
- window.open(pageStorefrontUrl(slug));
return (
<PageListPage
disabled={loading}
diff --git a/saleor/static/dashboard-next/products/views/ProductList.tsx b/saleor/static/dashboard-next/products/views/ProductList.tsx
index b7b164ec11..230cb5fc27 100644
--- a/saleor/static/dashboard-next/products/views/ProductList.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductList.tsx
@@ -1,10 +1,10 @@
import { withStyles } from "@material-ui/core/styles";
-import { stringify } from "querystring";
import * as React from "react";
import { productAddUrl, productListUrl, productUrl } from "..";
import ErrorMessageCard from "../../components/ErrorMessageCard";
import Navigator from "../../components/Navigator";
+import { createPaginationData, createPaginationState } from "../../misc";
import ProductListCard from "../components/ProductListCard";
import { productListQuery, TypedProductListQuery } from "../queries";
@@ -29,68 +29,28 @@ export const ProductList = decorate<ProductListProps>(({ classes, params }) => (
<Navigator>
{navigate => {
const PAGINATE_BY = 20;
- const paginationState =
- params && (params.before || params.after)
- ? params.after
- ? {
- after: params.after,
- first: PAGINATE_BY
- }
- : {
- before: params.before,
- last: PAGINATE_BY
- }
- : {
- first: PAGINATE_BY
- };
+ const paginationState = createPaginationState(PAGINATE_BY, params);
return (
<TypedProductListQuery
query={productListQuery}
variables={paginationState}
fetchPolicy="network-only"
>
- {({ data, loading, error, fetchMore }) => {
+ {({ data, loading, error }) => {
if (error) {
return <ErrorMessageCard message="Something went wrong" />;
}
- const loadNextPage = () => {
- if (loading) {
- return;
- }
- return navigate(
- productListUrl +
- "?" +
- stringify({
- after: data.products.pageInfo.endCursor
- }),
- true
- );
- };
- const loadPreviousPage = () => {
- if (loading) {
- return;
- }
- return navigate(
- productListUrl +
- "?" +
- stringify({
- before: data.products.pageInfo.startCursor
- }),
- true
- );
- };
- const pageInfo =
- data && data.products && data.products.pageInfo
- ? {
- ...data.products.pageInfo,
- hasNextPage:
- !!paginationState.before ||
- data.products.pageInfo.hasNextPage,
- hasPreviousPage:
- !!paginationState.after ||
- data.products.pageInfo.hasPreviousPage
- }
- : undefined;
+ const {
+ loadNextPage,
+ loadPreviousPage,
+ pageInfo
+ } = createPaginationData(
+ navigate,
+ paginationState,
+ productListUrl,
+ data && data.products ? data.products.pageInfo : undefined,
+ loading
+ );
return (
<>
<div>