fix attibute edit
This commit is contained in:
@@ -42,10 +42,7 @@ const UpdateProduct: FC = () => {
|
|||||||
seoTitle: product.seoTitle || '',
|
seoTitle: product.seoTitle || '',
|
||||||
source: product.source,
|
source: product.source,
|
||||||
isFake: product.isFake,
|
isFake: product.isFake,
|
||||||
attributes: product.specifications?.map(spec => ({
|
attributes: [],
|
||||||
id: 0, // This might need to be adjusted based on API
|
|
||||||
values: spec.values.map(() => 0) // This might need to be adjusted based on API
|
|
||||||
})) || [],
|
|
||||||
description: product.description,
|
description: product.description,
|
||||||
metaDescription: product.metaDescription,
|
metaDescription: product.metaDescription,
|
||||||
tags: product.tags,
|
tags: product.tags,
|
||||||
@@ -164,6 +161,7 @@ const UpdateProduct: FC = () => {
|
|||||||
loading={updateProductAttributeMutation.isPending}
|
loading={updateProductAttributeMutation.isPending}
|
||||||
onPrevious={handlePreviousStep}
|
onPrevious={handlePreviousStep}
|
||||||
categoryId={productData.category}
|
categoryId={productData.category}
|
||||||
|
specifications={productDetails?.results?.product?.specifications}
|
||||||
initialData={{
|
initialData={{
|
||||||
attributes: productData.attributes,
|
attributes: productData.attributes,
|
||||||
description: productData.description,
|
description: productData.description,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { type FC, useState } from 'react';
|
import { type FC, useState, useEffect } from 'react';
|
||||||
import Button from '../../../components/Button';
|
import Button from '../../../components/Button';
|
||||||
import Input from '../../../components/Input';
|
import Input from '../../../components/Input';
|
||||||
import Textarea from '../../../components/Textarea';
|
import Textarea from '../../../components/Textarea';
|
||||||
@@ -46,7 +46,8 @@ const AttributeSelector: FC<AttributeSelectorProps> = ({ attribute, selectedValu
|
|||||||
|
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
{attribute.values.map((value) => {
|
{attribute.values.map((value) => {
|
||||||
const isChecked = selectedValues.includes(value.text);
|
const valueIdStr = String(value._id);
|
||||||
|
const isChecked = selectedValues.includes(valueIdStr) || selectedValues.includes(value.text);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<label key={value._id} className="flex items-center space-x-2 cursor-pointer">
|
<label key={value._id} className="flex items-center space-x-2 cursor-pointer">
|
||||||
@@ -54,7 +55,7 @@ const AttributeSelector: FC<AttributeSelectorProps> = ({ attribute, selectedValu
|
|||||||
type={attribute.multiple ? "checkbox" : "radio"}
|
type={attribute.multiple ? "checkbox" : "radio"}
|
||||||
name={`attribute-${attribute._id}`}
|
name={`attribute-${attribute._id}`}
|
||||||
checked={isChecked}
|
checked={isChecked}
|
||||||
onChange={(e) => handleValueChange(value.text, e.target.checked)}
|
onChange={(e) => handleValueChange(valueIdStr, e.target.checked)}
|
||||||
className={`w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 focus:ring-2 ${attribute.multiple ? 'rounded' : 'rounded-full'
|
className={`w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 focus:ring-2 ${attribute.multiple ? 'rounded' : 'rounded-full'
|
||||||
}`}
|
}`}
|
||||||
/>
|
/>
|
||||||
@@ -122,6 +123,7 @@ interface Step2FormProps {
|
|||||||
onPrevious: () => void;
|
onPrevious: () => void;
|
||||||
categoryId?: string;
|
categoryId?: string;
|
||||||
initialData?: Partial<Omit<CreateProductAttributeRequestType, 'productId'>>;
|
initialData?: Partial<Omit<CreateProductAttributeRequestType, 'productId'>>;
|
||||||
|
specifications?: Array<{ title: string; values: string[] }>;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AttributeWithValue {
|
interface AttributeWithValue {
|
||||||
@@ -258,7 +260,7 @@ const ListInput: FC<ListInputProps> = ({ items, onChange, placeholder, label })
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const Step2Form: FC<Step2FormProps> = ({ onSubmit, loading, onPrevious, categoryId, initialData }) => {
|
const Step2Form: FC<Step2FormProps> = ({ onSubmit, loading, onPrevious, categoryId, initialData, specifications }) => {
|
||||||
const [formData, setFormData] = useState<Omit<CreateProductAttributeRequestType, 'productId'>>(() => ({
|
const [formData, setFormData] = useState<Omit<CreateProductAttributeRequestType, 'productId'>>(() => ({
|
||||||
attributes: (initialData?.attributes || []) as AttributeWithValue[],
|
attributes: (initialData?.attributes || []) as AttributeWithValue[],
|
||||||
description: initialData?.description || '',
|
description: initialData?.description || '',
|
||||||
@@ -270,6 +272,51 @@ const Step2Form: FC<Step2FormProps> = ({ onSubmit, loading, onPrevious, category
|
|||||||
|
|
||||||
const { data: categoryAttributesData } = useGetCategoryAttributes(categoryId || '');
|
const { data: categoryAttributesData } = useGetCategoryAttributes(categoryId || '');
|
||||||
|
|
||||||
|
// تبدیل specifications به attributes با استفاده از categoryAttributes
|
||||||
|
useEffect(() => {
|
||||||
|
if (specifications && categoryAttributesData?.results?.data?.category?.attributes) {
|
||||||
|
const categoryAttrs = categoryAttributesData.results.data.category.attributes;
|
||||||
|
const convertedAttributes: AttributeWithValue[] = [];
|
||||||
|
|
||||||
|
specifications.forEach(spec => {
|
||||||
|
// پیدا کردن attribute مربوطه از روی title
|
||||||
|
const matchedAttr = categoryAttrs.find(attr => attr.title === spec.title);
|
||||||
|
|
||||||
|
if (matchedAttr) {
|
||||||
|
// برای attributeهای انتخابی، باید value.text رو با spec.values مچ کنیم
|
||||||
|
if (matchedAttr.type === 'select') {
|
||||||
|
const matchedValueIds: string[] = [];
|
||||||
|
|
||||||
|
spec.values.forEach(specValue => {
|
||||||
|
const matchedValue = matchedAttr.values.find(v => v.text === specValue);
|
||||||
|
if (matchedValue) {
|
||||||
|
matchedValueIds.push(String(matchedValue._id));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (matchedValueIds.length > 0) {
|
||||||
|
convertedAttributes.push({
|
||||||
|
id: matchedAttr._id,
|
||||||
|
values: matchedValueIds
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// برای text، number، boolean مستقیماً مقدار رو استفاده میکنیم
|
||||||
|
convertedAttributes.push({
|
||||||
|
id: matchedAttr._id,
|
||||||
|
values: spec.values
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
setFormData(prev => ({
|
||||||
|
...prev,
|
||||||
|
attributes: convertedAttributes
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}, [specifications, categoryAttributesData]);
|
||||||
|
|
||||||
const handleAttributeChange = (attributeId: number, values: string[]) => {
|
const handleAttributeChange = (attributeId: number, values: string[]) => {
|
||||||
setFormData(prev => ({
|
setFormData(prev => ({
|
||||||
...prev,
|
...prev,
|
||||||
|
|||||||
Reference in New Issue
Block a user