fix attibute edit

This commit is contained in:
hamid zarghami
2025-10-26 11:24:14 +03:30
parent 40fb5a6c15
commit 1e48d12340
2 changed files with 53 additions and 8 deletions
+2 -4
View File
@@ -42,10 +42,7 @@ const UpdateProduct: FC = () => {
seoTitle: product.seoTitle || '',
source: product.source,
isFake: product.isFake,
attributes: product.specifications?.map(spec => ({
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
})) || [],
attributes: [],
description: product.description,
metaDescription: product.metaDescription,
tags: product.tags,
@@ -164,6 +161,7 @@ const UpdateProduct: FC = () => {
loading={updateProductAttributeMutation.isPending}
onPrevious={handlePreviousStep}
categoryId={productData.category}
specifications={productDetails?.results?.product?.specifications}
initialData={{
attributes: productData.attributes,
description: productData.description,
+51 -4
View File
@@ -1,4 +1,4 @@
import { type FC, useState } from 'react';
import { type FC, useState, useEffect } from 'react';
import Button from '../../../components/Button';
import Input from '../../../components/Input';
import Textarea from '../../../components/Textarea';
@@ -46,7 +46,8 @@ const AttributeSelector: FC<AttributeSelectorProps> = ({ attribute, selectedValu
<div className="space-y-2">
{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 (
<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"}
name={`attribute-${attribute._id}`}
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'
}`}
/>
@@ -122,6 +123,7 @@ interface Step2FormProps {
onPrevious: () => void;
categoryId?: string;
initialData?: Partial<Omit<CreateProductAttributeRequestType, 'productId'>>;
specifications?: Array<{ title: string; values: string[] }>;
}
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'>>(() => ({
attributes: (initialData?.attributes || []) as AttributeWithValue[],
description: initialData?.description || '',
@@ -270,6 +272,51 @@ const Step2Form: FC<Step2FormProps> = ({ onSubmit, loading, onPrevious, category
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[]) => {
setFormData(prev => ({
...prev,