import { ISerializedForm } from '@client/forms' ; import { ILanguage } from '@client/i18n/reducer' ; import { ILocation } from '@client/offline/reducer' ; import { IPDFTemplate } from '@client/pdfRenderer/transformer/types' ; import { getToken } from '@client/utils/authUtils' ; import { ICertificateCollectorDefinition } from '@client/views/PrintCertificate/VerifyCollector' ; export interface ILocationDataResponse { [ locationId ] : ILocation ; } export interface IFacilitiesDataResponse { [ facilityId ] : ILocation ; } export interface IDefinitionsResponse { languages : ILanguage [ ] ; forms : { registerForm : { birth : ISerializedForm ; death : ISerializedForm ; } ; certificateCollectorDefinition : { birth : ICertificateCollectorDefinition ; death : ICertificateCollectorDefinition ; } ; userForm : ISerializedForm ; } ; templates : { receipt ? : IPDFTemplate ; certificates : { birth : IPDFTemplate ; death : IPDFTemplate ; } ; } ; } export interface IAssetResponse { logo : string ; } async function loadDefinitions ( ) { const url = `${ window . config . RESOURCES_URL }/definitions/client` ; const res = await fetch ( url , { method : 'GET' , headers : { Authorization : `Bearer ${ getToken ( ) }` } } ) ; if ( res && res . status !== 200 ) { throw Error ( res . statusText ) ; } const response = await res . json ( ) ; return response ; } async function loadLocations ( ) { const url = `${ window . config . RESOURCES_URL }/locations` ; const res = await fetch ( url , { method : 'GET' , headers : { Authorization : `Bearer ${ getToken ( ) }` } } ) ; if ( res && res . status !== 200 ) { throw Error ( res . statusText ) ; } const response = await res . json ( ) ; return response . data ; } async function loadFacilities ( ) { const url = `${ window . config . RESOURCES_URL }/facilities` ; const res = await fetch ( url , { method : 'GET' , headers : { Authorization : `Bearer ${ getToken ( ) }` } } ) ; if ( res && res . status !== 200 ) { throw Error ( res . statusText ) ; } const response = await res . json ( ) ; return response . data ; } async function loadPilotLocations ( ) { const url = `${ window . config . RESOURCES_URL }/pilotLocations` ; const res = await fetch ( url , { method : 'GET' , headers : { Authorization : `Bearer ${ getToken ( ) }` } } ) ; if ( res && res . status !== 200 ) { throw Error ( res . statusText ) ; } const response = await res . json ( ) ; return response . data ; } const toDataURL = ( url ) => fetch ( url ) . then ( ( response ) => response . blob ( ) ) . then ( ( blob ) => new Promise ( ( resolve , reject ) => { const reader = new FileReader ( ) ; reader . onloadend = ( ) => resolve ( reader . result ) ; reader . onerror = reject ; reader . readAsDataURL ( blob ) ; } ) ) ; async function loadAssets ( ) { const url = `${ window . config . RESOURCES_URL }/assets/${ window . config . COUNTRY_LOGO_FILE }` ; return toDataURL ( url ) . then ( ( dataUrl ) => { return { logo : `${ dataUrl }` } ; } ) ; } export const referenceApi = { loadLocations , loadFacilities , loadPilotLocations , loadDefinitions , loadAssets } ;