Skip to content

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

Use dedicated component to pagination

dominik-zeglen999282a3d422
diff --git a/saleor/static/dashboard-next/categories/index.tsx b/saleor/static/dashboard-next/categories/index.tsx
index e1753da835..ee3db149e1 100644
--- a/saleor/static/dashboard-next/categories/index.tsx
+++ b/saleor/static/dashboard-next/categories/index.tsx
@@ -2,7 +2,9 @@ import { parse as parseQs } from "qs";
import * as React from "react";
import { Route, RouteComponentProps, Switch } from "react-router-dom";
import { CategoryCreateView } from "./views/CategoryCreate";
-import CategoryDetailsView from "./views/CategoryDetails";
+import CategoryDetailsView, {
+ CategoryDetailsQueryParams
+} from "./views/CategoryDetails";
import CategoryList from "./views/CategoryList";
interface CategoryDetailsRouteParams {
@@ -12,7 +14,7 @@ const CategoryDetails: React.StatelessComponent<
RouteComponentProps<CategoryDetailsRouteParams>
> = ({ location, match }) => {
const qs = parseQs(location.search.substr(1));
- const params = {
+ const params: CategoryDetailsQueryParams = {
after: qs.after,
before: qs.before
};
diff --git a/saleor/static/dashboard-next/categories/views/CategoryDetails/index.tsx b/saleor/static/dashboard-next/categories/views/CategoryDetails/index.tsx
index 0bd72b511e..5e32667441 100644
--- a/saleor/static/dashboard-next/categories/views/CategoryDetails/index.tsx
+++ b/saleor/static/dashboard-next/categories/views/CategoryDetails/index.tsx
@@ -6,12 +6,12 @@ import { categoryAddUrl, categoryListUrl, categoryUrl } from "../..";
import ActionDialog from "../../../components/ActionDialog";
import Messages from "../../../components/messages";
import Navigator from "../../../components/Navigator";
-import i18n from "../../../i18n";
import {
- createPaginationData,
createPaginationState,
- maybe
-} from "../../../misc";
+ Paginator
+} from "../../../components/Paginator";
+import i18n from "../../../i18n";
+import { maybe } from "../../../misc";
import { productAddUrl, productUrl } from "../../../products";
import { CategoryUpdatePage } from "../../components/CategoryUpdatePage/CategoryUpdatePage";
import {
@@ -23,12 +23,12 @@ import { CategoryDelete } from "../../types/CategoryDelete";
import { CategoryUpdate } from "../../types/CategoryUpdate";
import { categoryDeleteUrl } from "./urls";
-export interface QueryParams {
- after?: string;
- before?: string;
-}
+export type CategoryDetailsQueryParams = Partial<{
+ after: string;
+ before: string;
+}>;
export interface CategoryDetailsProps {
- params: QueryParams;
+ params: CategoryDetailsQueryParams;
id: string;
}
@@ -80,121 +80,132 @@ export const CategoryDetails: React.StatelessComponent<
<TypedCategoryDetailsQuery
variables={{ ...paginationState, id }}
>
- {({ data, loading }) => {
- const paginationData = createPaginationData(
- navigate,
- paginationState,
- categoryUrl(id),
- maybe(() => data.category.products.pageInfo),
- loading
- );
- return (
- <>
- <CategoryUpdatePage
- category={maybe(() => data.category)}
- disabled={loading}
- errors={maybe(
- () => updateResult.data.categoryUpdate.errors
- )}
- placeholderImage={""}
- onAddCategory={() =>
- navigate(
- categoryAddUrl(encodeURIComponent(id))
- )
- }
- onAddProduct={() => navigate(productAddUrl)}
- onBack={() =>
- navigate(
- categoryUrl(
- maybe(() =>
- encodeURIComponent(
- data.category.parent.id
+ {({ data, loading }) => (
+ <>
+ <Paginator
+ pageInfo={maybe(
+ () => data.category.products.pageInfo
+ )}
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({
+ loadNextPage,
+ loadPreviousPage,
+ pageInfo
+ }) => (
+ <CategoryUpdatePage
+ category={maybe(() => data.category)}
+ disabled={loading}
+ errors={maybe(
+ () =>
+ updateResult.data.categoryUpdate.errors
+ )}
+ placeholderImage={""}
+ onAddCategory={() =>
+ navigate(
+ categoryAddUrl(encodeURIComponent(id))
+ )
+ }
+ onAddProduct={() => navigate(productAddUrl)}
+ onBack={() =>
+ navigate(
+ categoryUrl(
+ maybe(() =>
+ encodeURIComponent(
+ data.category.parent.id
+ )
)
)
)
- )
- }
- onCategoryClick={id => () =>
- navigate(categoryUrl(encodeURIComponent(id)))}
- onDelete={() =>
- navigate(
- categoryDeleteUrl(encodeURIComponent(id))
- )
- }
- onImageDelete={() => undefined}
- onImageUpload={() => undefined}
- onNextPage={paginationData.loadNextPage}
- onPreviousPage={paginationData.loadPreviousPage}
- pageInfo={paginationData.pageInfo}
- onProductClick={id => () =>
- navigate(productUrl(encodeURIComponent(id)))}
- onSubmit={formData =>
- updateCategory({
- variables: {
- id,
- input: {
- description: formData.description,
- name: formData.name,
- seo: {
- description: formData.seoDescription,
- title: formData.seoTitle
+ }
+ onCategoryClick={id => () =>
+ navigate(
+ categoryUrl(encodeURIComponent(id))
+ )}
+ onDelete={() =>
+ navigate(
+ categoryDeleteUrl(encodeURIComponent(id))
+ )
+ }
+ onImageDelete={() => undefined}
+ onImageUpload={() => undefined}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ pageInfo={pageInfo}
+ onProductClick={id => () =>
+ navigate(
+ productUrl(encodeURIComponent(id))
+ )}
+ onSubmit={formData =>
+ updateCategory({
+ variables: {
+ id,
+ input: {
+ description: formData.description,
+ name: formData.name,
+ seo: {
+ description:
+ formData.seoDescription,
+ title: formData.seoTitle
+ }
}
}
- }
- })
- }
- products={maybe(() =>
- data.category.products.edges.map(
- edge => edge.node
- )
- )}
- saveButtonBarState={
- loading ? "loading" : "default"
- }
- subcategories={maybe(() =>
- data.category.children.edges.map(
- edge => edge.node
- )
- )}
- />
- <Route
- path={categoryDeleteUrl(encodeURIComponent(id))}
- render={({ match }) => (
- <ActionDialog
- onClose={() =>
- navigate(
- categoryUrl(encodeURIComponent(id)),
- true
+ })
+ }
+ products={maybe(() =>
+ data.category.products.edges.map(
+ edge => edge.node
+ )
+ )}
+ saveButtonBarState={
+ loading ? "loading" : "default"
+ }
+ subcategories={maybe(() =>
+ data.category.children.edges.map(
+ edge => edge.node
+ )
+ )}
+ />
+ )}
+ </Paginator>
+ <Route
+ path={categoryDeleteUrl(encodeURIComponent(id))}
+ render={({ match }) => (
+ <ActionDialog
+ onClose={() =>
+ navigate(
+ categoryUrl(encodeURIComponent(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"
+ }
)
- }
- 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>
- )}
- />
- </>
- );
- }}
+ }}
+ />
+ </ActionDialog>
+ )}
+ />
+ </>
+ )}
</TypedCategoryDetailsQuery>
);
}}
diff --git a/saleor/static/dashboard-next/collections/index.tsx b/saleor/static/dashboard-next/collections/index.tsx
index 16b8aafc96..9ba4a6eb90 100644
--- a/saleor/static/dashboard-next/collections/index.tsx
+++ b/saleor/static/dashboard-next/collections/index.tsx
@@ -4,14 +4,18 @@ import { Route, RouteComponentProps, Switch } from "react-router-dom";
import { collectionAddUrl, collectionListUrl, collectionUrl } from "./urls";
import CollectionCreate from "./views/CollectionCreate";
-import CollectionDetailsView from "./views/CollectionDetails";
-import CollectionListView from "./views/CollectionList";
+import CollectionDetailsView, {
+ CollectionDetailsQueryParams
+} from "./views/CollectionDetails";
+import CollectionListView, {
+ CollectionListQueryParams
+} from "./views/CollectionList";
const CollectionList: React.StatelessComponent<RouteComponentProps<{}>> = ({
location
}) => {
const qs = parseQs(location.search.substr(1));
- const params = {
+ const params: CollectionListQueryParams = {
after: qs.after,
before: qs.before
};
@@ -25,7 +29,7 @@ const CollectionDetails: React.StatelessComponent<
RouteComponentProps<CollectionDetailsRouteProps>
> = ({ location, match }) => {
const qs = parseQs(location.search.substr(1));
- const params = {
+ const params: CollectionDetailsQueryParams = {
after: qs.after,
before: qs.before
};
diff --git a/saleor/static/dashboard-next/collections/views/CollectionDetails.tsx b/saleor/static/dashboard-next/collections/views/CollectionDetails.tsx
index f7c681ffc8..dadc29585f 100644
--- a/saleor/static/dashboard-next/collections/views/CollectionDetails.tsx
+++ b/saleor/static/dashboard-next/collections/views/CollectionDetails.tsx
@@ -5,8 +5,9 @@ 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 i18n from "../../i18n";
-import { createPaginationData, createPaginationState, maybe } from "../../misc";
+import { maybe } from "../../misc";
import { productUrl } from "../../products";
import CollectionAssignProductDialog from "../components/CollectionAssignProductDialog/CollectionAssignProductDialog";
import CollectionDetailsPage, {
@@ -27,12 +28,14 @@ import {
collectionUrl
} from "../urls";
+export type CollectionDetailsQueryParams = Partial<{
+ after: string;
+ before: string;
+}>;
+
interface CollectionDetailsProps {
id: string;
- params: {
- after?: string;
- before?: string;
- };
+ params: CollectionDetailsQueryParams;
}
const PAGINATE_BY = 20;
@@ -52,18 +55,6 @@ export const CollectionDetails: React.StatelessComponent<
variables={{ id, ...paginationState }}
>
{({ data, loading }) => {
- const {
- loadNextPage,
- loadPreviousPage,
- pageInfo
- } = createPaginationData(
- navigate,
- paginationState,
- collectionUrl(encodeURIComponent(id)),
- maybe(() => data.collection.products.pageInfo),
- loading
- );
-
const handleCollectionUpdate = (data: CollectionUpdate) => {
if (
data.collectionUpdate.errors === null ||
@@ -164,64 +155,82 @@ export const CollectionDetails: React.StatelessComponent<
};
return (
<>
- <CollectionDetailsPage
- onAdd={() =>
- navigate(
- collectionAddProductUrl(
- encodeURIComponent(id)
- ),
- false,
- true
- )
- }
- onBack={() => navigate(collectionListUrl)}
- disabled={loading}
- collection={maybe(() => data.collection)}
- isFeatured={maybe(
- () =>
- data.shop.homepageCollection.id ===
- data.collection.id,
- false
+ <Paginator
+ pageInfo={maybe(
+ () => data.collection.products.pageInfo
)}
- onCollectionRemove={() =>
- navigate(
- collectionRemoveUrl(encodeURIComponent(id)),
- false,
- true
- )
- }
- onImageDelete={() =>
- navigate(
- collectionImageRemoveUrl(
- encodeURIComponent(id)
- ),
- false,
- true
- )
- }
- onImageUpload={event =>
- updateCollection.mutate({
- id,
- input: {
- backgroundImage: event.target.files[0]
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({
+ loadNextPage,
+ loadPreviousPage,
+ pageInfo
+ }) => (
+ <CollectionDetailsPage
+ onAdd={() =>
+ navigate(
+ collectionAddProductUrl(
+ encodeURIComponent(id)
+ ),
+ false,
+ true
+ )
}
- })
- }
- onSubmit={handleSubmit}
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- pageInfo={pageInfo}
- onProductUnassign={(productId, event) => {
- event.stopPropagation();
- unassignProduct.mutate({
- collectionId: id,
- productId,
- ...paginationState
- });
- }}
- onRowClick={id => () =>
- navigate(productUrl(encodeURIComponent(id)))}
- />
+ onBack={() => navigate(collectionListUrl)}
+ disabled={loading}
+ collection={maybe(() => data.collection)}
+ isFeatured={maybe(
+ () =>
+ data.shop.homepageCollection.id ===
+ data.collection.id,
+ false
+ )}
+ onCollectionRemove={() =>
+ navigate(
+ collectionRemoveUrl(
+ encodeURIComponent(id)
+ ),
+ false,
+ true
+ )
+ }
+ onImageDelete={() =>
+ navigate(
+ collectionImageRemoveUrl(
+ encodeURIComponent(id)
+ ),
+ false,
+ true
+ )
+ }
+ onImageUpload={event =>
+ updateCollection.mutate({
+ id,
+ input: {
+ backgroundImage: event.target.files[0]
+ }
+ })
+ }
+ onSubmit={handleSubmit}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ pageInfo={pageInfo}
+ onProductUnassign={(productId, event) => {
+ event.stopPropagation();
+ unassignProduct.mutate({
+ collectionId: id,
+ productId,
+ ...paginationState
+ });
+ }}
+ onRowClick={id => () =>
+ navigate(
+ productUrl(encodeURIComponent(id))
+ )}
+ />
+ )}
+ </Paginator>
<Route
path={collectionAddProductUrl(
encodeURIComponent(id)
@@ -241,8 +250,8 @@ export const CollectionDetails: React.StatelessComponent<
onSubmit={product =>
assignProduct.mutate({
collectionId: id,
- productId: product.product.value,
- first: PAGINATE_BY
+ first: PAGINATE_BY,
+ productId: product.product.value
})
}
products={maybe(() =>
diff --git a/saleor/static/dashboard-next/collections/views/CollectionList.tsx b/saleor/static/dashboard-next/collections/views/CollectionList.tsx
index 52f5ba4452..47307c154d 100644
--- a/saleor/static/dashboard-next/collections/views/CollectionList.tsx
+++ b/saleor/static/dashboard-next/collections/views/CollectionList.tsx
@@ -1,16 +1,19 @@
import * as React from "react";
import Navigator from "../../components/Navigator";
-import { createPaginationData, createPaginationState, maybe } from "../../misc";
+import { createPaginationState, Paginator } from "../../components/Paginator";
+import { maybe } from "../../misc";
import CollectionListPage from "../components/CollectionListPage/CollectionListPage";
import { TypedCollectionListQuery } from "../queries";
-import { collectionAddUrl, collectionListUrl, collectionUrl } from "../urls";
+import { collectionAddUrl, collectionUrl } from "../urls";
+
+export type CollectionListQueryParams = Partial<{
+ after: string;
+ before: string;
+}>;
interface CollectionListProps {
- params: {
- after?: string;
- before?: string;
- };
+ params: CollectionListQueryParams;
}
const PAGINATE_BY = 20;
@@ -23,33 +26,28 @@ export const CollectionList: React.StatelessComponent<CollectionListProps> = ({
const paginationState = createPaginationState(PAGINATE_BY, params);
return (
<TypedCollectionListQuery variables={paginationState}>
- {({ data, loading }) => {
- const {
- loadNextPage,
- loadPreviousPage,
- pageInfo
- } = createPaginationData(
- navigate,
- paginationState,
- collectionListUrl,
- maybe(() => data.collections.pageInfo),
- loading
- );
- return (
- <CollectionListPage
- onAdd={() => navigate(collectionAddUrl)}
- disabled={loading}
- collections={maybe(() =>
- data.collections.edges.map(edge => edge.node)
- )}
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- pageInfo={pageInfo}
- onRowClick={id => () =>
- navigate(collectionUrl(encodeURIComponent(id)))}
- />
- );
- }}
+ {({ data, loading }) => (
+ <Paginator
+ pageInfo={maybe(() => data.collections.pageInfo)}
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({ loadNextPage, loadPreviousPage, pageInfo }) => (
+ <CollectionListPage
+ onAdd={() => navigate(collectionAddUrl)}
+ disabled={loading}
+ collections={maybe(() =>
+ data.collections.edges.map(edge => edge.node)
+ )}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ pageInfo={pageInfo}
+ onRowClick={id => () =>
+ navigate(collectionUrl(encodeURIComponent(id)))}
+ />
+ )}
+ </Paginator>
+ )}
</TypedCollectionListQuery>
);
}}
diff --git a/saleor/static/dashboard-next/customers/index.tsx b/saleor/static/dashboard-next/customers/index.tsx
index d0cd1db0a5..ca4c16e516 100644
--- a/saleor/static/dashboard-next/customers/index.tsx
+++ b/saleor/static/dashboard-next/customers/index.tsx
@@ -5,13 +5,15 @@ import { Route, RouteComponentProps, Switch } from "react-router-dom";
import { customerAddUrl, customerListUrl, customerUrl } from "./urls";
import CustomerCreateView from "./views/CustomerCreate";
import CustomerDetailsViewComponent from "./views/CustomerDetails";
-import CustomerListViewComponent from "./views/CustomerList";
+import CustomerListViewComponent, {
+ CustomerListQueryParams
+} from "./views/CustomerList";
const CustomerListView: React.StatelessComponent<RouteComponentProps<{}>> = ({
location
}) => {
const qs = parseQs(location.search.substr(1));
- const params = {
+ const params: CustomerListQueryParams = {
after: qs.after,
before: qs.before
};
diff --git a/saleor/static/dashboard-next/customers/views/CustomerList.tsx b/saleor/static/dashboard-next/customers/views/CustomerList.tsx
index 43fdd051b1..9439f44638 100644
--- a/saleor/static/dashboard-next/customers/views/CustomerList.tsx
+++ b/saleor/static/dashboard-next/customers/views/CustomerList.tsx
@@ -1,18 +1,21 @@
import * as React from "react";
import Navigator from "../../components/Navigator";
-import { createPaginationData, createPaginationState, maybe } from "../../misc";
+import { createPaginationState, Paginator } from "../../components/Paginator";
+import { maybe } from "../../misc";
import CustomerListPage from "../components/CustomerListPage";
import { TypedCustomerListQuery } from "../queries";
-import { customerAddUrl, customerListUrl, customerUrl } from "../urls";
+import { customerAddUrl, customerUrl } from "../urls";
const PAGINATE_BY = 20;
+export type CustomerListQueryParams = Partial<{
+ after: string;
+ before: string;
+}>;
+
interface CustomerListProps {
- params: {
- after?: string;
- before?: string;
- };
+ params: CustomerListQueryParams;
}
export const CustomerList: React.StatelessComponent<CustomerListProps> = ({
@@ -23,33 +26,28 @@ export const CustomerList: React.StatelessComponent<CustomerListProps> = ({
const paginationState = createPaginationState(PAGINATE_BY, params);
return (
<TypedCustomerListQuery variables={paginationState}>
- {({ data, loading }) => {
- const {
- loadNextPage,
- loadPreviousPage,
- pageInfo
- } = createPaginationData(
- navigate,
- paginationState,
- customerListUrl,
- maybe(() => data.customers.pageInfo),
- loading
- );
- return (
- <CustomerListPage
- customers={maybe(() =>
- data.customers.edges.map(edge => edge.node)
- )}
- disabled={loading}
- pageInfo={pageInfo}
- onAdd={() => navigate(customerAddUrl)}
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- onRowClick={id => () =>
- navigate(customerUrl(encodeURIComponent(id)))}
- />
- );
- }}
+ {({ data, loading }) => (
+ <Paginator
+ pageInfo={maybe(() => data.customers.pageInfo)}
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({ loadNextPage, loadPreviousPage, pageInfo }) => (
+ <CustomerListPage
+ customers={maybe(() =>
+ data.customers.edges.map(edge => edge.node)
+ )}
+ disabled={loading}
+ pageInfo={pageInfo}
+ onAdd={() => navigate(customerAddUrl)}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ onRowClick={id => () =>
+ navigate(customerUrl(encodeURIComponent(id)))}
+ />
+ )}
+ </Paginator>
+ )}
</TypedCustomerListQuery>
);
}}
diff --git a/saleor/static/dashboard-next/misc.ts b/saleor/static/dashboard-next/misc.ts
index 1f2e5ba8e0..ad2bcf6860 100644
--- a/saleor/static/dashboard-next/misc.ts
+++ b/saleor/static/dashboard-next/misc.ts
@@ -1,92 +1,6 @@
-import { stringify } from "qs";
import i18n from "./i18n";
import { AuthorizationKeyType, TaxRateType } from "./types/globalTypes";
-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
- };
-}
-
export function renderCollection<T>(
collection: T[],
renderItem: (
diff --git a/saleor/static/dashboard-next/orders/index.tsx b/saleor/static/dashboard-next/orders/index.tsx
index 9dc42f8716..b4e690bc14 100644
--- a/saleor/static/dashboard-next/orders/index.tsx
+++ b/saleor/static/dashboard-next/orders/index.tsx
@@ -6,13 +6,13 @@ import i18n from "../i18n";
import { maybe } from "../misc";
import { OrderStatus, PaymentChargeStatusEnum } from "../types/globalTypes";
import OrderDetailsComponent from "./views/OrderDetails";
-import OrderListComponent from "./views/OrderList";
+import OrderListComponent, { OrderListQueryParams } from "./views/OrderList";
const OrderList: React.StatelessComponent<RouteComponentProps<any>> = ({
location
}) => {
const qs = parseQs(location.search.substr(1));
- const params = {
+ const params: OrderListQueryParams = {
after: qs.after,
before: qs.before
};
@@ -59,10 +59,6 @@ export const transformPaymentStatus = (status: string) => {
default:
return { localized: i18n.t("Unpaid"), status: "error" };
}
- return {
- localized: status,
- status
- };
};
export const transformOrderStatus = (status: string) => {
diff --git a/saleor/static/dashboard-next/orders/views/OrderList.tsx b/saleor/static/dashboard-next/orders/views/OrderList.tsx
index 712dc0ae12..90c38dd44e 100644
--- a/saleor/static/dashboard-next/orders/views/OrderList.tsx
+++ b/saleor/static/dashboard-next/orders/views/OrderList.tsx
@@ -1,20 +1,23 @@
import * as React from "react";
-import { orderListUrl, orderUrl } from "..";
+import { orderUrl } from "..";
import Messages from "../../components/messages";
import Navigator from "../../components/Navigator";
+import { createPaginationState, Paginator } from "../../components/Paginator";
import i18n from "../../i18n";
-import { createPaginationData, createPaginationState, maybe } from "../../misc";
+import { maybe } from "../../misc";
import OrderListPage from "../components/OrderListPage/OrderListPage";
import { TypedOrderDraftCreateMutation } from "../mutations";
import { TypedOrderListQuery } from "../queries";
import { OrderDraftCreate } from "../types/OrderDraftCreate";
+export type OrderListQueryParams = Partial<{
+ after: string;
+ before: string;
+}>;
+
interface OrderListProps {
- params: {
- after?: string;
- before?: string;
- };
+ params: OrderListQueryParams;
}
const PAGINATE_BY = 20;
@@ -45,33 +48,28 @@ export const OrderList: React.StatelessComponent<OrderListProps> = ({
);
return (
<TypedOrderListQuery variables={paginationState}>
- {({ data, loading }) => {
- const {
- loadNextPage,
- loadPreviousPage,
- pageInfo
- } = createPaginationData(
- navigate,
- paginationState,
- orderListUrl,
- maybe(() => data.orders.pageInfo),
- loading
- );
- return (
- <OrderListPage
- disabled={loading}
- orders={maybe(() =>
- data.orders.edges.map(edge => edge.node)
- )}
- pageInfo={pageInfo}
- onAdd={createOrder}
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- onRowClick={id => () =>
- navigate(orderUrl(encodeURIComponent(id)))}
- />
- );
- }}
+ {({ data, loading }) => (
+ <Paginator
+ pageInfo={maybe(() => data.orders.pageInfo)}
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({ loadNextPage, loadPreviousPage, pageInfo }) => (
+ <OrderListPage
+ disabled={loading}
+ orders={maybe(() =>
+ data.orders.edges.map(edge => edge.node)
+ )}
+ pageInfo={pageInfo}
+ onAdd={createOrder}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ onRowClick={id => () =>
+ navigate(orderUrl(encodeURIComponent(id)))}
+ />
+ )}
+ </Paginator>
+ )}
</TypedOrderListQuery>
);
}}
diff --git a/saleor/static/dashboard-next/pages/index.tsx b/saleor/static/dashboard-next/pages/index.tsx
index a8291bd33f..959221b921 100644
--- a/saleor/static/dashboard-next/pages/index.tsx
+++ b/saleor/static/dashboard-next/pages/index.tsx
@@ -4,13 +4,13 @@ import { Route, RouteComponentProps, Switch } from "react-router-dom";
import PageCreate from "./views/PageCreate";
import PageDetailsComponent from "./views/PageDetails";
-import PageListComponent from "./views/PageList";
+import PageListComponent, { PageListQueryParams } from "./views/PageList";
const PageList: React.StatelessComponent<RouteComponentProps<any>> = ({
location
}) => {
const qs = parseQs(location.search.substr(1));
- const params = {
+ const params: PageListQueryParams = {
after: qs.after,
before: qs.before
};
diff --git a/saleor/static/dashboard-next/pages/views/PageList.tsx b/saleor/static/dashboard-next/pages/views/PageList.tsx
index 6a0f07c6e1..70aea3b507 100644
--- a/saleor/static/dashboard-next/pages/views/PageList.tsx
+++ b/saleor/static/dashboard-next/pages/views/PageList.tsx
@@ -2,93 +2,71 @@ import * as React from "react";
import ErrorMessageCard from "../../components/ErrorMessageCard";
import Navigator from "../../components/Navigator";
+import { createPaginationState, Paginator } from "../../components/Paginator";
import i18n from "../../i18n";
-import { createPaginationData, createPaginationState } from "../../misc";
+import { maybe } from "../../misc";
import PageListPage from "../components/PageListPage/PageListPage";
-import { pageAddUrl, pageEditUrl, pageListUrl } from "../index";
+import { pageAddUrl, pageEditUrl } from "../index";
import { pageListQuery, TypedPageListQuery } from "../queries";
-interface PageListProps {
- params: {
- after?: string;
- before?: string;
- };
-}
+export type PageListQueryParams = Partial<{ after: string; before: string }>;
-interface PageListState {
- isFilterMenuOpened: boolean;
+interface PageListProps {
+ params: PageListQueryParams;
}
const PAGINATE_BY = 20;
-export class PageList extends React.Component<PageListProps, PageListState> {
- state = { isFilterMenuOpened: false };
- handleFilterMenuOpen = () => {
- this.setState(prevState => ({
- isFilterMenuOpened: !prevState.isFilterMenuOpened
- }));
- };
- render() {
- const { params } = this.props;
- const paginationState = createPaginationState(PAGINATE_BY, params);
- return (
- <Navigator>
- {navigate => (
- <TypedPageListQuery
- query={pageListQuery}
- variables={paginationState}
- fetchPolicy="network-only"
- >
- {({ data, loading, error }) => {
- if (error) {
- return (
- <ErrorMessageCard
- message={i18n.t("Something went terribly wrong.")}
- />
- );
- }
-
- const {
- loadNextPage,
- loadPreviousPage,
- pageInfo
- } = createPaginationData(
- navigate,
- paginationState,
- pageListUrl,
- data && data.pages ? data.pages.pageInfo : undefined,
- loading
- );
-
+export const PageList: React.StatelessComponent<PageListProps> = ({
+ params
+}) => (
+ <Navigator>
+ {navigate => {
+ const paginationState = createPaginationState(PAGINATE_BY, params);
+ const handleEditClick = (id: string) => () => navigate(pageEditUrl(id));
+ return (
+ <TypedPageListQuery
+ query={pageListQuery}
+ variables={paginationState}
+ fetchPolicy="network-only"
+ >
+ {({ data, loading, error }) => {
+ if (error) {
return (
- <Navigator>
- {navigate => {
- const handleEditClick = (id: string) => () =>
- navigate(pageEditUrl(id));
- return (
- <PageListPage
- disabled={loading}
- pages={
- data && data.pages
- ? data.pages.edges.map(edge => edge.node)
- : undefined
- }
- pageInfo={pageInfo}
- onAdd={() => navigate(pageAddUrl)}
- onRowClick={handleEditClick}
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- />
- );
- }}
- </Navigator>
+ <ErrorMessageCard
+ message={i18n.t("Something went terribly wrong.")}
+ />
);
- }}
- </TypedPageListQuery>
- )}
- </Navigator>
- );
- }
-}
+ }
+
+ return (
+ <Paginator
+ pageInfo={maybe(() => data.pages.pageInfo)}
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({ loadNextPage, loadPreviousPage, pageInfo }) => (
+ <PageListPage
+ disabled={loading}
+ pages={
+ data && data.pages
+ ? data.pages.edges.map(edge => edge.node)
+ : undefined
+ }
+ pageInfo={pageInfo}
+ onAdd={() => navigate(pageAddUrl)}
+ onRowClick={handleEditClick}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ />
+ )}
+ </Paginator>
+ );
+ }}
+ </TypedPageListQuery>
+ );
+ }}
+ </Navigator>
+);
export default PageList;
diff --git a/saleor/static/dashboard-next/productTypes/index.tsx b/saleor/static/dashboard-next/productTypes/index.tsx
index 7fd05f7730..e6e34e77d8 100644
--- a/saleor/static/dashboard-next/productTypes/index.tsx
+++ b/saleor/static/dashboard-next/productTypes/index.tsx
@@ -3,7 +3,9 @@ import * as React from "react";
import { Route, RouteComponentProps, Switch } from "react-router-dom";
import ProductTypeCreate from "./views/ProductTypeCreate";
-import ProductTypeListComponent from "./views/ProductTypeList";
+import ProductTypeListComponent, {
+ ProductTypeListQueryParams
+} from "./views/ProductTypeList";
import ProductTypeUpdateComponent from "./views/ProductTypeUpdate";
export const productTypeAddUrl = "/productTypes/add/";
@@ -14,7 +16,7 @@ const ProductTypeList: React.StatelessComponent<RouteComponentProps<{}>> = ({
location
}) => {
const qs = parseQs(location.search.substr(1));
- const params = {
+ const params: ProductTypeListQueryParams = {
after: qs.after,
before: qs.before
};
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
index fbcaa2363a..1b612f5040 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
@@ -1,21 +1,20 @@
import * as React from "react";
-import {
- productTypeAddUrl,
- productTypeDetailsUrl,
- productTypeListUrl
-} from "..";
+import { productTypeAddUrl, productTypeDetailsUrl } from "..";
import ErrorMessageCard from "../../components/ErrorMessageCard";
import Navigator from "../../components/Navigator";
-import { createPaginationData, createPaginationState, maybe } from "../../misc";
+import { createPaginationState, Paginator } from "../../components/Paginator";
+import { maybe } from "../../misc";
import ProductTypeListPage from "../components/ProductTypeListPage";
import { TypedProductTypeListQuery } from "../queries";
+export type ProductTypeListQueryParams = Partial<{
+ after: string;
+ before: string;
+}>;
+
interface ProductTypeListProps {
- params: {
- after?: string;
- before?: string;
- };
+ params: ProductTypeListQueryParams;
}
const PAGINATE_BY = 20;
@@ -32,31 +31,26 @@ export const ProductTypeList: React.StatelessComponent<
if (error) {
return <ErrorMessageCard message="Something went wrong" />;
}
- const {
- loadNextPage,
- loadPreviousPage,
- pageInfo
- } = createPaginationData(
- navigate,
- paginationState,
- productTypeListUrl,
- data && data.productTypes
- ? data.productTypes.pageInfo
- : undefined,
- loading
- );
return (
- <ProductTypeListPage
- disabled={loading}
- productTypes={maybe(() =>
- data.productTypes.edges.map(edge => edge.node)
+ <Paginator
+ pageInfo={maybe(() => data.productTypes.pageInfo)}
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({ loadNextPage, loadPreviousPage, pageInfo }) => (
+ <ProductTypeListPage
+ disabled={loading}
+ productTypes={maybe(() =>
+ data.productTypes.edges.map(edge => edge.node)
+ )}
+ pageInfo={pageInfo}
+ onAdd={() => navigate(productTypeAddUrl)}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ onRowClick={id => () => navigate(productTypeDetailsUrl(id))}
+ />
)}
- pageInfo={pageInfo}
- onAdd={() => navigate(productTypeAddUrl)}
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- onRowClick={id => () => navigate(productTypeDetailsUrl(id))}
- />
+ </Paginator>
);
}}
</TypedProductTypeListQuery>
diff --git a/saleor/static/dashboard-next/products/index.tsx b/saleor/static/dashboard-next/products/index.tsx
index 125353cec8..51bfb81fbb 100644
--- a/saleor/static/dashboard-next/products/index.tsx
+++ b/saleor/static/dashboard-next/products/index.tsx
@@ -4,7 +4,9 @@ import { Route, RouteComponentProps, Switch } from "react-router-dom";
import ProductCreate from "./views/ProductCreate";
import ProductImageComponent from "./views/ProductImage";
-import ProductListComponent from "./views/ProductList";
+import ProductListComponent, {
+ ProductListQueryParams
+} from "./views/ProductList";
import ProductUpdateComponent from "./views/ProductUpdate";
import ProductVariantComponent from "./views/ProductVariant";
import ProductVariantCreateComponent from "./views/ProductVariantCreate";
@@ -13,7 +15,7 @@ const ProductList: React.StatelessComponent<RouteComponentProps<any>> = ({
location
}) => {
const qs = parseQs(location.search.substr(1));
- const params = {
+ const params: ProductListQueryParams = {
after: qs.after,
before: qs.before
};
diff --git a/saleor/static/dashboard-next/products/views/ProductList.tsx b/saleor/static/dashboard-next/products/views/ProductList.tsx
index 917b9d9ccf..6b6ffc473e 100644
--- a/saleor/static/dashboard-next/products/views/ProductList.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductList.tsx
@@ -1,17 +1,20 @@
import * as React from "react";
-import { productAddUrl, productListUrl, productUrl } from "..";
+import { productAddUrl, productUrl } from "..";
import ErrorMessageCard from "../../components/ErrorMessageCard";
import Navigator from "../../components/Navigator";
-import { createPaginationData, createPaginationState } from "../../misc";
+import { createPaginationState, Paginator } from "../../components/Paginator";
+import { maybe } from "../../misc";
import ProductListCard from "../components/ProductListCard";
import { productListQuery, TypedProductListQuery } from "../queries";
+export type ProductListQueryParams = Partial<{
+ after: string;
+ before: string;
+}>;
+
interface ProductListProps {
- params: {
- after?: string;
- before?: string;
- };
+ params: ProductListQueryParams;
}
const PAGINATE_BY = 20;
@@ -32,32 +35,31 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
if (error) {
return <ErrorMessageCard message="Something went wrong" />;
}
- const {
- loadNextPage,
- loadPreviousPage,
- pageInfo
- } = createPaginationData(
- navigate,
- paginationState,
- productListUrl,
- data && data.products ? data.products.pageInfo : undefined,
- loading
- );
return (
- <ProductListCard
- onAdd={() => navigate(productAddUrl)}
- disabled={loading}
- products={
- data && data.products !== undefined && data.products !== null
- ? data.products.edges.map(p => p.node)
- : undefined
- }
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- pageInfo={pageInfo}
- onRowClick={id => () =>
- navigate(productUrl(encodeURIComponent(id)))}
- />
+ <Paginator
+ pageInfo={maybe(() => data.products.pageInfo)}
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({ loadNextPage, loadPreviousPage, pageInfo }) => (
+ <ProductListCard
+ onAdd={() => navigate(productAddUrl)}
+ disabled={loading}
+ products={
+ data &&
+ data.products !== undefined &&
+ data.products !== null
+ ? data.products.edges.map(p => p.node)
+ : undefined
+ }
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ pageInfo={pageInfo}
+ onRowClick={id => () =>
+ navigate(productUrl(encodeURIComponent(id)))}
+ />
+ )}
+ </Paginator>
);
}}
</TypedProductListQuery>
diff --git a/saleor/static/dashboard-next/staff/index.tsx b/saleor/static/dashboard-next/staff/index.tsx
index 1802086928..d17816051e 100644
--- a/saleor/static/dashboard-next/staff/index.tsx
+++ b/saleor/static/dashboard-next/staff/index.tsx
@@ -3,13 +3,13 @@ import * as React from "react";
import { Route, RouteComponentProps, Switch } from "react-router-dom";
import StaffDetailsComponent from "./views/StaffDetails";
-import StaffListComponent from "./views/StaffList";
+import StaffListComponent, { StaffListQueryParams } from "./views/StaffList";
const StaffList: React.StatelessComponent<RouteComponentProps<{}>> = ({
location
}) => {
const qs = parseQs(location.search.substr(1));
- const params = {
+ const params: StaffListQueryParams = {
after: qs.after,
before: qs.before
};
diff --git a/saleor/static/dashboard-next/staff/views/StaffList.tsx b/saleor/static/dashboard-next/staff/views/StaffList.tsx
index acf3085ff0..88bb434aaa 100644
--- a/saleor/static/dashboard-next/staff/views/StaffList.tsx
+++ b/saleor/static/dashboard-next/staff/views/StaffList.tsx
@@ -1,26 +1,29 @@
import * as React from "react";
-import { staffListUrl, staffMemberDetailsUrl } from "..";
+import { staffMemberDetailsUrl } from "..";
import Messages from "../../components/messages";
import Navigator from "../../components/Navigator";
+import { createPaginationState, Paginator } from "../../components/Paginator";
import i18n from "../../i18n";
-import { createPaginationData, createPaginationState, maybe } from "../../misc";
+import { maybe } from "../../misc";
import { FormData as AddStaffMemberForm } from "../components/StaffAddMemberDialog";
import StaffListPage from "../components/StaffListPage";
import { TypedStaffMemberAddMutation } from "../mutations";
import { TypedStaffListQuery } from "../queries";
import { StaffMemberAdd } from "../types/StaffMemberAdd";
-interface OrderListProps {
- params: {
- after?: string;
- before?: string;
- };
+export type StaffListQueryParams = Partial<{
+ after: string;
+ before: string;
+}>;
+
+interface StaffListProps {
+ params: StaffListQueryParams;
}
const PAGINATE_BY = 20;
-export const StaffList: React.StatelessComponent<OrderListProps> = ({
+export const StaffList: React.StatelessComponent<StaffListProps> = ({
params
}) => (
<Navigator>
@@ -31,17 +34,6 @@ export const StaffList: React.StatelessComponent<OrderListProps> = ({
return (
<TypedStaffListQuery variables={paginationState}>
{({ data, loading }) => {
- const {
- loadNextPage,
- loadPreviousPage,
- pageInfo
- } = createPaginationData(
- navigate,
- paginationState,
- staffListUrl,
- maybe(() => data.staffUsers.pageInfo),
- loading
- );
const handleStaffMemberAddSuccess = (data: StaffMemberAdd) => {
if (!maybe(() => data.staffCreate.errors.length)) {
pushMessage({
@@ -74,24 +66,33 @@ export const StaffList: React.StatelessComponent<OrderListProps> = ({
}
});
return (
- <StaffListPage
- disabled={loading || addStaffMemberData.loading}
- errors={maybe(
- () => addStaffMemberData.data.staffCreate.errors,
- []
- )}
- pageInfo={pageInfo}
- staffMembers={maybe(() =>
- data.staffUsers.edges.map(edge => edge.node)
+ <Paginator
+ pageInfo={maybe(() => data.staffUsers.pageInfo)}
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({ loadNextPage, loadPreviousPage, pageInfo }) => (
+ <StaffListPage
+ disabled={loading || addStaffMemberData.loading}
+ errors={maybe(
+ () =>
+ addStaffMemberData.data.staffCreate.errors,
+ []
+ )}
+ pageInfo={pageInfo}
+ staffMembers={maybe(() =>
+ data.staffUsers.edges.map(edge => edge.node)
+ )}
+ onAdd={handleStaffMemberAdd}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ onRowClick={id => () =>
+ navigate(
+ staffMemberDetailsUrl(encodeURIComponent(id))
+ )}
+ />
)}
- onAdd={handleStaffMemberAdd}
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- onRowClick={id => () =>
- navigate(
- staffMemberDetailsUrl(encodeURIComponent(id))
- )}
- />
+ </Paginator>
);
}}
</TypedStaffMemberAddMutation>