Skip to content

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

Update types

dominik-zeglendd2c2a94da73
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeList/ProductTypeList.tsx b/saleor/static/dashboard-next/productTypes/components/ProductTypeList/ProductTypeList.tsx
index 6cc0962005..ef334afc65 100644
--- a/saleor/static/dashboard-next/productTypes/components/ProductTypeList/ProductTypeList.tsx
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeList/ProductTypeList.tsx
@@ -12,27 +12,16 @@ import { ListProps } from "../../..";
import Skeleton from "../../../components/Skeleton";
import TablePagination from "../../../components/TablePagination";
import i18n from "../../../i18n";
-import { renderCollection } from "../../../misc";
+import { maybe, renderCollection } from "../../../misc";
+import { AttributeType } from "../../../products";
-interface AttributeType {
- id: string;
- sortNumber?: number;
- name?: string;
-}
-interface AttributeEdgeType {
- node: AttributeType;
-}
interface ProductTypeListProps extends ListProps {
productTypes?: Array<{
id: string;
name?: string;
hasVariants?: boolean;
- productAttributes?: {
- edges: AttributeEdgeType[];
- };
- variantAttributes?: {
- edges: AttributeEdgeType[];
- };
+ productAttributes?: AttributeType[];
+ variantAttributes?: AttributeType[];
}>;
}
@@ -92,28 +81,25 @@ const ProductTypeList = decorate<ProductTypeListProps>(
{productType ? productType.name : <Skeleton />}
</TableCell>
<TableCell>
- {productType &&
- productType.productAttributes &&
- productType.productAttributes.edges ? (
- productType.productAttributes.edges
- .map(edge => edge.node.name)
+ {maybe(() => productType.productAttributes) ? (
+ productType.productAttributes
+ .map(attribute => attribute.name)
.join(", ")
) : (
<Skeleton />
)}
</TableCell>
<TableCell>
- {productType &&
- productType.hasVariants &&
- productType.variantAttributes &&
- productType.variantAttributes.edges !== undefined ? (
- productType.variantAttributes.edges
- .map(edge => edge.node.name)
- .join(", ")
- ) : productType && productType.hasVariants === false ? (
- "-"
+ {maybe(() => productType.hasVariants) ? (
+ maybe(() => productType.variantAttributes) ? (
+ productType.variantAttributes
+ .map(attribute => attribute.name)
+ .join(", ")
+ ) : (
+ <Skeleton />
+ )
) : (
- <Skeleton />
+ "-"
)}
</TableCell>
</TableRow>
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeListPage/ProductTypeListPage.tsx b/saleor/static/dashboard-next/productTypes/components/ProductTypeListPage/ProductTypeListPage.tsx
index 2a958fd1a7..46e050c8c5 100644
--- a/saleor/static/dashboard-next/productTypes/components/ProductTypeListPage/ProductTypeListPage.tsx
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeListPage/ProductTypeListPage.tsx
@@ -6,27 +6,19 @@ import { PageListProps } from "../../..";
import Container from "../../../components/Container";
import PageHeader from "../../../components/PageHeader";
import i18n from "../../../i18n";
+import {
+ ProductTypeList_productTypes_edges_node_productAttributes,
+ ProductTypeList_productTypes_edges_node_variantAttributes
+} from "../../types/ProductTypeList";
import ProductTypeList from "../ProductTypeList";
-interface AttributeType {
- id: string;
- sortNumber?: number;
- name?: string;
-}
-interface AttributeEdgeType {
- node: AttributeType;
-}
interface ProductTypeListPageProps extends PageListProps {
productTypes?: Array<{
id: string;
name?: string;
hasVariants?: boolean;
- productAttributes?: {
- edges: AttributeEdgeType[];
- };
- variantAttributes?: {
- edges: AttributeEdgeType[];
- };
+ productAttributes?: ProductTypeList_productTypes_edges_node_productAttributes[];
+ variantAttributes?: ProductTypeList_productTypes_edges_node_variantAttributes[];
}>;
}
diff --git a/saleor/static/dashboard-next/productTypes/queries.ts b/saleor/static/dashboard-next/productTypes/queries.ts
index 13ed4bdb22..60c505e94f 100644
--- a/saleor/static/dashboard-next/productTypes/queries.ts
+++ b/saleor/static/dashboard-next/productTypes/queries.ts
@@ -14,10 +14,24 @@ import {
SearchAttributeVariables
} from "./types/SearchAttribute";
+export const attributeFragment = gql`
+ fragment AttributeFragment on Attribute {
+ id
+ name
+ slug
+ values {
+ id
+ name
+ slug
+ }
+ }
+`;
+
export const TypedProductTypeListQuery = Query as React.ComponentType<
QueryProps<ProductTypeList, ProductTypeListVariables>
>;
export const productTypeListQuery = gql`
+ ${attributeFragment}
query ProductTypeList(
$after: String
$before: String
@@ -31,12 +45,10 @@ export const productTypeListQuery = gql`
name
hasVariants
productAttributes {
- id
- name
+ ...AttributeFragment
}
variantAttributes {
- id
- name
+ ...AttributeFragment
}
}
}
@@ -54,20 +66,17 @@ export const TypedProductTypeDetailsQuery = Query as React.ComponentType<
QueryProps<ProductTypeDetails, ProductTypeDetailsVariables>
>;
export const productTypeDetailsQuery = gql`
+ ${attributeFragment}
query ProductTypeDetails($id: ID!) {
productType(id: $id) {
id
name
hasVariants
productAttributes {
- id
- slug
- name
+ ...AttributeFragment
}
variantAttributes {
- id
- slug
- name
+ ...AttributeFragment
}
isShippingRequired
taxRate
@@ -79,13 +88,12 @@ export const TypedSearchAttributeQuery = Query as React.ComponentType<
QueryProps<SearchAttribute, SearchAttributeVariables>
>;
export const searchAttributeQuery = gql`
+ ${attributeFragment}
query SearchAttribute($search: String!) {
attributes(query: $search, first: 5) {
edges {
node {
- id
- slug
- name
+ ...AttributeFragment
}
}
}
diff --git a/saleor/static/dashboard-next/productTypes/types/AttributeFragment.ts b/saleor/static/dashboard-next/productTypes/types/AttributeFragment.ts
new file mode 100644
index 0000000000..49440e54b1
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/types/AttributeFragment.ts
@@ -0,0 +1,21 @@
+/* tslint:disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL fragment: AttributeFragment
+// ====================================================
+
+export interface AttributeFragment_values {
+ __typename: "AttributeValue";
+ id: string;
+ name: string | null;
+ slug: string | null;
+}
+
+export interface AttributeFragment {
+ __typename: "Attribute";
+ id: string;
+ name: string | null;
+ slug: string | null;
+ values: (AttributeFragment_values | null)[] | null;
+}
diff --git a/saleor/static/dashboard-next/productTypes/types/ProductTypeDetails.ts b/saleor/static/dashboard-next/productTypes/types/ProductTypeDetails.ts
index 4529f8032f..d7462c206c 100644
--- a/saleor/static/dashboard-next/productTypes/types/ProductTypeDetails.ts
+++ b/saleor/static/dashboard-next/productTypes/types/ProductTypeDetails.ts
@@ -7,18 +7,34 @@ import { TaxRateType } from "./../../types/globalTypes";
// GraphQL query operation: ProductTypeDetails
// ====================================================
+export interface ProductTypeDetails_productType_productAttributes_values {
+ __typename: "AttributeValue";
+ id: string;
+ name: string | null;
+ slug: string | null;
+}
+
export interface ProductTypeDetails_productType_productAttributes {
__typename: "Attribute";
id: string;
+ name: string | null;
slug: string | null;
+ values: (ProductTypeDetails_productType_productAttributes_values | null)[] | null;
+}
+
+export interface ProductTypeDetails_productType_variantAttributes_values {
+ __typename: "AttributeValue";
+ id: string;
name: string | null;
+ slug: string | null;
}
export interface ProductTypeDetails_productType_variantAttributes {
__typename: "Attribute";
id: string;
- slug: string | null;
name: string | null;
+ slug: string | null;
+ values: (ProductTypeDetails_productType_variantAttributes_values | null)[] | null;
}
export interface ProductTypeDetails_productType {
diff --git a/saleor/static/dashboard-next/productTypes/types/ProductTypeList.ts b/saleor/static/dashboard-next/productTypes/types/ProductTypeList.ts
index 1282953cb3..7ea577132a 100644
--- a/saleor/static/dashboard-next/productTypes/types/ProductTypeList.ts
+++ b/saleor/static/dashboard-next/productTypes/types/ProductTypeList.ts
@@ -5,16 +5,34 @@
// GraphQL query operation: ProductTypeList
// ====================================================
+export interface ProductTypeList_productTypes_edges_node_productAttributes_values {
+ __typename: "AttributeValue";
+ id: string;
+ name: string | null;
+ slug: string | null;
+}
+
export interface ProductTypeList_productTypes_edges_node_productAttributes {
__typename: "Attribute";
id: string;
name: string | null;
+ slug: string | null;
+ values: (ProductTypeList_productTypes_edges_node_productAttributes_values | null)[] | null;
+}
+
+export interface ProductTypeList_productTypes_edges_node_variantAttributes_values {
+ __typename: "AttributeValue";
+ id: string;
+ name: string | null;
+ slug: string | null;
}
export interface ProductTypeList_productTypes_edges_node_variantAttributes {
__typename: "Attribute";
id: string;
name: string | null;
+ slug: string | null;
+ values: (ProductTypeList_productTypes_edges_node_variantAttributes_values | null)[] | null;
}
export interface ProductTypeList_productTypes_edges_node {
diff --git a/saleor/static/dashboard-next/productTypes/types/SearchAttribute.ts b/saleor/static/dashboard-next/productTypes/types/SearchAttribute.ts
index 560abb095d..5fdff37b6b 100644
--- a/saleor/static/dashboard-next/productTypes/types/SearchAttribute.ts
+++ b/saleor/static/dashboard-next/productTypes/types/SearchAttribute.ts
@@ -5,11 +5,19 @@
// GraphQL query operation: SearchAttribute
// ====================================================
+export interface SearchAttribute_attributes_edges_node_values {
+ __typename: "AttributeValue";
+ id: string;
+ name: string | null;
+ slug: string | null;
+}
+
export interface SearchAttribute_attributes_edges_node {
__typename: "Attribute";
id: string;
- slug: string | null;
name: string | null;
+ slug: string | null;
+ values: (SearchAttribute_attributes_edges_node_values | null)[] | null;
}
export interface SearchAttribute_attributes_edges {
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
index 5fb664d4da..2cf6ca38d5 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
@@ -7,7 +7,7 @@ import {
} from "../";
import ErrorMessageCard from "../../components/ErrorMessageCard";
import Navigator from "../../components/Navigator";
-import { createPaginationData, createPaginationState } from "../../misc";
+import { createPaginationData, createPaginationState, maybe } from "../../misc";
import ProductTypeListPage from "../components/ProductTypeListPage";
import { productTypeListQuery, TypedProductTypeListQuery } from "../queries";
@@ -52,11 +52,9 @@ export const ProductTypeList: React.StatelessComponent<
return (
<ProductTypeListPage
disabled={loading}
- productTypes={
- data && data.productTypes
- ? data.productTypes.edges.map(edge => edge.node)
- : undefined
- }
+ productTypes={maybe(() =>
+ data.productTypes.edges.map(edge => edge.node)
+ )}
pageInfo={pageInfo}
onAdd={() => navigate(productTypeAddUrl)}
onNextPage={loadNextPage}
diff --git a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
index 7b6593ca9f..b76f0f6782 100644
--- a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
@@ -35,9 +35,7 @@ interface FormData {
hasVariants: boolean;
id: string;
name: string;
- productAttributes: {
- edges: Array<{ node: AttributeType }>;
- };
+ productAttributes: AttributeType[];
};
};
seoDescription: string;
@@ -61,11 +59,7 @@ interface ProductCreatePageProps {
id: string;
name: string;
hasVariants: boolean;
- productAttributes: {
- edges: Array<{
- node: AttributeType;
- }>;
- };
+ productAttributes: AttributeType[];
}>;
header: string;
saveButtonBarState?: SaveButtonBarState;
@@ -124,9 +118,7 @@ export const ProductCreatePage = decorate<ProductCreatePageProps>(
hasVariants: false,
id: "",
name: "",
- productAttributes: {
- edges: [] as Array<{ node: AttributeType }>
- }
+ productAttributes: [] as AttributeType[]
}
},
seoDescription: "",
diff --git a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
index 206c754153..2cc474bda0 100644
--- a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
@@ -12,14 +12,13 @@ import SingleAutocompleteSelectField from "../../../components/SingleAutocomplet
import SingleSelectField from "../../../components/SingleSelectField";
import Skeleton from "../../../components/Skeleton";
import i18n from "../../../i18n";
+import { maybe } from "../../../misc";
interface ProductType {
hasVariants: boolean;
id: string;
name: string;
- productAttributes: {
- edges: Array<{ node: AttributeType }>;
- };
+ productAttributes: AttributeType[];
}
interface ProductOrganizationProps {
categories?: Array<{ value: string; label: string }>;
@@ -37,9 +36,7 @@ interface ProductOrganizationProps {
hasVariants: boolean;
id: string;
name: string;
- productAttributes: {
- edges: Array<{ node: AttributeType }>;
- };
+ productAttributes: AttributeType[];
};
};
};
@@ -84,16 +81,14 @@ const ProductOrganization = decorate<ProductOrganizationProps>(
productTypes,
onChange
}) => {
- const unrolledAttributes =
- data.productType &&
- data.productType.value &&
- data.productType.value.productAttributes
- ? data.productType.value.productAttributes.edges.map(edge => edge.node)
- : [];
+ const unrolledAttributes = maybe(
+ () => data.productType.value.productAttributes,
+ []
+ );
const getAttributeName = (slug: string) =>
unrolledAttributes.filter(a => a.slug === slug)[0].name;
const getAttributeValue = (slug: string) => {
- if (unrolledAttributes && unrolledAttributes.length > 0) {
+ if (unrolledAttributes.length > 0) {
const value = data.attributes.filter(a => a.slug === slug)[0];
const matches = unrolledAttributes
.filter(a => a.slug === slug)[0]
@@ -126,8 +121,8 @@ const ProductOrganization = decorate<ProductOrganizationProps>(
target: {
...event.target,
name: "attributes",
- value: event.target.value.value.productAttributes.edges.map(edge => ({
- slug: edge.node.slug,
+ value: event.target.value.value.productAttributes.map(attribute => ({
+ slug: attribute.slug,
value: ""
}))
}
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
index ea72c78f37..e1ed51b2e2 100644
--- a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
@@ -184,11 +184,7 @@ export const ProductUpdate = decorate<ProductUpdateProps>(
hasVariants: product.productType.hasVariants,
id: product.productType.id,
name: product.productType.name,
- productAttributes: {
- edges: product.attributes.map(a => ({
- node: a.attribute
- }))
- }
+ productAttributes: product.attributes.map(a => a.attribute)
}
}
: undefined,
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx b/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx
index d653433bda..1112163ef7 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx
@@ -10,6 +10,7 @@ import SaveButtonBar, {
SaveButtonBarState
} from "../../../components/SaveButtonBar";
import i18n from "../../../i18n";
+import { maybe } from "../../../misc";
import ProductVariantAttributes from "../ProductVariantAttributes";
import ProductVariantNavigation from "../ProductVariantNavigation";
import ProductVariantPrice from "../ProductVariantPrice";
@@ -38,11 +39,7 @@ interface ProductVariantCreatePageProps {
};
productType?: {
name?: string;
- variantAttributes?: {
- edges?: Array<{
- node: AttributeType;
- }>;
- };
+ variantAttributes?: AttributeType[];
};
variants?: {
edges?: Array<{
@@ -92,21 +89,14 @@ const ProductVariantCreatePage = decorate<ProductVariantCreatePageProps>(
onVariantClick
}) => {
const initialForm = {
- attributes:
- product &&
- product.productType &&
- product.productType.variantAttributes &&
- product.productType.variantAttributes.edges
- ? product.productType.variantAttributes.edges.map(a => ({
- slug: a.node.slug,
- value: ""
- }))
- : undefined,
+ attributes: maybe(() =>
+ product.productType.variantAttributes.map(attribute => ({
+ slug: attribute.slug,
+ value: ""
+ }))
+ ),
costPrice: "",
- images:
- product && product.images && product.images.edges
- ? product.images.edges.map(edge => edge.node.id)
- : undefined,
+ images: maybe(() => product.images.edges.map(edge => edge.node.id)),
priceOverride: "",
quantity: 0,
sku: ""
@@ -140,16 +130,9 @@ const ProductVariantCreatePage = decorate<ProductVariantCreatePageProps>(
</div>
<div>
<ProductVariantAttributes
- attributes={
- product &&
- product.productType &&
- product.productType.variantAttributes &&
- product.productType.variantAttributes.edges
- ? product.productType.variantAttributes.edges.map(
- edge => edge.node
- )
- : undefined
- }
+ attributes={maybe(
+ () => product.productType.variantAttributes
+ )}
data={data}
disabled={loading}
onChange={change}
diff --git a/saleor/static/dashboard-next/products/views/ProductVariantCreate.tsx b/saleor/static/dashboard-next/products/views/ProductVariantCreate.tsx
index c683e7daed..2f764eac7e 100644
--- a/saleor/static/dashboard-next/products/views/ProductVariantCreate.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductVariantCreate.tsx
@@ -5,7 +5,7 @@ import ErrorMessageCard from "../../components/ErrorMessageCard";
import Messages from "../../components/messages";
import Navigator from "../../components/Navigator";
import i18n from "../../i18n";
-import { decimal } from "../../misc";
+import { decimal, maybe } from "../../misc";
import ProductVariantCreatePage from "../components/ProductVariantCreatePage";
import { TypedVariantCreateMutation } from "../mutations";
import {
@@ -93,20 +93,15 @@ export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
productLoading || variantCreateResult.loading;
return (
<ProductVariantCreatePage
- errors={
- variantCreateResult.data &&
- variantCreateResult.data.productVariantCreate &&
- variantCreateResult.data.productVariantCreate &&
- variantCreateResult.data.productVariantCreate.errors
- ? variantCreateResult.data.productVariantCreate
- .errors
- : []
- }
+ errors={maybe(
+ () =>
+ variantCreateResult.data.productVariantCreate
+ .errors,
+ []
+ )}
header={i18n.t("Add Variant")}
loading={loading}
- product={
- data && data.product ? data.product : undefined
- }
+ product={maybe(() => data.product)}
onBack={handleBack}
onSubmit={handleSubmit}
onVariantClick={handleVariantClick}