-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdb71ad
commit 990b4da
Showing
17 changed files
with
289 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 29 additions & 7 deletions
36
...documentDesign/components/DocumentDesignTemplatePreview/DocumentDesignTemplatePreview.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,37 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { components } from '@/api'; | ||
import { ImageWithSkeleton } from '@/ui/imageWithSkeleton'; | ||
import { Box } from '@mui/material'; | ||
import { FileViewer } from '@/ui/FileViewer'; | ||
import { Box, Skeleton } from '@mui/material'; | ||
|
||
export interface DocumentDesignTemplatePreviewProps { | ||
template: components['schemas']['TemplateReceivableResponse']; | ||
getPreview: (id: string) => Promise<string>; | ||
} | ||
|
||
export const DocumentDesignTemplatePreview = ({ | ||
template, | ||
}: DocumentDesignTemplatePreviewProps) => ( | ||
<Box sx={{ width: 595 }}> | ||
<ImageWithSkeleton url={template.preview?.url} alt={template.name} /> | ||
</Box> | ||
); | ||
getPreview, | ||
}: DocumentDesignTemplatePreviewProps) => { | ||
const [preview, setPreview] = useState<string>(); | ||
|
||
useEffect(() => { | ||
const waitForPreview = async () => { | ||
setPreview(await getPreview(template.id)); | ||
}; | ||
|
||
waitForPreview(); | ||
}, [template]); | ||
Check failure on line 24 in packages/sdk-react/src/components/documentDesign/components/DocumentDesignTemplatePreview/DocumentDesignTemplatePreview.tsx
|
||
|
||
return ( | ||
<Box sx={{ width: 595, minHeight: 700 }}> | ||
{!preview && ( | ||
<Skeleton | ||
variant="rectangular" | ||
sx={{ width: '100%', height: '100%' }} | ||
/> | ||
)} | ||
{preview && <FileViewer mimetype="application/pdf" url={preview} />} | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 0 additions & 19 deletions
19
packages/sdk-react/src/components/documentDesign/preview.svg
This file was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
packages/sdk-react/src/components/documentDesign/useDocumentTemplatePreviewLoader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { components } from '@/api'; | ||
import { Services } from '@/api/services'; | ||
import { useMoniteContext, FetchToken } from '@/core/context/MoniteContext'; | ||
|
||
type FetchPdfPreviewParams = { | ||
apiUrl: string; | ||
api: Services; | ||
entityId: string; | ||
version: string; | ||
id: string; | ||
token: string; | ||
}; | ||
|
||
const getTokenCaller = (fetchToken: FetchToken): (() => Promise<string>) => { | ||
let expiresAt: number; | ||
let token: string; | ||
let promise: null | Promise<{ access_token: string; expires_in: number }>; | ||
|
||
return async () => { | ||
const now = Date.now(); | ||
|
||
if (!promise && (!expiresAt || expiresAt < now)) { | ||
promise = fetchToken(); | ||
} | ||
|
||
if (promise) { | ||
const { access_token, expires_in } = await promise; | ||
|
||
expiresAt = now + expires_in; | ||
token = access_token; | ||
|
||
promise = null; | ||
} | ||
|
||
return token; | ||
}; | ||
}; | ||
|
||
const pdfPreveiwStore: Record<string, string> = {}; | ||
|
||
const getPdfPreview = async ({ | ||
apiUrl, | ||
api, | ||
entityId, | ||
version, | ||
id, | ||
token, | ||
}: FetchPdfPreviewParams): Promise<string> => { | ||
if (!pdfPreveiwStore[id]) { | ||
const path = | ||
api.documentTemplates.getDocumentTemplatesIdPreview.schema.url.replace( | ||
/{(.*?)}/g, | ||
id | ||
); | ||
const response = await fetch(`${apiUrl}${path}`, { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
'x-monite-entity-id': entityId, | ||
'x-monite-version': version, | ||
}, | ||
}); | ||
|
||
const blob = await response.blob(); | ||
pdfPreveiwStore[id] = URL.createObjectURL(blob); | ||
} | ||
|
||
return pdfPreveiwStore[id]; | ||
}; | ||
|
||
export const useDocumentTemplatePreviewLoader = () => { | ||
const { api, fetchToken, apiUrl, entityId, version } = useMoniteContext(); | ||
const tokenCaller = getTokenCaller(fetchToken); | ||
|
||
const getPreview = async ( | ||
id: components['schemas']['TemplateReceivableResponse']['id'] | ||
) => { | ||
const token = await tokenCaller(); | ||
const preview = await getPdfPreview({ | ||
api, | ||
apiUrl, | ||
entityId, | ||
version, | ||
id, | ||
token, | ||
}); | ||
|
||
return preview; | ||
}; | ||
|
||
return { | ||
getPreview, | ||
}; | ||
}; |
Oops, something went wrong.