Skip to content

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

Add product type details

dominik-zeglen408674509c7c
diff --git a/saleor/static/dashboard-next/gql-types.ts b/saleor/static/dashboard-next/gql-types.ts
index d2a7880991..ddfa8db84a 100644
--- a/saleor/static/dashboard-next/gql-types.ts
+++ b/saleor/static/dashboard-next/gql-types.ts
@@ -395,6 +395,28 @@ export interface PageDetailsQuery {
} | null,
};
+export interface ProductTypeDeleteMutationVariables {
+ id: string,
+};
+
+export interface ProductTypeDeleteMutation {
+ productTypeDelete: {
+ // List of errors that occurred executing the mutation.
+ errors: Array< {
+ // Name of a field that caused the error. A value of
+ // `null` indicates that the error isn't associated with a particular
+ // field.
+ field: string | null,
+ // The error message.
+ message: string | null,
+ } | null > | null,
+ productType: {
+ // The ID of the object.
+ id: string,
+ } | null,
+ } | null,
+};
+
export interface ProductTypeListQueryVariables {
after?: string | null,
before?: string | null,
@@ -449,6 +471,69 @@ export interface ProductTypeListQuery {
} | null,
};
+export interface ProductTypeDetailsQueryVariables {
+ id: string,
+};
+
+export interface ProductTypeDetailsQuery {
+ // Lookup a product type by ID.
+ productType: {
+ // 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,
+ // Internal representation of an attribute name.
+ slug: string | null,
+ // 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,
+ // Internal representation of an attribute name.
+ slug: string | null,
+ // Visible name for display purposes.
+ name: string | null,
+ },
+ } >,
+ } | null,
+ isShippingRequired: boolean,
+ taxRate: string,
+ } | null,
+};
+
+export interface SearchAttributeQueryVariables {
+ search: string,
+};
+
+export interface SearchAttributeQuery {
+ // List of the shop's product attributes.
+ attributes: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ // Internal representation of an attribute name.
+ slug: string | null,
+ // Visible name for display purposes.
+ name: string | null,
+ },
+ } >,
+ } | null,
+};
+
export interface ProductImageCreateMutationVariables {
product: string,
image: string,
diff --git a/saleor/static/dashboard-next/productTypes/containers/ProductTypeDelete.tsx b/saleor/static/dashboard-next/productTypes/containers/ProductTypeDelete.tsx
new file mode 100644
index 0000000000..5d03f88218
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/containers/ProductTypeDelete.tsx
@@ -0,0 +1,44 @@
+import * as React from "react";
+
+import {
+ PartialMutationProviderProps,
+ PartialMutationProviderRenderProps
+} from "../..";
+import {
+ ProductTypeDeleteMutation,
+ ProductTypeDeleteMutationVariables
+} from "../../gql-types";
+import {
+ productTypeDeleteMutation,
+ TypedProductTypeDeleteMutation
+} from "../mutations";
+
+interface ProductTypeDeleteProviderProps extends PartialMutationProviderProps {
+ id: string;
+ children: PartialMutationProviderRenderProps<
+ ProductTypeDeleteMutation,
+ ProductTypeDeleteMutationVariables
+ >;
+}
+
+const ProductTypeDeleteProvider: React.StatelessComponent<
+ ProductTypeDeleteProviderProps
+> = ({ id, children, onError, onSuccess }) => (
+ <TypedProductTypeDeleteMutation
+ mutation={productTypeDeleteMutation}
+ variables={{ id }}
+ onCompleted={onSuccess}
+ onError={onError}
+ >
+ {(mutate, { data, loading, error }) => {
+ return children({
+ data,
+ error,
+ loading,
+ mutate
+ });
+ }}
+ </TypedProductTypeDeleteMutation>
+);
+
+export default ProductTypeDeleteProvider;
diff --git a/saleor/static/dashboard-next/productTypes/containers/ProductTypeOperations.tsx b/saleor/static/dashboard-next/productTypes/containers/ProductTypeOperations.tsx
new file mode 100644
index 0000000000..977b58abbe
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/containers/ProductTypeOperations.tsx
@@ -0,0 +1,45 @@
+import * as React from "react";
+
+import {
+ MutationProviderProps,
+ MutationProviderRenderProps,
+ PartialMutationProviderOutput
+} from "../..";
+import {
+ ProductTypeDeleteMutation,
+ ProductTypeDeleteMutationVariables
+} from "../../gql-types";
+import ProductTypeDeleteProvider from "./ProductTypeDelete";
+
+interface ProductTypeOperationsProps extends MutationProviderProps {
+ id: string;
+ children: MutationProviderRenderProps<{
+ deleteProductType: PartialMutationProviderOutput<
+ ProductTypeDeleteMutation,
+ ProductTypeDeleteMutationVariables
+ >;
+ loading: boolean;
+ }>;
+ onDelete?: (data: ProductTypeDeleteMutation) => void;
+}
+
+const ProductTypeOperations: React.StatelessComponent<
+ ProductTypeOperationsProps
+> = ({ id, children, onError, onDelete }) => {
+ return (
+ <ProductTypeDeleteProvider id={id} onError={onError} onSuccess={onDelete}>
+ {deleteProductType =>
+ children({
+ deleteProductType: {
+ data: deleteProductType.data,
+ loading: deleteProductType.loading,
+ mutate: () => deleteProductType.mutate({ variables: { id } })
+ },
+ errors: [],
+ loading: deleteProductType.loading
+ })
+ }
+ </ProductTypeDeleteProvider>
+ );
+};
+export default ProductTypeOperations;
diff --git a/saleor/static/dashboard-next/productTypes/index.tsx b/saleor/static/dashboard-next/productTypes/index.tsx
index 651b77ff38..fbd0e3a98b 100644
--- a/saleor/static/dashboard-next/productTypes/index.tsx
+++ b/saleor/static/dashboard-next/productTypes/index.tsx
@@ -3,11 +3,12 @@ import * as React from "react";
import { Route, RouteComponentProps, Switch } from "react-router-dom";
import ProductTypeListComponent from "./views/ProductTypeList";
+import ProductTypeUpdateComponent from "./views/ProductTypeUpdate";
export const productTypeListUrl = "/productTypes";
export const productTypeDetailsUrl = (id: string) => `/productTypes/${id}/`;
-const ProductTypeList: React.StatelessComponent<RouteComponentProps<any>> = ({
+const ProductTypeList: React.StatelessComponent<RouteComponentProps<{}>> = ({
location
}) => {
const queryString = parseQs(location.search.substr(1));
@@ -22,11 +23,19 @@ const ProductTypeList: React.StatelessComponent<RouteComponentProps<any>> = ({
return <ProductTypeListComponent params={params} />;
};
+interface ProductTypeUpdateRouteParams {
+ id: string;
+}
+const ProductTypeUpdate: React.StatelessComponent<
+ RouteComponentProps<ProductTypeUpdateRouteParams>
+> = ({ match }) => <ProductTypeUpdateComponent id={match.params.id} />;
+
export const ProductTypeRouter: React.StatelessComponent<
RouteComponentProps<any>
> = ({ match }) => (
<Switch>
<Route exact path={match.url} component={ProductTypeList} />
+ <Route exact path={match.url + "/:id/"} component={ProductTypeUpdate} />
</Switch>
);
ProductTypeRouter.displayName = "ProductTypeRouter";
diff --git a/saleor/static/dashboard-next/productTypes/mutations.ts b/saleor/static/dashboard-next/productTypes/mutations.ts
new file mode 100644
index 0000000000..ee413441e2
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/mutations.ts
@@ -0,0 +1,24 @@
+import gql from "graphql-tag";
+import { Mutation, MutationProps } from "react-apollo";
+
+import {
+ ProductTypeDeleteMutation,
+ ProductTypeDeleteMutationVariables
+} from "../gql-types";
+
+export const TypedProductTypeDeleteMutation = Mutation as React.ComponentType<
+ MutationProps<ProductTypeDeleteMutation, ProductTypeDeleteMutationVariables>
+>;
+export const productTypeDeleteMutation = gql`
+ mutation ProductTypeDelete($id: ID!) {
+ productTypeDelete(id: $id) {
+ errors {
+ field
+ message
+ }
+ productType {
+ id
+ }
+ }
+ }
+`;
diff --git a/saleor/static/dashboard-next/productTypes/queries.ts b/saleor/static/dashboard-next/productTypes/queries.ts
index 904e426036..02735691d5 100644
--- a/saleor/static/dashboard-next/productTypes/queries.ts
+++ b/saleor/static/dashboard-next/productTypes/queries.ts
@@ -2,8 +2,12 @@ import gql from "graphql-tag";
import { Query, QueryProps } from "react-apollo";
import {
+ ProductTypeDetailsQuery,
+ ProductTypeDetailsQueryVariables,
ProductTypeListQuery,
- ProductTypeListQueryVariables
+ ProductTypeListQueryVariables,
+ SearchAttributeQuery,
+ SearchAttributeQueryVariables
} from "../gql-types";
export const TypedProductTypeListQuery = Query as React.ComponentType<
@@ -49,3 +53,53 @@ export const productTypeListQuery = gql`
}
}
`;
+
+export const TypedProductTypeDetailsQuery = Query as React.ComponentType<
+ QueryProps<ProductTypeDetailsQuery, ProductTypeDetailsQueryVariables>
+>;
+export const productTypeDetailsQuery = gql`
+ query ProductTypeDetails($id: ID!) {
+ productType(id: $id) {
+ id
+ name
+ hasVariants
+ productAttributes {
+ edges {
+ node {
+ id
+ slug
+ name
+ }
+ }
+ }
+ variantAttributes {
+ edges {
+ node {
+ id
+ slug
+ name
+ }
+ }
+ }
+ isShippingRequired
+ taxRate
+ }
+ }
+`;
+
+export const TypedSearchAttributeQuery = Query as React.ComponentType<
+ QueryProps<SearchAttributeQuery, SearchAttributeQueryVariables>
+>;
+export const searchAttributeQuery = gql`
+ query SearchAttribute($search: String!) {
+ attributes(query: $search, first: 5) {
+ edges {
+ node {
+ id
+ slug
+ name
+ }
+ }
+ }
+ }
+`;
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
index 4356ee714b..9e295ade6f 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
@@ -1,6 +1,6 @@
import * as React from "react";
-import { productTypeListUrl } from "../";
+import { productTypeDetailsUrl, productTypeListUrl } from "../";
import ErrorMessageCard from "../../components/ErrorMessageCard";
import Navigator from "../../components/Navigator";
import { createPaginationData, createPaginationState } from "../../misc";
@@ -14,7 +14,7 @@ interface ProductTypeListProps {
};
}
-const PAGINATE_BY = 3;
+const PAGINATE_BY = 20;
export const ProductTypeList: React.StatelessComponent<
ProductTypeListProps
@@ -57,7 +57,7 @@ export const ProductTypeList: React.StatelessComponent<
onAdd={() => undefined}
onNextPage={loadNextPage}
onPreviousPage={loadPreviousPage}
- onRowClick={id => () => undefined}
+ onRowClick={id => () => navigate(productTypeDetailsUrl(id))}
/>
);
}}
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
new file mode 100644
index 0000000000..8c171e47f4
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
@@ -0,0 +1,123 @@
+import * as React from "react";
+import Navigator from "../../components/Navigator";
+
+import { productTypeListUrl } from "..";
+import ErrorMessageCard from "../../components/ErrorMessageCard";
+import Messages from "../../components/messages";
+import i18n from "../../i18n";
+import ProductTypeDetailsPage from "../components/ProductTypeDetailsPage";
+import ProductTypeOperations from "../containers/ProductTypeOperations";
+import {
+ productTypeDetailsQuery,
+ searchAttributeQuery,
+ TypedProductTypeDetailsQuery,
+ TypedSearchAttributeQuery
+} from "../queries";
+import { ProductTypeDeleteMutation } from "../../gql-types";
+
+const taxRates = ["standard", "electronics", "food", "apparel"]; // FIXME: delet dis
+
+interface ProductTypeUpdateProps {
+ id: string;
+}
+
+export const ProductTypeUpdate: React.StatelessComponent<
+ ProductTypeUpdateProps
+> = ({ id }) => (
+ <Messages>
+ {pushMessage => (
+ <Navigator>
+ {navigate => (
+ <TypedProductTypeDetailsQuery
+ query={productTypeDetailsQuery}
+ variables={{ id }}
+ >
+ {({ data, error, loading: dataLoading }) => {
+ if (error) {
+ return (
+ <ErrorMessageCard
+ message={i18n.t("Something went terribly wrong")}
+ />
+ );
+ }
+ return (
+ <TypedSearchAttributeQuery query={searchAttributeQuery}>
+ {searchAttribute => {
+ const handleSearchAttribute = (search: string) =>
+ search ? searchAttribute.refetch({ search }) : undefined;
+ const handleDeleteSuccess = (
+ deleteData: ProductTypeDeleteMutation
+ ) => {
+ if (deleteData.productTypeDelete.errors.length === 0) {
+ pushMessage({
+ text: i18n.t("Successfully deleted product type")
+ });
+ navigate(productTypeListUrl);
+ }
+ };
+ return (
+ <ProductTypeOperations
+ id={id}
+ onDelete={handleDeleteSuccess}
+ >
+ {({ deleteProductType, loading: mutationLoading }) => {
+ const handleDelete = () =>
+ deleteProductType.mutate({ id });
+ const loading = mutationLoading || dataLoading;
+ return (
+ <ProductTypeDetailsPage
+ disabled={false}
+ productType={data ? data.productType : undefined}
+ productAttributes={
+ data &&
+ data.productType &&
+ data.productType.productAttributes &&
+ data.productType.productAttributes.edges
+ ? data.productType.productAttributes.edges.map(
+ edge => edge.node
+ )
+ : undefined
+ }
+ variantAttributes={
+ data &&
+ data.productType &&
+ data.productType.variantAttributes &&
+ data.productType.variantAttributes.edges
+ ? data.productType.variantAttributes.edges.map(
+ edge => edge.node
+ )
+ : undefined
+ }
+ saveButtonBarState={
+ loading ? "loading" : "default"
+ }
+ searchLoading={searchAttribute.loading}
+ searchResults={
+ searchAttribute.data &&
+ searchAttribute.data.attributes
+ ? searchAttribute.data.attributes.edges.map(
+ edge => edge.node
+ )
+ : []
+ }
+ taxRates={taxRates}
+ onAttributeSearch={handleSearchAttribute}
+ onBack={() => navigate(productTypeListUrl)}
+ onDelete={handleDelete}
+ onSubmit={() => undefined}
+ />
+ );
+ }}
+ </ProductTypeOperations>
+ );
+ }}
+ </TypedSearchAttributeQuery>
+ );
+ }}
+ </TypedProductTypeDetailsQuery>
+ )}
+ </Navigator>
+ )}
+ </Messages>
+);
+export default ProductTypeUpdate;