Skip to content

Commit bfaee79

Browse files
committed
Fix: Fix create new product image errors
1 parent 01fc086 commit bfaee79

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

packages/evershop/src/modules/catalog/pages/admin/productEdit/ProductEditForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default function ProductEditForm({
4848
const submit: SubmitHandler<any> = async (data) => {
4949
try {
5050
const images = (data.images || []).map(
51-
(image: { id: string; url: string }) => image.url
51+
(image: { uuid: string; url: string }) => image.url
5252
);
5353
data.images = images;
5454
const response = await fetch(action, {

packages/evershop/src/modules/catalog/pages/admin/productNew/ProductNewForm.tsx

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { FormButtons } from '@components/admin/FormButtons.js';
22
import Area from '@components/common/Area.js';
33
import { Form } from '@components/common/form/Form.js';
44
import React from 'react';
5+
import { SubmitHandler, useForm } from 'react-hook-form';
56
import { toast } from 'react-toastify';
67

78
export default function ProductNewForm({
@@ -11,22 +12,46 @@ export default function ProductNewForm({
1112
action: string;
1213
gridUrl: string;
1314
}) {
15+
const form = useForm({
16+
shouldUnregister: true
17+
});
18+
const submit: SubmitHandler<any> = async (data) => {
19+
try {
20+
const images = (data.images || []).map(
21+
(image: { uuid: string; url: string }) => image.url
22+
);
23+
data.images = images;
24+
const response = await fetch(action, {
25+
method: 'POST',
26+
headers: {
27+
'Content-Type': 'application/json'
28+
},
29+
body: JSON.stringify({ ...data, action: undefined, method: undefined })
30+
});
31+
const result = await response.json();
32+
if (result.error) {
33+
toast.error(result.error.message);
34+
} else {
35+
toast.success('Product created successfully');
36+
const editUrl = result.data.links.find(
37+
(link) => link.rel === 'edit'
38+
).href;
39+
setTimeout(() => {
40+
window.location.href = editUrl;
41+
}, 1500);
42+
}
43+
} catch (error) {
44+
toast.error(error.message);
45+
}
46+
};
1447
return (
1548
<Form
1649
id="productNewForm"
1750
method="POST"
1851
action={action}
52+
form={form}
53+
onSubmit={submit}
1954
submitBtn={false}
20-
onSuccess={(response) => {
21-
toast.success('Product created successfully!');
22-
const editUrl = response.data.links.find(
23-
(link) => link.rel === 'edit'
24-
).href;
25-
window.location.href = editUrl;
26-
setTimeout(() => {
27-
window.location.href = gridUrl;
28-
}, 1500);
29-
}}
3055
>
3156
<div className="grid grid-cols-3 gap-x-5 grid-flow-row ">
3257
<div className="col-span-2 grid grid-cols-1 gap-5 auto-rows-max">

0 commit comments

Comments
 (0)