diff --git a/libs/api/domains/official-journal-of-iceland-application/src/lib/ojoiApplication.resolver.ts b/libs/api/domains/official-journal-of-iceland-application/src/lib/ojoiApplication.resolver.ts index 6b91e5a28f39..f84eab477c40 100644 --- a/libs/api/domains/official-journal-of-iceland-application/src/lib/ojoiApplication.resolver.ts +++ b/libs/api/domains/official-journal-of-iceland-application/src/lib/ojoiApplication.resolver.ts @@ -27,6 +27,7 @@ import { GetUserInvolvedPartiesResponse } from '../models/getUserInvolvedParties import { GetUserInvolvedPartiesInput } from '../models/getUserInvolvedParties.input' import { OJOIAIdInput } from '../models/id.input' import { OJOIAApplicationCaseResponse } from '../models/applicationCase.response' +import { GetPdfResponse } from '../models/getPdf.response' @Scopes(ApiScope.internal) @UseGuards(IdsUserGuard, ScopesGuard) @@ -71,6 +72,13 @@ export class OfficialJournalOfIcelandApplicationResolver { return this.ojoiApplicationService.getPdfUrl(id, user) } + @Query(() => GetPdfResponse, { + name: 'OJOIAGetPdf', + }) + getPdf(@Args('input') input: OJOIAIdInput, @CurrentUser() user: User) { + return this.ojoiApplicationService.getPdf(input, user) + } + @Mutation(() => GetPresignedUrlResponse, { name: 'officialJournalOfIcelandApplicationGetPresignedUrl', }) diff --git a/libs/api/domains/official-journal-of-iceland-application/src/lib/ojoiApplication.service.ts b/libs/api/domains/official-journal-of-iceland-application/src/lib/ojoiApplication.service.ts index 10414ff452b0..b07ba8b38a1f 100644 --- a/libs/api/domains/official-journal-of-iceland-application/src/lib/ojoiApplication.service.ts +++ b/libs/api/domains/official-journal-of-iceland-application/src/lib/ojoiApplication.service.ts @@ -24,6 +24,8 @@ import { GetCommentsResponse, } from '../models/getComments.response' import { OJOIAApplicationCaseResponse } from '../models/applicationCase.response' +import { GetPdfResponse } from '../models/getPdf.response' +import { OJOIAIdInput } from '../models/id.input' const LOG_CATEGORY = 'official-journal-of-iceland-application' @@ -238,4 +240,27 @@ export class OfficialJournalOfIcelandApplicationService { return mapped } + + async getPdf(input: OJOIAIdInput, user: User): Promise { + try { + const data = await this.ojoiApplicationService.getPdf( + { + id: input.id, + }, + user, + ) + + return { + pdf: data.content, + } + } catch (error) { + this.logger.error('Failed to get pdf', { + category: LOG_CATEGORY, + applicationId: input.id, + error: error, + }) + + throw error + } + } } diff --git a/libs/api/domains/official-journal-of-iceland-application/src/models/getPdf.response.ts b/libs/api/domains/official-journal-of-iceland-application/src/models/getPdf.response.ts new file mode 100644 index 000000000000..c0d093ac2498 --- /dev/null +++ b/libs/api/domains/official-journal-of-iceland-application/src/models/getPdf.response.ts @@ -0,0 +1,7 @@ +import { Field, ObjectType } from '@nestjs/graphql' + +@ObjectType('OJOIAGetPdfResponse') +export class GetPdfResponse { + @Field() + pdf!: string +} diff --git a/libs/application/templates/official-journal-of-iceland/src/fields/Preview.tsx b/libs/application/templates/official-journal-of-iceland/src/fields/Preview.tsx index 1097cd41fa1c..3255b2e091e5 100644 --- a/libs/application/templates/official-journal-of-iceland/src/fields/Preview.tsx +++ b/libs/application/templates/official-journal-of-iceland/src/fields/Preview.tsx @@ -4,7 +4,6 @@ import { Bullet, BulletList, Button, - SkeletonLoader, Stack, Text, } from '@island.is/island-ui/core' @@ -14,6 +13,7 @@ import { OJOIFieldBaseProps } from '../lib/types' import { useLocale } from '@island.is/localization' import { HTMLText } from '@island.is/regulations-tools/types' import { + base64ToBlob, getAdvertMarkup, getSignaturesMarkup, parseZodIssue, @@ -23,28 +23,70 @@ import { useApplication } from '../hooks/useUpdateApplication' import { advert, error, preview, signatures } from '../lib/messages' import { useType } from '../hooks/useType' import { - advertValidationSchema, previewValidationSchema, signatureValidationSchema, } from '../lib/dataSchema' import { ZodCustomIssue } from 'zod' +import { usePdf } from '../hooks/usePdf' +import { useState } from 'react' export const Preview = ({ application, goToScreen }: OJOIFieldBaseProps) => { const { application: currentApplication } = useApplication({ applicationId: application.id, }) + const [hasInvalidPdf, setHasInvalidPdf] = useState(false) + const { formatMessage: f } = useLocale() - const { type, loading } = useType({ + const { type } = useType({ typeId: currentApplication.answers.advert?.typeId, }) - if (loading) { - return ( - - ) - } + const { + fetchPdf, + error: pdfError, + loading: pdfLoading, + } = usePdf({ + applicationId: application.id, + onComplete: (data) => { + const blob = base64ToBlob(data.OJOIAGetPdf.pdf) + + if (!blob) { + setHasInvalidPdf(true) + return + } + + const url = URL.createObjectURL(blob) + + let downloadName + const type = currentApplication.answers.advert?.typeName + if (type) { + downloadName = type.replace('.', '') + } + + const title = currentApplication.answers.advert?.title + if (title) { + downloadName += ` ${title}` + } + + if (!downloadName) { + downloadName = `Innsending ${application.id}` + } + + downloadName += '.pdf' + + try { + const anchor = document.createElement('a') + anchor.href = url + anchor.download = downloadName + anchor.click() + anchor.remove() + } finally { + URL.revokeObjectURL(url) + } + }, + }) const advertValidationCheck = previewValidationSchema.safeParse( currentApplication.answers, @@ -75,14 +117,42 @@ export const Preview = ({ application, goToScreen }: OJOIFieldBaseProps) => { ? (`${advertMarkup}
${signatureMarkup}` as HTMLText) : (`${signatureMarkup}` as HTMLText) + const hasError = !( + advertValidationCheck.success && + signatureValidationCheck.success && + !pdfError && + !hasInvalidPdf + ) + return ( -