-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
8 changed files
with
160 additions
and
93 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Stream } from 'stream' | ||
import { Application } from '~typings/api' | ||
import { format } from '~utils/date' | ||
|
||
export const getBufferFromStream = (stream: Stream): Promise<Buffer> => { | ||
return new Promise((resolve, reject) => { | ||
const chunks: any[] = [] | ||
stream.on('data', (chunk) => chunks.push(chunk)) | ||
stream.on('error', reject) | ||
stream.on('end', () => resolve(Buffer.concat(chunks))) | ||
}) | ||
} | ||
|
||
export const formatApplicationPdfName = (application: Application) => { | ||
//@ts-expect-error | ||
return `${application?.disponibility?.espace?.name | ||
?.split(' ') | ||
.join('_')}_${format( | ||
application?.disponibility?.start, | ||
'dd-MM-yyyy', | ||
)}_${format(application?.disponibility?.end, 'dd-MM-yyyy')}_Ref-${ | ||
application?.id | ||
//@ts-expect-error | ||
}_${application?.disponibility?.espace?.users_permissions_user?.structureName | ||
?.split(' ') | ||
.join('_')}_${application?.campaign?.title?.split(' ').join('_')}.pdf` | ||
} | ||
|
||
export const handleApplicationDownload = async ({ | ||
application, | ||
onError, | ||
}: { | ||
application: Application | ||
onError: () => void | ||
}) => { | ||
const res = await fetch(`/api/pdfs/single/${application.id}`) | ||
if (!res.ok) { | ||
onError() | ||
} | ||
const blob = await res.blob() | ||
const url = window.URL.createObjectURL(blob) | ||
const link = document.createElement('a') | ||
link.href = url | ||
link.setAttribute('download', formatApplicationPdfName(application)) | ||
document.body.appendChild(link) | ||
link.click() | ||
link.parentNode?.removeChild(link) | ||
} |