618 lines
21 KiB
JavaScript
618 lines
21 KiB
JavaScript
import { defineStore } from 'pinia';
|
|
import { ref, reactive, computed } from 'vue';
|
|
import { a as useConfigStore, ax as dataURLtoFile, aj as supabaseService, d as api$1, ay as updateProduct, az as createProduct } from './server.mjs';
|
|
import { cloneDeep, differenceBy, differenceWith, isEqual } from 'lodash-es';
|
|
|
|
const DEFAULTS = {
|
|
maxWidth: 1200,
|
|
maxHeight: 1200,
|
|
maxBytes: 200 * 1024,
|
|
// 200KB
|
|
startQuality: 0.82,
|
|
minQuality: 0.5
|
|
};
|
|
function fileBaseName(name) {
|
|
return (name || "image").replace(/\.[^.]+$/, "");
|
|
}
|
|
async function decodeToCanvas(inputFile, maxW, maxH) {
|
|
const canvas = (void 0).createElement("canvas");
|
|
const ctx = canvas.getContext("2d", { alpha: true });
|
|
let drew = false, w = 0, h = 0;
|
|
if (typeof createImageBitmap === "function") {
|
|
try {
|
|
const bmp = await createImageBitmap(inputFile);
|
|
w = bmp.width;
|
|
h = bmp.height;
|
|
const s = Math.min(1, maxW / w, maxH / h);
|
|
canvas.width = Math.round(w * s);
|
|
canvas.height = Math.round(h * s);
|
|
ctx.drawImage(bmp, 0, 0, canvas.width, canvas.height);
|
|
bmp.close?.();
|
|
drew = true;
|
|
} catch {
|
|
}
|
|
}
|
|
if (!drew) {
|
|
const img = new Image();
|
|
img.src = URL.createObjectURL(inputFile);
|
|
await img.decode();
|
|
w = img.naturalWidth;
|
|
h = img.naturalHeight;
|
|
const s = Math.min(1, maxW / w, maxH / h);
|
|
canvas.width = Math.round(w * s);
|
|
canvas.height = Math.round(h * s);
|
|
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
|
URL.revokeObjectURL(img.src);
|
|
}
|
|
return canvas;
|
|
}
|
|
async function encodeCanvas(canvas, mime, maxBytes, q0 = DEFAULTS.startQuality, minQ = DEFAULTS.minQuality) {
|
|
const toBlob = (q2) => new Promise((res) => canvas.toBlob(res, mime, q2));
|
|
let q = q0;
|
|
let blob = await toBlob(q);
|
|
if (!blob) throw new Error("encode failed");
|
|
let guard = 0;
|
|
while (blob.size > maxBytes && q > minQ && guard < 12) {
|
|
q -= 0.05;
|
|
blob = await toBlob(q) ?? blob;
|
|
guard++;
|
|
}
|
|
return { blob, quality: q };
|
|
}
|
|
function isWebpRejected(status, text) {
|
|
const t = (text || "").toLowerCase();
|
|
return status === 415 || /invalid file type|unsupported/i.test(t);
|
|
}
|
|
async function postMultipart(endpoint, blob, filename, token) {
|
|
const fd = new FormData();
|
|
fd.append("file", blob, filename);
|
|
const res = await fetch(endpoint, {
|
|
method: "POST",
|
|
headers: { ...token ? { Authorization: `Bearer ${token}` } : {} },
|
|
body: fd
|
|
});
|
|
const text = await res.text();
|
|
return { ok: res.ok, status: res.status, text };
|
|
}
|
|
async function uploadFile(file, opts) {
|
|
try {
|
|
if (!opts?.endpoint) return { error: "uploadFile: endpoint is required" };
|
|
const maxW = opts.maxWidth ?? DEFAULTS.maxWidth;
|
|
const maxH = opts.maxHeight ?? DEFAULTS.maxHeight;
|
|
const maxB = opts.maxBytes ?? DEFAULTS.maxBytes;
|
|
const startQ = opts.startQuality ?? DEFAULTS.startQuality;
|
|
const minQ = opts.minQuality ?? DEFAULTS.minQuality;
|
|
const canvas = await decodeToCanvas(file, maxW, maxH);
|
|
const base = fileBaseName(file.name);
|
|
const webp = await encodeCanvas(canvas, "image/webp", maxB, startQ, minQ);
|
|
const webpName = `${Date.now()}-${base}.webp`;
|
|
const r1 = await postMultipart(opts.endpoint, webp.blob, webpName, opts.authToken);
|
|
if (r1.ok) {
|
|
let json;
|
|
try {
|
|
json = JSON.parse(r1.text);
|
|
} catch {
|
|
json = {};
|
|
}
|
|
const path = json?.path;
|
|
if (!path) return { error: "No path returned" };
|
|
return { data: { fullPath: path }, error: null };
|
|
}
|
|
if (isWebpRejected(r1.status, r1.text)) {
|
|
const jpg = await encodeCanvas(canvas, "image/jpeg", maxB, startQ, minQ);
|
|
const jpgName = `${Date.now()}-${base}.jpg`;
|
|
const r2 = await postMultipart(opts.endpoint, jpg.blob, jpgName, opts.authToken);
|
|
if (r2.ok) {
|
|
let json;
|
|
try {
|
|
json = JSON.parse(r2.text);
|
|
} catch {
|
|
json = {};
|
|
}
|
|
const path = json?.path;
|
|
if (!path) return { error: "No path returned" };
|
|
return { data: { fullPath: path }, error: null };
|
|
}
|
|
return { error: r2.text || `HTTP ${r2.status}` };
|
|
}
|
|
return { error: r1.text || `HTTP ${r1.status}` };
|
|
} catch (e) {
|
|
return { error: e?.message || e };
|
|
}
|
|
}
|
|
const getOneFeature = async (id) => supabaseService.fetch("attributes_active", "select", {
|
|
columns: "*, attribute_values:attribute_values_active(*)",
|
|
field: "id",
|
|
value: id
|
|
});
|
|
const previewImg = ref();
|
|
const previewImgPos = ref();
|
|
const loading = ref(false);
|
|
const priceType = ref(1);
|
|
let temp = [];
|
|
const defaultVariant = ref();
|
|
const myAttributeValue = ref([]);
|
|
const myAttributes = ref([]);
|
|
const mediaFilesUrl = ref([]);
|
|
const original = ref({ url: void 0, index: 0 });
|
|
const originalFile = ref();
|
|
ref();
|
|
const items = ref([]);
|
|
const variants = ref([]);
|
|
const selectedFeatures = ref([]);
|
|
ref([]);
|
|
const formFields = reactive(
|
|
{
|
|
title_fa: "",
|
|
title: "",
|
|
price: 0,
|
|
body: "",
|
|
is_show: true,
|
|
is_active: true,
|
|
box_size: 0,
|
|
sell_type: "numeric",
|
|
original_image: void 0,
|
|
product_attributes: [],
|
|
files: [],
|
|
price_type: "manual",
|
|
auto_calculate: false,
|
|
is_exist: true,
|
|
created_at: void 0,
|
|
updated_at: void 0,
|
|
deleted_at: null,
|
|
count_order: void 0,
|
|
count_fav: void 0,
|
|
count_view: void 0,
|
|
min_price: void 0,
|
|
max_price: void 0,
|
|
type: "simple",
|
|
owner_id: void 0,
|
|
users: void 0,
|
|
saved: [],
|
|
variants: [],
|
|
code: void 0,
|
|
brand_id: void 0,
|
|
// brand: undefined,
|
|
collection_id: void 0,
|
|
collections: {
|
|
parent_id: void 0,
|
|
id: void 0
|
|
},
|
|
discount: void 0,
|
|
discount_type: void 0,
|
|
// main_slider: false,
|
|
// bottom_slider: false,
|
|
id: void 0,
|
|
count: void 0,
|
|
display_price: void 0,
|
|
// weight: null,
|
|
meta: void 0,
|
|
seo_title: void 0,
|
|
description: void 0,
|
|
product_type: void 0,
|
|
weight: void 0,
|
|
rial_fee: void 0,
|
|
percent_fee: void 0
|
|
}
|
|
);
|
|
const baseFormFields = cloneDeep(formFields);
|
|
function resetFormFields() {
|
|
Object.assign(formFields, baseFormFields);
|
|
myAttributes.value = [];
|
|
myAttributeValue.value = [];
|
|
variants.value = [];
|
|
priceType.value = 1;
|
|
original.value = { url: void 0, index: 0 };
|
|
mediaFilesUrl.value = [];
|
|
defaultVariant.value = void 0;
|
|
previewImg.value = void 0;
|
|
originalFile.value = void 0;
|
|
mediaFiles.value = [];
|
|
useProductStore().mediaFiles = [];
|
|
useProductStore().originalFile = void 0;
|
|
if ((formFields.count ?? 0) <= 0) formFields.count = void 0;
|
|
}
|
|
const files = computed(() => {
|
|
let tempMedia = cloneDeep(mediaFilesUrl.value);
|
|
tempMedia?.splice(0, 0, original.value);
|
|
tempMedia = tempMedia?.map((value, index) => {
|
|
return value;
|
|
});
|
|
console.log(tempMedia);
|
|
return tempMedia;
|
|
});
|
|
function handleCropped(src) {
|
|
const i = previewImgPos.value;
|
|
if (!Array.isArray(mediaFilesUrl.value) || i == null || i < 0 || i >= mediaFilesUrl.value.length) {
|
|
console.warn("invalid index or list", i, mediaFilesUrl.value?.length);
|
|
return;
|
|
}
|
|
const oldUrl = mediaFilesUrl.value[i];
|
|
if (oldUrl && oldUrl?.startsWith("blob:")) {
|
|
try {
|
|
URL.revokeObjectURL(oldUrl);
|
|
} catch {
|
|
}
|
|
}
|
|
const file = dataURLtoFile(src, inferFilename(mediaFiles.value[i]));
|
|
const newUrl = URL.createObjectURL(file);
|
|
mediaFiles.value[i] = file;
|
|
mediaFilesUrl.value[i] = newUrl;
|
|
previewImg.value = newUrl;
|
|
}
|
|
function handleProductCropped(src) {
|
|
const store = useProductStore();
|
|
const i = previewImgPos.value;
|
|
if (!src?.startsWith("data:")) return;
|
|
if (i === -2 || previewImg.value === original.value?.url) {
|
|
const oldUrl2 = original.value?.url;
|
|
const prevName = store.originalFile?.file instanceof File && store.originalFile.file.name || filenameFromUrl(oldUrl2, "original.jpg");
|
|
const newFile2 = dataURLtoFile(src, prevName);
|
|
const newUrl2 = URL.createObjectURL(newFile2);
|
|
original.value.url = newUrl2;
|
|
store.originalFile = { file: newFile2, index: 0 };
|
|
safeRevoke(oldUrl2);
|
|
previewImg.value = newUrl2;
|
|
return;
|
|
}
|
|
let pos = typeof i === "number" && i >= 0 && i < mediaFilesUrl.value.length ? i : -1;
|
|
if (pos < 0) {
|
|
pos = mediaFilesUrl.value.findIndex(
|
|
(x) => (typeof x === "string" ? x : x?.url) === previewImg.value
|
|
);
|
|
}
|
|
if (pos < 0) return;
|
|
const entry = mediaFilesUrl.value[pos];
|
|
const oldUrl = typeof entry === "string" ? entry : entry.url;
|
|
const fname = filenameFromUrl(oldUrl, `image-${pos}.jpg`);
|
|
const newFile = dataURLtoFile(src, fname);
|
|
const newUrl = URL.createObjectURL(newFile);
|
|
console.log(newFile, src, 1111111);
|
|
if (typeof entry === "string") {
|
|
mediaFilesUrl.value[pos] = newUrl;
|
|
} else {
|
|
mediaFilesUrl.value[pos] = { ...entry, url: newUrl };
|
|
}
|
|
if (Array.isArray(store.mediaFiles)) {
|
|
if (typeof entry === "string") {
|
|
if (store.mediaFiles[pos] && "file" in store.mediaFiles[pos]) {
|
|
store.mediaFiles[pos].file = newFile;
|
|
} else {
|
|
store.mediaFiles[pos] = newFile;
|
|
}
|
|
} else {
|
|
const mfIdx = store.mediaFiles.findIndex((m) => m.index === entry.index);
|
|
if (mfIdx >= 0) {
|
|
store.mediaFiles[mfIdx].file = newFile;
|
|
} else {
|
|
store.mediaFiles.push({ file: newFile, index: entry.index });
|
|
store.mediaFiles.sort((a, b) => (a.index ?? 0) - (b.index ?? 0));
|
|
}
|
|
}
|
|
}
|
|
safeRevoke(oldUrl);
|
|
previewImg.value = newUrl;
|
|
}
|
|
function safeRevoke(url) {
|
|
if (url && url.startsWith("blob:")) {
|
|
try {
|
|
URL.revokeObjectURL(url);
|
|
} catch {
|
|
}
|
|
}
|
|
}
|
|
function filenameFromUrl(u, fallback = "image.jpg") {
|
|
if (!u) return fallback;
|
|
try {
|
|
const last = new URL(u).pathname.split("/").pop() || "";
|
|
if (/\.[a-z0-9]+$/i.test(last)) return last;
|
|
} catch {
|
|
}
|
|
return fallback;
|
|
}
|
|
function inferFilename(old) {
|
|
if (old instanceof File) return withJPGExt(old.name);
|
|
const fallback = "image.jpg";
|
|
if (old && typeof old === "object" && "name" in old && typeof old.name === "string") {
|
|
return withJPGExt(old.name);
|
|
}
|
|
return fallback;
|
|
}
|
|
function withJPGExt(name) {
|
|
return name && /\.[a-zA-Z0-9]+$/.test(name) ? name : `${name || "image"}.jpg`;
|
|
}
|
|
function addToVariants() {
|
|
let attrIds = [];
|
|
let attrNames = [];
|
|
myAttributeValue.value?.forEach((attributeValue) => {
|
|
if (!attributeValue.valueId) ;
|
|
attrIds.push(attributeValue.valueId);
|
|
attrNames.push(attributeValue.valueName);
|
|
});
|
|
console.log(attrIds, myAttributeValue.value);
|
|
formFields.variants?.push({
|
|
id: void 0,
|
|
attribute_values: [],
|
|
attribute_values_name: [],
|
|
price: 0,
|
|
weight: 0,
|
|
stock: 0,
|
|
auto_calculate: false,
|
|
discount_type: void 0,
|
|
discount: void 0,
|
|
is_default: false,
|
|
sku: ""
|
|
});
|
|
}
|
|
function updateMyAttrs(newValue, flag) {
|
|
let tempAttributes = newValue;
|
|
priceType.value = 1;
|
|
myAttributes.value = [];
|
|
myAttributeValue.value = [];
|
|
let tempIndex = 0;
|
|
let notShowInVarIndexArray = [];
|
|
newValue?.forEach((x, index) => {
|
|
if (x.show_in_variable) {
|
|
priceType.value = 2;
|
|
if (formFields.variants.length === 0)
|
|
addToVariants();
|
|
myAttributes.value.push({
|
|
attributeId: x.id,
|
|
name: x.name,
|
|
values: x.new_feature,
|
|
show: x.show_in_variable,
|
|
valueKey: index
|
|
});
|
|
x.new_feature?.forEach((val, i) => {
|
|
myAttributeValue.value.push({
|
|
attributeName: x.name,
|
|
attributeId: x.id,
|
|
valueId: val.id,
|
|
valueName: val.value,
|
|
valueKey: tempIndex
|
|
});
|
|
});
|
|
} else {
|
|
notShowInVarIndexArray.push(index);
|
|
variants.value?.forEach((val) => {
|
|
if (tempAttributes.length > val.attribute_values.length && val.attribute_values[index] != null) {
|
|
if (val.attribute_values.length != index) {
|
|
val.attribute_values.splice(index, 0, null);
|
|
val.attribute_values_name.splice(index, 0, null);
|
|
} else {
|
|
val.attribute_values = [...val.attribute_values || [], null];
|
|
val.attribute_values_name = [...val.attribute_values_name || [], null];
|
|
}
|
|
}
|
|
});
|
|
}
|
|
tempIndex++;
|
|
variants.value?.forEach((val, ind) => {
|
|
if (tempAttributes.length > val.attribute_values.length && val.attribute_values[index] != null) {
|
|
val.attribute_values = [...val.attribute_values || [], null];
|
|
val.attribute_values_name = [...val.attribute_values_name || [], null];
|
|
}
|
|
});
|
|
});
|
|
sortAttributeByValueKey(variants.value);
|
|
if (notShowInVarIndexArray.length != 0) {
|
|
variants.value?.forEach((val) => {
|
|
notShowInVarIndexArray.forEach((y) => {
|
|
val.attribute_values[y] = null;
|
|
val.attribute_values_name[y] = null;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
function sortAttributeByValueKey(x) {
|
|
x?.forEach((val, ind) => {
|
|
temp = cloneDeep(val.attribute_values);
|
|
if (myAttributes.value.length <= temp.length) {
|
|
temp?.forEach((x2, i) => {
|
|
myAttributeValue.value?.forEach((item, index) => {
|
|
if (temp.find((e) => e == item?.valueId) && val.attribute_values[item.valueKey] != temp.find((e) => e == item.valueId)) {
|
|
val.attribute_values[item.valueKey] = temp.find((e) => e == item.valueId);
|
|
val.attribute_values_name[item.valueKey] = item.valueName;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
if (ind == 0) {
|
|
defaultVariant.value = showDefaultVal(val.attribute_values_name);
|
|
}
|
|
if (val.is_default) {
|
|
defaultVariant.value = showDefaultVal(val.attribute_values_name);
|
|
}
|
|
});
|
|
}
|
|
function showDefaultVal(val) {
|
|
let res = "";
|
|
val.forEach((x, index) => {
|
|
if (x) return res += (index !== 0 ? " - " : "") + x;
|
|
});
|
|
return res;
|
|
}
|
|
async function setItems(new_features) {
|
|
let featureTemp;
|
|
if (new_features[new_features.length - 1] != null)
|
|
featureTemp = await getOneFeature(new_features[new_features.length - 1]);
|
|
let feature;
|
|
feature = featureTemp[0];
|
|
Object.assign(feature, {
|
|
new_feature: [],
|
|
show_in_product: false,
|
|
show_in_variable: false
|
|
});
|
|
items.value.push(feature);
|
|
}
|
|
const findEdit = (val) => {
|
|
return val.split("-")[0] === "edit";
|
|
};
|
|
const firstFiles = ref([]);
|
|
const mediaFiles = ref([]);
|
|
const useProductStore = defineStore("product", () => {
|
|
const originalFile2 = ref();
|
|
const mediaFiles2 = ref([]);
|
|
const percentUpload = ref(0);
|
|
const loading2 = ref(false);
|
|
const isFileChanged = ref(false);
|
|
const config = useConfigStore();
|
|
let progress = 0;
|
|
const fileUpLoader = async (formFields2, id = -1) => {
|
|
loading2.value = true;
|
|
progress = cloneDeep(100 / (mediaFiles2.value.length + 3));
|
|
percentUpload.value = progress;
|
|
if (id != -1) {
|
|
await update(formFields2, id);
|
|
} else {
|
|
await create(formFields2);
|
|
}
|
|
};
|
|
const create = async (formFields2) => {
|
|
let tempFormFields = cloneDeep(formFields2);
|
|
const originalUrl = await uploadFile(originalFile2.value.file, {
|
|
endpoint: config.STORAGE_UPLOAD
|
|
});
|
|
percentUpload.value = progress;
|
|
try {
|
|
const uploadPromises = mediaFiles2.value.map(async (item, index) => {
|
|
const mediaUrl = await uploadFile(item.file, {
|
|
endpoint: config.STORAGE_UPLOAD
|
|
// authToken: getToken(),
|
|
});
|
|
percentUpload.value = progress * (index + 1);
|
|
return { url: mediaUrl.data?.fullPath, type: item.file.type.includes("video") ? "video" : "image" };
|
|
});
|
|
tempFormFields.original_image = originalUrl.data?.fullPath;
|
|
tempFormFields.files.push({
|
|
url: originalUrl.data?.fullPath,
|
|
type: originalFile2.value.file.type.includes("video") ? "video" : "image"
|
|
});
|
|
const uploadedFiles = await Promise.all(uploadPromises);
|
|
tempFormFields.files.push(...uploadedFiles);
|
|
tempFormFields.collection_id = tempFormFields.collection_id ?? tempFormFields.collections?.parent_id;
|
|
tempFormFields.price = tempFormFields.price ? parseInt(tempFormFields.price) : 0;
|
|
tempFormFields.weight = tempFormFields.weight ? parseFloat(tempFormFields.weight) : 0;
|
|
tempFormFields.percent_fee = tempFormFields.percent_fee ? parseFloat(tempFormFields.percent_fee) : 0;
|
|
tempFormFields.rial_fee = tempFormFields.rial_fee ? parseInt(tempFormFields.rial_fee) : 0;
|
|
tempFormFields.count = tempFormFields.count ? parseInt(tempFormFields.count) : void 0;
|
|
tempFormFields.discount = tempFormFields.discount ? parseInt(tempFormFields.discount) : void 0;
|
|
tempFormFields.box_size = parseInt(tempFormFields.box_size);
|
|
tempFormFields.variants?.forEach((val) => {
|
|
val.discount = val.discount ? parseInt(val.discount) : void 0;
|
|
});
|
|
} catch (e) {
|
|
api$1.error(config.useString("theProductImageDidNotLoadCorrectly"));
|
|
percentUpload.value = 0;
|
|
console.error(e);
|
|
throw new Error(config.useString("theProductImageDidNotLoadCorrectly"));
|
|
}
|
|
if (tempFormFields.original_image && !originalUrl.error) {
|
|
try {
|
|
await createProduct(tempFormFields);
|
|
resetFormFields();
|
|
percentUpload.value = 100;
|
|
api$1.success(config.useString("addedSuccessfully"));
|
|
} catch (e) {
|
|
console.error(e);
|
|
api$1.error(e);
|
|
percentUpload.value = 0;
|
|
throw new Error(e);
|
|
} finally {
|
|
loading2.value = false;
|
|
}
|
|
} else {
|
|
api$1.error(config.useString("theProductImageDidNotLoadCorrectly"));
|
|
loading2.value = false;
|
|
percentUpload.value = 0;
|
|
throw new Error(config.useString("theProductImageDidNotLoadCorrectly"));
|
|
}
|
|
resetFormFields();
|
|
mediaFiles2.value = [];
|
|
originalFile2.value = void 0;
|
|
};
|
|
const update = async (formFields2, id) => {
|
|
let indexs;
|
|
let tempFormFields = cloneDeep(formFields2);
|
|
let imagesDeleted = differenceBy(firstFiles.value, files.value, "index");
|
|
imagesDeleted.forEach((x) => {
|
|
firstFiles.value.splice(x.index, 1, null);
|
|
tempFormFields.files.splice(x.index, 1, null);
|
|
});
|
|
firstFiles.value = firstFiles.value.filter((value) => value != null);
|
|
indexs = differenceWith(files.value, firstFiles.value, isEqual);
|
|
let uploadedFiles;
|
|
if (indexs.length != 0) {
|
|
if (originalFile2.value)
|
|
mediaFiles2.value.splice(0, 0, originalFile2.value);
|
|
const uploadPromises = mediaFiles2.value.map(async (item, index) => {
|
|
const mediaUrl = await uploadFile(item.file, {
|
|
endpoint: config.STORAGE_UPLOAD
|
|
});
|
|
percentUpload.value = progress * (index + 1);
|
|
if (mediaUrl.error) {
|
|
api$1.error(config.useString("theProductImageDidNotLoadCorrectly"));
|
|
percentUpload.value = 0;
|
|
throw new Error(config.useString("theProductImageDidNotLoadCorrectly"));
|
|
}
|
|
return {
|
|
value: {
|
|
url: mediaUrl.data?.fullPath,
|
|
type: item.file.type.includes("video") ? "video" : "image"
|
|
},
|
|
id: indexs[index]?.index
|
|
};
|
|
});
|
|
uploadedFiles = await Promise.all(uploadPromises);
|
|
if (!tempFormFields.files) tempFormFields.files = [];
|
|
uploadedFiles.forEach((x, i) => {
|
|
tempFormFields.files[x.id] = x.value;
|
|
});
|
|
}
|
|
tempFormFields.files = tempFormFields.files.filter((value) => value != null);
|
|
tempFormFields.original_image = tempFormFields.files[0].url;
|
|
tempFormFields.price = tempFormFields.price ? parseInt(tempFormFields.price) : void 0;
|
|
tempFormFields.weight = tempFormFields.weight ? parseFloat(tempFormFields.weight) : void 0;
|
|
tempFormFields.percent_fee = tempFormFields.weight ? parseFloat(tempFormFields.percent_fee) : void 0;
|
|
tempFormFields.rial_fee = tempFormFields.rial_fee ? parseInt(tempFormFields.rial_fee) : 0;
|
|
tempFormFields.count = tempFormFields.count ? parseInt(tempFormFields.count) : void 0;
|
|
tempFormFields.discount = tempFormFields.discount ? parseInt(tempFormFields.discount) : void 0;
|
|
tempFormFields.collection_id = tempFormFields.collection_id ?? tempFormFields.collections?.parent_id;
|
|
tempFormFields.collection_id = tempFormFields?.collection_id ?? null;
|
|
delete tempFormFields.collections;
|
|
tempFormFields.variants?.forEach((val) => {
|
|
val.discount = val.discount ? parseInt(val.discount) : void 0;
|
|
});
|
|
tempFormFields.box_size = parseInt(tempFormFields.box_size);
|
|
if (tempFormFields.discount == null)
|
|
tempFormFields.discount = void 0;
|
|
try {
|
|
percentUpload.value = 99.5;
|
|
await updateProduct(tempFormFields, id);
|
|
resetFormFields();
|
|
percentUpload.value = 100;
|
|
api$1.success(config.useString("updatedSuccessfully"));
|
|
} catch (e) {
|
|
console.error(e);
|
|
percentUpload.value = 0;
|
|
api$1.error(e);
|
|
throw new Error(e);
|
|
} finally {
|
|
loading2.value = false;
|
|
}
|
|
mediaFiles2.value = [];
|
|
originalFile2.value = void 0;
|
|
resetFormFields();
|
|
};
|
|
return {
|
|
originalFile: originalFile2,
|
|
percentUpload,
|
|
mediaFiles: mediaFiles2,
|
|
fileUpLoader,
|
|
isFileChanged,
|
|
loading: loading2
|
|
};
|
|
});
|
|
|
|
export { setItems as a, updateMyAttrs as b, myAttributes as c, findEdit as d, handleProductCropped as e, formFields as f, previewImgPos as g, handleCropped as h, items as i, originalFile as j, loading as l, mediaFilesUrl as m, original as o, previewImg as p, resetFormFields as r, selectedFeatures as s, useProductStore as u, variants as v };
|