From 14ac3dbd42e7fce675165e365c5fbf1a077d6cb0 Mon Sep 17 00:00:00 2001 From: JeremiahUy Date: Tue, 19 Nov 2024 14:48:14 +0100 Subject: [PATCH] ADD: logic for parsing and transforming of file from backend Co-authored-by: andregroseth --- apps/frontend/package.json | 1 + .../src/api/BehandlingensLivslopApi.ts | 18 +++++++++++++++++- .../behandlingensLivlop/CustomFileUpload.tsx | 2 +- .../src/pages/BehandlingensLivslopPage.tsx | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/apps/frontend/package.json b/apps/frontend/package.json index 3638ea084..6d4dd2286 100644 --- a/apps/frontend/package.json +++ b/apps/frontend/package.json @@ -19,6 +19,7 @@ "@types/react-draft-wysiwyg": "^1.13.5", "amplitude-js": "^8.21.9", "axios": "^1.5.1", + "buffer": "^6.0.3", "core-js": "^3.32.2", "depcheck": "^1.4.7", "draft-js": "^0.11.7", diff --git a/apps/frontend/src/api/BehandlingensLivslopApi.ts b/apps/frontend/src/api/BehandlingensLivslopApi.ts index 278608c34..369789c7e 100644 --- a/apps/frontend/src/api/BehandlingensLivslopApi.ts +++ b/apps/frontend/src/api/BehandlingensLivslopApi.ts @@ -1,4 +1,5 @@ import axios from 'axios' +import { Buffer } from 'buffer' import { useEffect, useState } from 'react' import { IBehandlingensLivslop, IBehandlingensLivslopRequest, IPageResponse } from '../constants' import { env } from '../util/env' @@ -166,12 +167,27 @@ export const mapBehandlingensLivslopToFormValue = ( export const mapBehandlingensLivslopRequestToFormValue = ( behandlingensLivslop: Partial ): IBehandlingensLivslopRequest => { + const filer: File[] = [] + if ( + behandlingensLivslop && + behandlingensLivslop.filer && + behandlingensLivslop.filer?.length > 0 + ) { + behandlingensLivslop.filer.forEach((fil) => { + const decodedBase64File = Buffer.from(fil.fil, 'base64') + const parsedFile = Uint8Array.from(decodedBase64File) + const blob = new Blob([parsedFile], { type: fil.filtype }) + const file = new File([blob], fil.filnavn, { type: fil.filtype }) + filer.push(file) + }) + } + return { id: behandlingensLivslop.id || '', changeStamp: behandlingensLivslop.changeStamp || { lastModifiedDate: '', lastModifiedBy: '' }, version: -1, etterlevelseDokumentasjonId: behandlingensLivslop.etterlevelseDokumentasjonId || '', beskrivelse: behandlingensLivslop.beskrivelse || '', - filer: [], + filer: filer, } } diff --git a/apps/frontend/src/components/behandlingensLivlop/CustomFileUpload.tsx b/apps/frontend/src/components/behandlingensLivlop/CustomFileUpload.tsx index bbf67c1a7..f4a416597 100644 --- a/apps/frontend/src/components/behandlingensLivlop/CustomFileUpload.tsx +++ b/apps/frontend/src/components/behandlingensLivlop/CustomFileUpload.tsx @@ -36,7 +36,7 @@ export const CustomFileUpload = (props: IProps) => { }) setFiles(initialFiles) } - }, []) + }, [initialValues]) const removeFile = (fileToRemove: FileObject) => { setFiles(files.filter((file) => file !== fileToRemove)) diff --git a/apps/frontend/src/pages/BehandlingensLivslopPage.tsx b/apps/frontend/src/pages/BehandlingensLivslopPage.tsx index 803af60f1..8caf5ef6f 100644 --- a/apps/frontend/src/pages/BehandlingensLivslopPage.tsx +++ b/apps/frontend/src/pages/BehandlingensLivslopPage.tsx @@ -181,7 +181,7 @@ export const BehandlingensLivslopPage = () => { behandlingsLivslop as IBehandlingensLivslop )} > - {({ initialValues, submitForm }) => ( + {({ submitForm, initialValues }) => (