Skip to content

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

Fix form reloading

dominik-zeglen9b46f8a4fa1b
diff --git a/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx b/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx
index 2e2b39bdbd..9610b74948 100644
--- a/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx
+++ b/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx
@@ -14,6 +14,7 @@ import {
ENTITY_TYPE,
INLINE_STYLE
} from "draftail";
+import isEqual from "lodash-es/isEqual";
import React from "react";
import BoldIcon from "../../icons/BoldIcon";
@@ -29,6 +30,7 @@ import UnorderedListIcon from "../../icons/UnorderedListIcon";
// import ImageEntity from "./ImageEntity";
// import ImageSource from "./ImageSource";
+import { ChangeEvent } from "@saleor/hooks/useForm";
import LinkEntity from "./LinkEntity";
import LinkSource from "./LinkSource";
@@ -240,6 +242,23 @@ const styles = (theme: Theme) =>
marginLeft: 10
}
});
+
+function handleSave(
+ value: any,
+ initial: any,
+ name: string,
+ onChange: (event: ChangeEvent) => void
+) {
+ if (value && !isEqual(value, initial)) {
+ onChange({
+ target: {
+ name,
+ value
+ }
+ });
+ }
+}
+
const RichTextEditor = withStyles(styles, { name: "RichTextEditor" })(
({
classes,
@@ -267,14 +286,7 @@ const RichTextEditor = withStyles(styles, { name: "RichTextEditor" })(
rawContentState={
initial && Object.keys(initial).length > 0 ? initial : null
}
- onSave={value =>
- onChange({
- target: {
- name,
- value
- }
- } as any)
- }
+ onSave={value => handleSave(value, initial, name, onChange)}
blockTypes={[
{
icon: <HeaderOne />,
diff --git a/saleor/static/dashboard-next/hooks/useForm.ts b/saleor/static/dashboard-next/hooks/useForm.ts
index c921ac51ef..a3ea63b352 100644
--- a/saleor/static/dashboard-next/hooks/useForm.ts
+++ b/saleor/static/dashboard-next/hooks/useForm.ts
@@ -35,13 +35,13 @@ function parseErrors(errors: UserError[]): Record<string, string> {
: {};
}
-function useForm<T extends Record<keyof T, any | any[]>>(
+function useForm<T extends Record<string, any | any[]>>(
initial: T,
errors: UserError[],
onSubmit: (data: T) => void
): UseFormResult<T> {
- const [data, setData] = useStateFromProps(initial);
const [hasChanged, setChanged] = useState(false);
+ const [data, setData] = useStateFromProps(initial, () => setChanged(false));
function toggleValue(event: ChangeEvent<T>) {
const { name, value } = event.target;
@@ -61,15 +61,15 @@ function useForm<T extends Record<keyof T, any | any[]>>(
if (!(name in data)) {
console.error(`Unknown form field: ${name}`);
return;
+ } else {
+ if (data[name] !== value) {
+ setChanged(true);
+ }
+ setData(data => ({
+ ...data,
+ [name]: value
+ }));
}
-
- if (!hasChanged) {
- setChanged(true);
- }
- setData({
- ...data,
- [name]: value
- });
}
function reset() {
@@ -77,10 +77,10 @@ function useForm<T extends Record<keyof T, any | any[]>>(
}
function set(newData: Partial<T>) {
- setData({
+ setData(data => ({
...data,
...newData
- });
+ }));
}
function submit() {
diff --git a/saleor/static/dashboard-next/hooks/useStateFromProps.ts b/saleor/static/dashboard-next/hooks/useStateFromProps.ts
index c78e0c8f14..d023c36de4 100644
--- a/saleor/static/dashboard-next/hooks/useStateFromProps.ts
+++ b/saleor/static/dashboard-next/hooks/useStateFromProps.ts
@@ -1,13 +1,19 @@
import isEqual from "lodash-es/isEqual";
import { Dispatch, SetStateAction, useState } from "react";
-function useStateFromProps<T>(data: T): [T, Dispatch<SetStateAction<T>>] {
+function useStateFromProps<T>(
+ data: T,
+ onRefresh?: (data: T) => void
+): [T, Dispatch<SetStateAction<T>>] {
const [state, setState] = useState(data);
- const [prevState, setPrevState] = useState(data);
+ const [prevProps, setPrevProps] = useState(data);
- if (!isEqual(prevState, data)) {
+ if (!isEqual(prevProps, data)) {
setState(data);
- setPrevState(data);
+ setPrevProps(data);
+ if (typeof data === "function") {
+ onRefresh(data);
+ }
}
return [state, setState];
diff --git a/saleor/static/dashboard-next/products/components/ProductDetailsForm/ProductDetailsForm.tsx b/saleor/static/dashboard-next/products/components/ProductDetailsForm/ProductDetailsForm.tsx
index 66fef810b9..9a13586c3f 100644
--- a/saleor/static/dashboard-next/products/components/ProductDetailsForm/ProductDetailsForm.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductDetailsForm/ProductDetailsForm.tsx
@@ -13,8 +13,6 @@ import CardTitle from "@saleor/components/CardTitle";
import FormSpacer from "@saleor/components/FormSpacer";
import RichTextEditor from "@saleor/components/RichTextEditor";
import i18n from "../../../i18n";
-import { maybe } from "../../../misc";
-import { ProductDetails_product } from "../../types/ProductDetails";
import { FormData as CreateFormData } from "../ProductCreatePage";
import { FormData as UpdateFormData } from "../ProductUpdatePage";
@@ -31,50 +29,40 @@ interface ProductDetailsFormProps extends WithStyles<typeof styles> {
data: CreateFormData & UpdateFormData;
disabled?: boolean;
errors: { [key: string]: string };
- product?: ProductDetails_product;
onChange(event: any);
}
export const ProductDetailsForm = withStyles(styles, {
name: "ProductDetailsForm"
-})(
- ({
- classes,
- data,
- disabled,
- errors,
- product,
- onChange
- }: ProductDetailsFormProps) => (
- <Card>
- <CardTitle title={i18n.t("General information")} />
- <CardContent>
- <div className={classes.root}>
- <TextField
- error={!!errors.name}
- helperText={errors.name}
- disabled={disabled}
- fullWidth
- label={i18n.t("Name")}
- name="name"
- rows={5}
- value={data.name}
- onChange={onChange}
- />
- </div>
- <FormSpacer />
- <RichTextEditor
+})(({ classes, data, disabled, errors, onChange }: ProductDetailsFormProps) => (
+ <Card>
+ <CardTitle title={i18n.t("General information")} />
+ <CardContent>
+ <div className={classes.root}>
+ <TextField
+ error={!!errors.name}
+ helperText={errors.name}
disabled={disabled}
- error={!!errors.descriptionJson}
- helperText={errors.descriptionJson}
- initial={maybe(() => JSON.parse(product.descriptionJson), null)}
- label={i18n.t("Description")}
- name="description"
+ fullWidth
+ label={i18n.t("Name")}
+ name="name"
+ rows={5}
+ value={data.name}
onChange={onChange}
/>
- </CardContent>
- </Card>
- )
-);
+ </div>
+ <FormSpacer />
+ <RichTextEditor
+ disabled={disabled}
+ error={!!errors.descriptionJson}
+ helperText={errors.descriptionJson}
+ initial={data.description}
+ label={i18n.t("Description")}
+ name="description"
+ onChange={onChange}
+ />
+ </CardContent>
+ </Card>
+));
ProductDetailsForm.displayName = "ProductDetailsForm";
export default ProductDetailsForm;
diff --git a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
index c747cc53a5..c0f8473dc5 100644
--- a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
@@ -86,7 +86,7 @@ interface ProductOrganizationProps extends WithStyles<typeof styles> {
productTypes?: ProductType[];
fetchCategories: (query: string) => void;
fetchCollections: (query: string) => void;
- onChange: (event: ChangeEvent<string, any>) => void;
+ onChange: (event: ChangeEvent<any>) => void;
onSet: (data: Partial<ProductOrganizationFormData>) => void;
}
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
index 8fc31ac91d..70fade6d7c 100644
--- a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
@@ -138,11 +138,11 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
})),
[]
),
- basePrice: maybe(() => product.basePrice.amount),
- category: maybe(() => ({
- label: product.category.name,
- value: product.category.id
- })),
+ basePrice: maybe(() => product.basePrice.amount, 0),
+ category: {
+ label: maybe(() => product.category.name, ""),
+ value: maybe(() => product.category.id, "")
+ },
chargeTaxes: maybe(() => product.chargeTaxes, false),
collections: productCollections
? productCollections.map(collection => ({
@@ -152,7 +152,7 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
: [],
description: maybe(() => JSON.parse(product.descriptionJson)),
isPublished: maybe(() => product.isPublished, false),
- name: maybe(() => product.name),
+ name: maybe(() => product.name, ""),
productType: maybe(() => ({
label: product.productType.name,
value: {
@@ -162,22 +162,22 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
productAttributes: product.attributes.map(a => a.attribute)
}
})),
- publicationDate: maybe(() => product.publicationDate),
- seoDescription: maybe(() => product.seoDescription) || "",
- seoTitle: maybe(() => product.seoTitle) || "",
+ 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
+ ? 0
: variants && variants[0]
? variants[0].quantity
- : undefined
+ : 0
)
};
const categories =
@@ -217,7 +217,6 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
data={data}
disabled={disabled}
errors={errors}
- product={product}
onChange={change}
/>
<CardSpacer />