106 lines
2.6 KiB
TypeScript
106 lines
2.6 KiB
TypeScript
|
|
import * as Yup from "yup";
|
|
import { ImageBaseURL } from "../../api/config";
|
|
|
|
|
|
|
|
export const getInitialValues = (objectToEdit: any) => {
|
|
if (!objectToEdit || !objectToEdit?.products) {
|
|
return {};
|
|
}
|
|
const products = objectToEdit?.products?.map((item: any) => {
|
|
console.log(item,"item");
|
|
|
|
|
|
const formattedData = item?.info ? Object.entries(item?.info).map(([key, value], index) => ({
|
|
[`${index}.Description`]: key,
|
|
[`${index}.key`]: value,
|
|
})) : [];
|
|
|
|
|
|
return ({
|
|
|
|
name_ar: item?.name["ar"],
|
|
name_en: item?.name["en"],
|
|
name_de: item?.name["de"],
|
|
description_ar: item?.description["ar"],
|
|
description_en: item?.description["en"],
|
|
description_de: item?.description["de"],
|
|
images: item?.images,
|
|
main_photo: item?.main_photo,
|
|
price: item?.price,
|
|
quantity: item?.quantity,
|
|
products_attributes: item?.products_attributes,
|
|
id:item?.id,
|
|
info:formattedData
|
|
|
|
})
|
|
})
|
|
|
|
const productsInfo = objectToEdit?.products?.info || {};
|
|
const formattedData = Object.entries(productsInfo).map(([key, value], index) => ({
|
|
[`${index}.Description`]: key,
|
|
[`${index}.key`]: value,
|
|
}));
|
|
console.log(products,"products");
|
|
console.log(objectToEdit,"objectToEdit");
|
|
|
|
|
|
return {
|
|
id:objectToEdit?.id,
|
|
name: objectToEdit?.name ?? "",
|
|
name_ar: objectToEdit?.name?.ar ?? '',
|
|
name_en: objectToEdit?.name?.en ?? '',
|
|
name_de: objectToEdit?.name?.de ?? '',
|
|
description_ar: objectToEdit?.description?.ar ?? '',
|
|
description_en: objectToEdit?.description?.en ?? '',
|
|
description_de: objectToEdit?.description?.de ?? '',
|
|
// attribute: objectToEdit?.attribute ?? "",
|
|
category_id: objectToEdit?.category?.id ?? "",
|
|
price: objectToEdit?.price ?? "",
|
|
|
|
variable: [{}, ...products],
|
|
info: [undefined, ...formattedData] ?? [],
|
|
removedVariant:[],
|
|
}
|
|
};
|
|
|
|
export const getInitialValuesAdd = (objectToEdit: any | null = null) => {
|
|
|
|
return {
|
|
id: "",
|
|
name: "",
|
|
name_ar:'',
|
|
name_en: '',
|
|
name_de: '',
|
|
description_ar:'',
|
|
description_en: '',
|
|
description_de: '',
|
|
price: "",
|
|
category_id: "",
|
|
variable: [],
|
|
info: [],
|
|
|
|
}
|
|
};
|
|
|
|
export const getValidationSchema = (editMode: boolean = false) => {
|
|
// validate input
|
|
return Yup.object().shape({
|
|
name_ar: Yup.string().required('Required'),
|
|
name_en: Yup.string().required('Required'),
|
|
name_de: Yup.string().required('Required'),
|
|
category_id: Yup.string().required('Required'),
|
|
|
|
|
|
|
|
});
|
|
};
|
|
|
|
export const getDataToSend = (values: any): FormData => {
|
|
const data = { ...values };
|
|
|
|
return data;
|
|
};
|
|
|