Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KAIZEN-0] Støtte jpg og png i saksdokument visning #2058

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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} />;
}