Skip to content

Commit

Permalink
[KAIZEN-0] Støtte jpg og png i saksdokument visning
Browse files Browse the repository at this point in the history
  • Loading branch information
abrhanav committed Aug 25, 2023
1 parent b1227e4 commit 07b0a15
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function DokumentVisning(props: Props) {
const url = getMockableUrl(byggDokumentVisningUrl(props.url, props.fnr));

return (
<ObjectHttpFeilHandtering type="application/pdf" url={url} width="100%" height="100%" onError={onError}>
<ObjectHttpFeilHandtering url={url} width="100%" height="100%" onError={onError}>
<ErrorStyle>
<AlertStripeAdvarsel>{errMsg}</AlertStripeAdvarsel>
</ErrorStyle>
Expand Down
9 changes: 6 additions & 3 deletions src/components/ObjectHttpFeilHandtering.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@ interface Props

export function ObjectHttpFeilHandtering({ url, onError, children, ...rest }: Props) {
const [blobUrl, setBlobUrl] = useState('');
const [contentType, setContentType] = useState('');
const [isError, setError] = useState(false);

useEffect(() => {
let objectUrl = '';
fetch(url)
.then(res => {
.then((res) => {
if (!res.ok) {
setError(true);
onError(res.status);
} else {
setContentType(res.headers.get('Content-Type') ?? 'application/pdf');
setError(false);
}
return res.blob();
})
.then(blob => {
.then((blob) => {
objectUrl = URL.createObjectURL(blob);

setBlobUrl(objectUrl);
});

Expand All @@ -42,5 +45,5 @@ export function ObjectHttpFeilHandtering({ url, onError, children, ...rest }: Pr
return <>{children}</>;
}

return <object data={blobUrl} children={children} {...rest} />;
return <object data={blobUrl} children={children} {...rest} type={contentType} />;
}

0 comments on commit 07b0a15

Please sign in to comment.