feat: implement resolveUrl utility to prepend API base path for document resources
This commit is contained in:
parent
c90eabfe52
commit
880ed06fd5
1 changed files with 12 additions and 4 deletions
|
|
@ -7,6 +7,14 @@ const DocumentViewer = ({ documents, sellerId, refreshDocs }) => {
|
|||
const { token } = useAuth();
|
||||
const [previewDoc, setPreviewDoc] = useState(null);
|
||||
|
||||
const resolveUrl = (url) => {
|
||||
if (!url) return '';
|
||||
if (url.startsWith('/') || url.startsWith('//')) {
|
||||
return `${API_BASE_URL}${url}`;
|
||||
}
|
||||
return url;
|
||||
};
|
||||
|
||||
const handleApprove = async (docId) => {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE_URL}/sellers/${sellerId}/documents/${docId}/approve/`, {
|
||||
|
|
@ -26,7 +34,7 @@ const DocumentViewer = ({ documents, sellerId, refreshDocs }) => {
|
|||
|
||||
const handleDownload = (doc) => {
|
||||
const link = document.createElement('a');
|
||||
link.href = doc.url;
|
||||
link.href = resolveUrl(doc.url);
|
||||
link.download = doc.filename;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
|
|
@ -68,7 +76,7 @@ const DocumentViewer = ({ documents, sellerId, refreshDocs }) => {
|
|||
textAlign: 'center'
|
||||
}}>
|
||||
{doc.filename.match(/\.(jpg|jpeg|png)$/i) ? (
|
||||
<img src={doc.url} alt={doc.type} style={{ maxWidth: '100%', maxHeight: '100%', borderRadius: '4px', objectFit: 'contain' }} />
|
||||
<img src={resolveUrl(doc.url)} alt={doc.type} style={{ maxWidth: '100%', maxHeight: '100%', borderRadius: '4px', objectFit: 'contain' }} />
|
||||
) : (
|
||||
<span>[Document Preview: {doc.filename}]</span>
|
||||
)}
|
||||
|
|
@ -116,13 +124,13 @@ const DocumentViewer = ({ documents, sellerId, refreshDocs }) => {
|
|||
<div className="modal-body" style={{ display: 'flex', justifyContent: 'center', background: '#000', borderRadius: '8px', padding: '16px', overflow: 'hidden' }}>
|
||||
{previewDoc.filename.match(/\.(jpg|jpeg|png)$/i) ? (
|
||||
<img
|
||||
src={previewDoc.url}
|
||||
src={resolveUrl(previewDoc.url)}
|
||||
alt={previewDoc.type}
|
||||
style={{ maxWidth: '100%', maxHeight: '60vh', objectFit: 'contain' }}
|
||||
/>
|
||||
) : previewDoc.filename.match(/\.pdf$/i) ? (
|
||||
<iframe
|
||||
src={previewDoc.url}
|
||||
src={`https://docs.google.com/gview?url=${encodeURIComponent(resolveUrl(previewDoc.url))}&embedded=true`}
|
||||
title={previewDoc.type}
|
||||
style={{ width: '100%', height: '60vh', border: 'none', background: '#fff' }}
|
||||
/>
|
||||
|
|
|
|||
Loading…
Reference in a new issue