Untitled

 avatar
unknown
plain_text
a month ago
2.1 kB
3
Indexable
const updatePaymentDetail = (values, selectedFile) => {
    // Validate the file (if any)
    if (selectedFile && selectedFile.length > 0 && !docUploadValidation(selectedFile)) {
        toast.error("Invalid file upload");
        return;
    }

    const formData = new FormData();

    // Build paymentMethodsDTO from form values
    const paymentMethodsDTO = {
        id: values.id,
        title: values.title,
        description: values.description,
        status: values.status,
        minimum: values.minimum,
        maximum: values.maximum,
        processingTime: values.processingTime,
        transactionFee: values.transactionFee,
        paymentMethod: values.paymentMethod,
        networkData: values.networkData,
    };
    formData.append('paymentMethodsDTO', JSON.stringify(paymentMethodsDTO));
    console.log("paymentMethodsDTO:", paymentMethodsDTO);

    // Handle the file (if new file uploaded, otherwise keep the original file or none)
    if (selectedFile && selectedFile.length > 0) {
        formData.append('file', selectedFile[0].file); // Add the new file
        console.log("Uploaded file:", selectedFile[0].file);
    } else if (values.document) {
        // Include the original file URL as is
        const originalFile = new File([""], values.document, { type: "application/octet-stream" });
        formData.append('file', originalFile);
        console.log("Using original file URL:", values.document);
    } else {
        // No file to include
        console.warn("No file to upload");
    }

    // Send the request
    api.put(url.ADMIN_UPDATE_PAYMENT_DETAIL, formData)
        .then((response) => {
            const res = response.data;
            if (res.code === 0) {
                getPaymentDetails(); // Refresh data on success
                toast.success(res.message);
            } else {
                toast.error(res.message);
            }
        })
        .catch((error) => {
            console.error("Update error:", error);
            toast.error("Something went wrong. Please try again later.");
        });
};
Leave a Comment