Skip to content

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

Add product type list

dominik-zeglenef57ba272adc
diff --git a/saleor/static/dashboard-next/gql-types.ts b/saleor/static/dashboard-next/gql-types.ts
index 7bb01ea699..d2a7880991 100644
--- a/saleor/static/dashboard-next/gql-types.ts
+++ b/saleor/static/dashboard-next/gql-types.ts
@@ -395,6 +395,60 @@ export interface PageDetailsQuery {
} | null,
};
+export interface ProductTypeListQueryVariables {
+ after?: string | null,
+ before?: string | null,
+ first?: number | null,
+ last?: number | null,
+};
+
+export interface ProductTypeListQuery {
+ // List of the shop's product types.
+ productTypes: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ name: string,
+ hasVariants: boolean,
+ productAttributes: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ // Visible name for display purposes.
+ name: string | null,
+ },
+ } >,
+ } | null,
+ variantAttributes: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ // Visible name for display purposes.
+ name: string | null,
+ },
+ } >,
+ } | null,
+ },
+ } >,
+ pageInfo: {
+ // When paginating forwards, are there more items?
+ hasNextPage: boolean,
+ // When paginating backwards, are there more items?
+ hasPreviousPage: boolean,
+ // When paginating backwards, the cursor to continue.
+ startCursor: string | null,
+ // When paginating forwards, the cursor to continue.
+ endCursor: string | null,
+ },
+ } | null,
+};
+
export interface ProductImageCreateMutationVariables {
product: string,
image: string,
diff --git a/saleor/static/dashboard-next/index.tsx b/saleor/static/dashboard-next/index.tsx
index 41de5119e2..82264b992d 100644
--- a/saleor/static/dashboard-next/index.tsx
+++ b/saleor/static/dashboard-next/index.tsx
@@ -23,6 +23,7 @@ import HomePage from "./home";
import "./i18n";
import PageSection from "./pages";
import ProductSection from "./products";
+import ProductTypesSection from "./productTypes";
import theme from "./theme";
const cookies = new Cookies();
@@ -83,6 +84,10 @@ render(
<Route path="/categories" component={CategorySection} />
<Route path="/pages" component={PageSection} />
<Route path="/products" component={ProductSection} />
+ <Route
+ path="/productTypes"
+ component={ProductTypesSection}
+ />
<Route
exact
path="/configuration"
diff --git a/saleor/static/dashboard-next/misc.ts b/saleor/static/dashboard-next/misc.ts
index 7b36fdc655..a420224a6c 100644
--- a/saleor/static/dashboard-next/misc.ts
+++ b/saleor/static/dashboard-next/misc.ts
@@ -52,7 +52,7 @@ export function createPaginationData(
url +
"?" +
stringify({
- after: pageInfo.endCursor
+ after: encodeURIComponent(pageInfo.endCursor)
}),
true
);
@@ -65,7 +65,7 @@ export function createPaginationData(
url +
"?" +
stringify({
- before: pageInfo.startCursor
+ before: encodeURIComponent(pageInfo.startCursor)
}),
true
);
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeList/ProductTypeList.tsx b/saleor/static/dashboard-next/productTypes/components/ProductTypeList/ProductTypeList.tsx
index a728f2c2f9..6cc0962005 100644
--- a/saleor/static/dashboard-next/productTypes/components/ProductTypeList/ProductTypeList.tsx
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeList/ProductTypeList.tsx
@@ -106,7 +106,7 @@ const ProductTypeList = decorate<ProductTypeListProps>(
{productType &&
productType.hasVariants &&
productType.variantAttributes &&
- productType.variantAttributes.edges ? (
+ productType.variantAttributes.edges !== undefined ? (
productType.variantAttributes.edges
.map(edge => edge.node.name)
.join(", ")
diff --git a/saleor/static/dashboard-next/productTypes/index.tsx b/saleor/static/dashboard-next/productTypes/index.tsx
new file mode 100644
index 0000000000..651b77ff38
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/index.tsx
@@ -0,0 +1,33 @@
+import { parse as parseQs } from "qs";
+import * as React from "react";
+import { Route, RouteComponentProps, Switch } from "react-router-dom";
+
+import ProductTypeListComponent from "./views/ProductTypeList";
+
+export const productTypeListUrl = "/productTypes";
+export const productTypeDetailsUrl = (id: string) => `/productTypes/${id}/`;
+
+const ProductTypeList: React.StatelessComponent<RouteComponentProps<any>> = ({
+ location
+}) => {
+ const queryString = parseQs(location.search.substr(1));
+ const params = {
+ after: queryString.after
+ ? decodeURIComponent(queryString.after)
+ : undefined,
+ before: queryString.before
+ ? decodeURIComponent(queryString.before)
+ : undefined
+ };
+ return <ProductTypeListComponent params={params} />;
+};
+
+export const ProductTypeRouter: React.StatelessComponent<
+ RouteComponentProps<any>
+> = ({ match }) => (
+ <Switch>
+ <Route exact path={match.url} component={ProductTypeList} />
+ </Switch>
+);
+ProductTypeRouter.displayName = "ProductTypeRouter";
+export default ProductTypeRouter;
diff --git a/saleor/static/dashboard-next/productTypes/queries.ts b/saleor/static/dashboard-next/productTypes/queries.ts
new file mode 100644
index 0000000000..904e426036
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/queries.ts
@@ -0,0 +1,51 @@
+import gql from "graphql-tag";
+import { Query, QueryProps } from "react-apollo";
+
+import {
+ ProductTypeListQuery,
+ ProductTypeListQueryVariables
+} from "../gql-types";
+
+export const TypedProductTypeListQuery = Query as React.ComponentType<
+ QueryProps<ProductTypeListQuery, ProductTypeListQueryVariables>
+>;
+export const productTypeListQuery = gql`
+ query ProductTypeList(
+ $after: String
+ $before: String
+ $first: Int
+ $last: Int
+ ) {
+ productTypes(after: $after, before: $before, first: $first, last: $last) {
+ edges {
+ node {
+ id
+ name
+ hasVariants
+ productAttributes {
+ edges {
+ node {
+ id
+ name
+ }
+ }
+ }
+ variantAttributes {
+ edges {
+ node {
+ id
+ name
+ }
+ }
+ }
+ }
+ }
+ pageInfo {
+ hasNextPage
+ hasPreviousPage
+ startCursor
+ endCursor
+ }
+ }
+ }
+`;
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
new file mode 100644
index 0000000000..4356ee714b
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
@@ -0,0 +1,70 @@
+import * as React from "react";
+
+import { productTypeListUrl } from "../";
+import ErrorMessageCard from "../../components/ErrorMessageCard";
+import Navigator from "../../components/Navigator";
+import { createPaginationData, createPaginationState } from "../../misc";
+import ProductTypeListPage from "../components/ProductTypeListPage";
+import { productTypeListQuery, TypedProductTypeListQuery } from "../queries";
+
+interface ProductTypeListProps {
+ params: {
+ after?: string;
+ before?: string;
+ };
+}
+
+const PAGINATE_BY = 3;
+
+export const ProductTypeList: React.StatelessComponent<
+ ProductTypeListProps
+> = ({ params }) => (
+ <Navigator>
+ {navigate => {
+ const paginationState = createPaginationState(PAGINATE_BY, params);
+ return (
+ <TypedProductTypeListQuery
+ query={productTypeListQuery}
+ variables={paginationState}
+ fetchPolicy="network-only"
+ >
+ {({ data, loading, error }) => {
+ 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={
+ data && data.productTypes
+ ? data.productTypes.edges.map(edge => edge.node)
+ : undefined
+ }
+ pageInfo={pageInfo}
+ onAdd={() => undefined}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ onRowClick={id => () => undefined}
+ />
+ );
+ }}
+ </TypedProductTypeListQuery>
+ );
+ }}
+ </Navigator>
+);
+ProductTypeList.displayName = "ProductTypeList";
+export default ProductTypeList;