UNPKG

@amsterdam/bmi-component-library

Version:

A React component library based on ASC and Material-UI aimed at repurposing and sharing components across BMI projects

2 lines 4.84 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:true});Object.defineProperty(exports,"default",{enumerable:true,get:function(){return _default}});var _jsxruntime=require("react/jsx-runtime");var _react=require("react");var _ascui=require("@amsterdam/asc-ui");var _DocumentViewerStyles=require("./DocumentViewerStyles");var _DocumentRenderer=_interop_require_default(require("./Renderer/DocumentRenderer"));function _array_like_to_array(arr,len){if(len==null||len>arr.length)len=arr.length;for(var i=0,arr2=new Array(len);i<len;i++)arr2[i]=arr[i];return arr2}function _array_with_holes(arr){if(Array.isArray(arr))return arr}function _define_property(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}function _interop_require_default(obj){return obj&&obj.__esModule?obj:{default:obj}}function _iterable_to_array_limit(arr,i){var _i=arr==null?null:typeof Symbol!=="undefined"&&arr[Symbol.iterator]||arr["@@iterator"];if(_i==null)return;var _arr=[];var _n=true;var _d=false;var _s,_e;try{for(_i=_i.call(arr);!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break}}catch(err){_d=true;_e=err}finally{try{if(!_n&&_i["return"]!=null)_i["return"]()}finally{if(_d)throw _e}}return _arr}function _non_iterable_rest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _object_spread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};var ownKeys=Object.keys(source);if(typeof Object.getOwnPropertySymbols==="function"){ownKeys=ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym){return Object.getOwnPropertyDescriptor(source,sym).enumerable}))}ownKeys.forEach(function(key){_define_property(target,key,source[key])})}return target}function _sliced_to_array(arr,i){return _array_with_holes(arr)||_iterable_to_array_limit(arr,i)||_unsupported_iterable_to_array(arr,i)||_non_iterable_rest()}function _unsupported_iterable_to_array(o,minLen){if(!o)return;if(typeof o==="string")return _array_like_to_array(o,minLen);var n=Object.prototype.toString.call(o).slice(8,-1);if(n==="Object"&&o.constructor)n=o.constructor.name;if(n==="Map"||n==="Set")return Array.from(n);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return _array_like_to_array(o,minLen)}var DocumentViewer=function(param){var uri=param.uri,onFailure=param.onFailure,currentFilename=param.currentFilename;var _useState=_sliced_to_array((0,_react.useState)({loading:true}),2),documentState=_useState[0],setDocumentState=_useState[1];var loading=documentState.loading,filename=documentState.filename,error=documentState.error,contentType=documentState.contentType;var updateDocumentState=function(documentState){setDocumentState(function(prevState){return _object_spread({},prevState,documentState)})};(0,_react.useEffect)(function(){updateDocumentState({loading:true,error:undefined,contentType:undefined});fetch(uri,{method:"GET"}).then(function(response){var _response_headers_get,_response_headers_get1,_filenameParts_;if(!response.ok)throw new Error(response.status===404?"Document niet gevonden.":"Fout bij het ophalen.");var _ref=_sliced_to_array(((_response_headers_get=response.headers.get("content-type"))===null||_response_headers_get===void 0?void 0:_response_headers_get.split(";"))||[],1),firstContentType=_ref[0];if(!firstContentType)throw new Error("Fout bij het ophalen.");var filenameParts=((_response_headers_get1=response.headers.get("content-disposition"))===null||_response_headers_get1===void 0?void 0:_response_headers_get1.split("filename="))||[];var filename=(_filenameParts_=filenameParts[1])===null||_filenameParts_===void 0?void 0:_filenameParts_.replace(/^"(.+(?="$))"$/,"$1");updateDocumentState({loading:false,contentType:firstContentType,filename:filename})}).catch(function(e){updateDocumentState({loading:false,error:e.message});if(onFailure)onFailure(e)})},[uri]);if(loading){return(0,_jsxruntime.jsx)(_DocumentViewerStyles.DocumentViewerStyle,{"data-testid":"document-viewer",children:(0,_jsxruntime.jsx)(_DocumentViewerStyles.SpinnerStyle,{"data-testid":"spinner",color:(0,_ascui.themeColor)("secondary"),size:25})})}return(0,_jsxruntime.jsxs)(_DocumentViewerStyles.DocumentViewerStyle,{"data-testid":"document-viewer",children:[(0,_jsxruntime.jsx)(_ascui.Heading,{forwardedAs:"h3",children:filename!==null&&filename!==void 0?filename:currentFilename}),error&&(0,_jsxruntime.jsx)(_ascui.Alert,{level:"error",outline:true,children:error}),contentType&&(0,_jsxruntime.jsx)(_DocumentRenderer.default,{uri:uri,contentType:contentType})]})};var _default=DocumentViewer; //# sourceMappingURL=DocumentViewer.js.map