META-INF.resources.js.info_box.PurchaseOrderDocumentView.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.commerce.order.content.web
Show all versions of com.liferay.commerce.order.content.web
Liferay Commerce Order Content Web
The newest version!
/**
* SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
import ClayButton, {ClayButtonWithIcon} from '@clayui/button';
import ClayIcon from '@clayui/icon';
import ClayLink from '@clayui/link';
import {CommerceServiceProvider} from 'commerce-frontend-js';
import {openToast, sub} from 'frontend-js-web';
import React, {useRef, useState} from 'react';
import {isEditable} from '../util';
function getBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result.split(',').pop());
reader.onerror = (error) => reject(error);
});
}
const PurchaseOrderDocumentView = ({
additionalProps,
buttonDisplayType,
elementId,
field,
fieldValue,
hasPermission,
isOpen,
label,
namespace,
orderId,
readOnly,
spritemap,
}) => {
const [downloadURL, setDownloadURL] = useState(
additionalProps?.downloadURL ? additionalProps?.downloadURL : null
);
const inputFileRef = useRef(null);
const [inputValue, setInputValue] = useState(
additionalProps?.value ? additionalProps?.value : null
);
const [value, setValue] = useState(fieldValue);
const addAttachment = async (file) => {
CommerceServiceProvider.DeliveryCartAPI('v1')
.addAttachment(orderId, {
attachment: await getBase64(file),
title: file.name,
})
.then((response) => {
setDownloadURL(response.url);
setInputValue(response.id);
setValue(response.title);
})
.catch((error) => {
openToast({
message:
error.message ||
Liferay.Language.get('an-unexpected-error-occurred'),
type: 'danger',
});
});
};
const deleteAttachment = async (attachmentId) => {
return CommerceServiceProvider.DeliveryCartAPI('v1')
.deleteAttachment(orderId, attachmentId)
.then(() => {
setDownloadURL(null);
setInputValue(null);
setValue('');
})
.catch((error) => {
openToast({
message:
error.message ||
Liferay.Language.get('an-unexpected-error-occurred'),
type: 'danger',
});
});
};
const handleChangeFile = async (event) => {
event.preventDefault();
event.stopPropagation();
const file = event.target.files[0];
if (inputValue) {
deleteAttachment(inputValue).then(async () => {
await addAttachment(file);
});
}
else {
await addAttachment(file);
}
};
const handleDeleteFile = async (event) => {
event.preventDefault();
await deleteAttachment(inputValue);
};
const handleFileChooser = async (event) => {
event.preventDefault();
inputFileRef.current.click();
};
return (
{hasPermission && !readOnly && isEditable(field, isOpen) ? (
) : null}
{label ? (
{label}
) : null}
{hasPermission &&
!readOnly &&
!value &&
isEditable(field, isOpen) ? (
{Liferay.Language.get('add')}
) : null}
{value && (
{value}
)}
{hasPermission &&
!readOnly &&
value &&
isEditable(field, isOpen) ? (
) : null}
);
};
export default PurchaseOrderDocumentView;
© 2015 - 2025 Weber Informatics LLC | Privacy Policy