Skip to content

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

Clean code

dominik-zeglen496a87788efd
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
index 77788c791a..fb749afa1b 100644
--- a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
@@ -23,21 +23,25 @@ import {
ProductDetails_product_images,
ProductDetails_product_variants
} from "../../types/ProductDetails";
-import ProductAttributes, {
- ProductAttributeInput,
- ProductAttributeInputData
-} from "../ProductAttributes";
+import ProductAttributes, { ProductAttributeInput } from "../ProductAttributes";
import ProductDetailsForm from "../ProductDetailsForm";
import ProductImages from "../ProductImages";
import ProductOrganization from "../ProductOrganization";
import ProductPricing from "../ProductPricing";
import ProductStock from "../ProductStock";
import ProductVariants from "../ProductVariants";
-
-interface Collection {
- id: string;
- label: string;
-}
+import {
+ getAttributeInput,
+ getChoices,
+ getCollectionInput,
+ getFormData,
+ ProductUpdatePageFormData
+} from "./data";
+import {
+ createAttributeChangeHandler,
+ createCategorySelectHandler,
+ createCollectionSelectHandler
+} from "./handlers";
interface ProductUpdatePageProps extends ListActions {
errors: UserError[];
@@ -67,21 +71,7 @@ interface ProductUpdatePageProps extends ListActions {
onVariantAdd?();
}
-export interface FormData {
- basePrice: number;
- category: string | null;
- chargeTaxes: boolean;
- collections: string[];
- description: RawDraftContentState;
- isPublished: boolean;
- name: string;
- publicationDate: string;
- seoDescription: string;
- seoTitle: string;
- sku: string;
- stockQuantity: number;
-}
-export interface ProductUpdatePageSubmitData extends FormData {
+export interface ProductUpdatePageSubmitData extends ProductUpdatePageFormData {
attributes: ProductAttributeInput[];
}
@@ -116,89 +106,25 @@ export const ProductUpdatePage: React.FC<ProductUpdatePageProps> = ({
toggleAll,
toolbar
}) => {
- const { change: changeAttributeData, data: attributes } = useFormset<
- ProductAttributeInputData
- >(
- maybe(
- (): ProductAttributeInput[] =>
- product.attributes.map(attribute => ({
- data: {
- inputType: attribute.attribute.inputType,
- values: attribute.attribute.values
- },
- id: attribute.attribute.id,
- label: attribute.attribute.name,
- value: attribute.value.slug
- })),
- []
- )
+ const { change: changeAttributeData, data: attributes } = useFormset(
+ getAttributeInput(product)
);
const [selectedCategory, setSelectedCategory] = React.useState("");
- const [selectedCollections, setSelectedCollections] = React.useState<
- Collection[]
- >(
- maybe(
- () =>
- productCollections.map(collection => ({
- id: collection.id,
- label: collection.name
- })),
- []
- )
+ const [selectedCollections, setSelectedCollections] = React.useState(
+ getCollectionInput(productCollections)
);
const initialDescription = maybe<RawDraftContentState>(() =>
JSON.parse(product.descriptionJson)
);
+ const initialData = getFormData(product, productCollections, variants);
- const initialData: FormData = {
- basePrice: maybe(() => product.basePrice.amount, 0),
- category: maybe(() => product.category.id),
- chargeTaxes: maybe(() => product.chargeTaxes, false),
- collections: productCollections
- ? productCollections.map(collection => collection.id)
- : [],
- description: initialDescription,
- isPublished: maybe(() => product.isPublished, false),
- name: maybe(() => product.name, ""),
- publicationDate: maybe(() => product.publicationDate, ""),
- seoDescription: maybe(() => product.seoDescription, ""),
- seoTitle: maybe(() => product.seoTitle, ""),
- sku: maybe(() =>
- product.productType.hasVariants
- ? ""
- : variants && variants[0]
- ? variants[0].sku
- : ""
- ),
- stockQuantity: maybe(() =>
- product.productType.hasVariants
- ? 0
- : variants && variants[0]
- ? variants[0].quantity
- : 0
- )
- };
- const categories = maybe(
- () =>
- categoryChoiceList.map(collection => ({
- label: collection.name,
- value: collection.id
- })),
- []
- );
- const collections = maybe(
- () =>
- collectionChoiceList.map(collection => ({
- label: collection.name,
- value: collection.id
- })),
- []
- );
+const categories = getChoices(categoryChoiceList);
+ const collections = getChoices(collectionChoiceList);
const currency = maybe(() => product.basePrice.currency);
const hasVariants = maybe(() => product.productType.hasVariants, false);
- const handleSubmit = (data: FormData) =>
+ const handleSubmit = (data: ProductUpdatePageFormData) =>
onSubmit({
attributes,
...data
@@ -211,42 +137,23 @@ export const ProductUpdatePage: React.FC<ProductUpdatePageProps> = ({
initial={initialData}
confirmLeave
>
- {({ change, data, errors, hasChanged, submit }) => {
- const handleCollectionSelect = (event: React.ChangeEvent<any>) => {
- const id = event.target.value;
- const collectionIndex = data.collections.indexOf(id);
- const collectionList =
- collectionIndex === -1
- ? [
- ...selectedCollections,
- {
- id,
- label: collections.find(
- collection => collection.value === id
- ).label
- }
- ]
- : [
- ...selectedCollections.slice(0, collectionIndex),
- ...selectedCollections.slice(collectionIndex + 1)
- ];
-
- setSelectedCollections(collectionList);
- change({
- target: {
- name: "collections",
- value: collectionList.map(collection => collection.id)
- }
- } as any);
- };
-
- const handleCategorySelect = (event: React.ChangeEvent<any>) => {
- const id = event.target.value;
- setSelectedCategory(
- categoryChoiceList.find(category => category.id === id).name
- );
- change(event);
- };
+ {({ change, data, errors, hasChanged, submit, triggerChange }) => {
+ const handleCollectionSelect = createCollectionSelectHandler(
+ data,
+ collections,
+ selectedCollections,
+ setSelectedCollections,
+ change
+ );
+ const handleCategorySelect = createCategorySelectHandler(
+ categoryChoiceList,
+ setSelectedCategory,
+ change
+ );
+ const handleAttributeChange = createAttributeChangeHandler(
+ changeAttributeData,
+ triggerChange
+ );
return (
<>
@@ -275,7 +182,7 @@ export const ProductUpdatePage: React.FC<ProductUpdatePageProps> = ({
<ProductAttributes
attributes={attributes}
disabled={disabled}
- onChange={changeAttributeData}
+ onChange={handleAttributeChange}
/>
<CardSpacer />
<ProductPricing
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/data.ts b/saleor/static/dashboard-next/products/components/ProductUpdatePage/data.ts
new file mode 100644
index 0000000000..b24145b071
--- /dev/null
+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/data.ts
@@ -0,0 +1,113 @@
+import { RawDraftContentState } from "draft-js";
+
+import { SingleAutocompleteChoiceType } from "@saleor/components/SingleAutocompleteSelectField";
+import { maybe } from "@saleor/misc";
+import {
+ ProductDetails_product,
+ ProductDetails_product_collections,
+ ProductDetails_product_variants
+} from "@saleor/products/types/ProductDetails";
+import { ProductAttributeInput } from "../ProductAttributes";
+
+// TODO: Take attributes from product type
+export function getAttributeInput(
+ product: ProductDetails_product
+): ProductAttributeInput[] {
+ return maybe(
+ (): ProductAttributeInput[] =>
+ product.attributes.map(attribute => ({
+ data: {
+ inputType: attribute.attribute.inputType,
+ values: attribute.attribute.values
+ },
+ id: attribute.attribute.id,
+ label: attribute.attribute.name,
+ value: attribute.value.slug
+ })),
+ []
+ );
+}
+
+export interface Collection {
+ id: string;
+ label: string;
+}
+
+export function getCollectionInput(
+ productCollections: ProductDetails_product_collections[]
+): Collection[] {
+ return maybe(
+ () =>
+ productCollections.map(collection => ({
+ id: collection.id,
+ label: collection.name
+ })),
+ []
+ );
+}
+
+interface Node {
+ id: string;
+ name: string;
+}
+
+export function getChoices(nodes: Node[]): SingleAutocompleteChoiceType[] {
+ return maybe(
+ () =>
+ nodes.map(node => ({
+ label: node.name,
+ value: node.id
+ })),
+ []
+ );
+}
+
+export interface ProductUpdatePageFormData {
+ basePrice: number;
+ category: string | null;
+ chargeTaxes: boolean;
+ collections: string[];
+ description: RawDraftContentState;
+ isPublished: boolean;
+ name: string;
+ publicationDate: string;
+ seoDescription: string;
+ seoTitle: string;
+ sku: string;
+ stockQuantity: number;
+}
+
+export function getFormData(
+ product: ProductDetails_product,
+ productCollections: ProductDetails_product_collections[],
+ variants: ProductDetails_product_variants[]
+): ProductUpdatePageFormData {
+ return {
+ basePrice: maybe(() => product.basePrice.amount),
+ category: maybe(() => product.category.id),
+ chargeTaxes: maybe(() => product.chargeTaxes, false),
+ collections: productCollections
+ ? productCollections.map(collection => collection.id)
+ : [],
+ description: maybe(() => JSON.parse(product.descriptionJson)),
+ isPublished: maybe(() => product.isPublished, false),
+ name: maybe(() => product.name),
+ publicationDate: maybe(() => product.publicationDate),
+ seoDescription: maybe(() => product.seoDescription) || "",
+ seoTitle: maybe(() => product.seoTitle) || "",
+ sku: maybe(() =>
+ product.productType.hasVariants
+ ? undefined
+ : variants && variants[0]
+ ? variants[0].sku
+ : undefined
+ ),
+ stockQuantity: maybe(() =>
+ product.productType.hasVariants
+ ? undefined
+ : variants && variants[0]
+ ? variants[0].quantity
+ : undefined
+ )
+ };
+}
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/handlers.ts b/saleor/static/dashboard-next/products/components/ProductUpdatePage/handlers.ts
new file mode 100644
index 0000000000..68e86967e8
--- /dev/null
+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/handlers.ts
@@ -0,0 +1,64 @@
+import { FormChange } from "@saleor/components/Form";
+import { SingleAutocompleteChoiceType } from "@saleor/components/SingleAutocompleteSelectField";
+import { SearchCategories_categories_edges_node } from "@saleor/containers/SearchCategories/types/SearchCategories";
+import { Collection, ProductUpdatePageFormData } from "./data";
+import { FormsetChange } from "@saleor/hooks/useFormset";
+
+export function createCollectionSelectHandler(
+ data: ProductUpdatePageFormData,
+ collections: SingleAutocompleteChoiceType[],
+ selectedCollections: Collection[],
+ setSelectedCollections: (collections: Collection[]) => void,
+ change: FormChange
+): FormChange {
+ return (event: React.ChangeEvent<any>) => {
+ const id = event.target.value;
+ const collectionIndex = data.collections.indexOf(id);
+ const collectionList =
+ collectionIndex === -1
+ ? [
+ ...selectedCollections,
+ {
+ id,
+ label: collections.find(collection => collection.value === id)
+ .label
+ }
+ ]
+ : [
+ ...selectedCollections.slice(0, collectionIndex),
+ ...selectedCollections.slice(collectionIndex + 1)
+ ];
+
+ setSelectedCollections(collectionList);
+ change({
+ target: {
+ name: "collections",
+ value: collectionList.map(collection => collection.id)
+ }
+ } as any);
+ };
+}
+
+export function createCategorySelectHandler(
+ categoryChoiceList: SearchCategories_categories_edges_node[],
+ setSelectedCategory: (id: string) => void,
+ change: FormChange
+): FormChange {
+ return (event: React.ChangeEvent<any>) => {
+ const id = event.target.value;
+ setSelectedCategory(
+ categoryChoiceList.find(category => category.id === id).name
+ );
+ change(event);
+ };
+}
+
+export function createAttributeChangeHandler(
+ changeAttributeData: FormsetChange,
+ triggerChange: () => void
+): FormsetChange {
+ return (id: string, value: string) => {
+ triggerChange();
+ changeAttributeData(id, value);
+ };
+}