UNPKG

@postnord/web-components

Version:

PostNord Web Components

6 lines 3.9 kB
/*! * Built with Stencil * By PostNord. */ import{r as e,g as n,h as t,a as r}from"./p-c2c6299e.js";import{j as i,s}from"./p-c511b4fb.js";const a={SEARCH_BUTTON:{da:"Search",en:"Search",fi:"Search",no:"Search",sv:"Sök"},SEARCH_SUCCESS:{da:"Your OCR number is",en:"Your OCR number is",fi:"Your OCR number is",no:"Your OCR number is",sv:"Ditt OCR-nummer är"},ERROR_INVALID_LICENSE_NUMBER:{da:"Invalid license number",en:"Invalid license number",fi:"Invalid license number",no:"Invalid license number",sv:"Ogiltligt licensnummer"},ERROR_NO_RESULTS:{da:"The search did not generate any results",en:"The search did not generate any results",fi:"The search did not generate any results",no:"The search did not generate any results",sv:"Sökningen genererade inga träffar"},ERROR_REASON_HEADER:{da:"It may be due to:",en:"It may be due to:",fi:"It may be due to:",no:"It may be due to:",sv:"Det kan bero på:"},ERROR_REASON_ONE:{da:"We could not find that license number",en:"We could not find that license number",fi:"We could not find that license number",no:"We could not find that license number",sv:"Vi hittar inget sådant licensnummer"},ERROR_REASON_TWO:{da:"Check the information and try again.",en:"Check the information and try again.",fi:"Check the information and try again.",no:"Check the information and try again.",sv:"Kontrollera uppgifterna och försök igen."},PLACEHOLDER_TEXT:{da:"License number 6 digits",en:"License number 6 digits",fi:"License number 6 digits",no:"License number 6 digits",sv:"Licensnummer 6 tecken"}};const c="pn-ocr-search .bold{font-weight:700}pn-ocr-search .search-results,pn-ocr-search .error-text-container{padding:1em 0.5em}pn-ocr-search .invalid-text-container{padding:0.5em 1em}pn-ocr-search .invalid-text-container .invalid-ocr{color:#a70707}";const o=c;const h=class{constructor(n){e(this,n)}get hostElement(){return n(this)}loading=false;error=false;ocrNumber;searchSuccess=false;invalidOcrNumber=false;language=null;async componentWillLoad(){if(this.language===null)return await i(this.hostElement)}getTranslation(e){return a[e][this.language||s]}handleSearch(e){this.invalidOcrNumber=false;this.searchSuccess=false;this.error=false;const n=this.validateOcrNumber(e.detail);if(n.error){this.invalidOcrNumber=true;return}this.ocrNumberSearch(n.value)}async ocrNumberSearch(e){this.loading=true;await fetch(`https://portal.postnord.com/public-service/api/ocr?key=${e}`).then((e=>e.json())).then((e=>{this.ocrNumber=e.newOcr;this.searchSuccess=true})).catch((()=>{this.error=true;this.searchSuccess=false})).finally((()=>{this.loading=false}))}validateOcrNumber(e){const n=e?e.replace(/[ -]/gi,""):"";const t=new RegExp(/\D+/);const r=t.test(n)||n.length!==6;return{error:r,value:n}}renderSearchResult(){if(!this.loading&&!this.error&&this.searchSuccess){return t("div",{class:"search-results"},this.getTranslation("SEARCH_SUCCESS"),": ",t("b",null,this.ocrNumber))}}renderErrorMessage(){if(this.invalidOcrNumber&&!this.loading){return t("div",{class:"invalid-text-container"},t("p",{class:"invalid-ocr"},this.getTranslation("ERROR_INVALID_LICENSE_NUMBER")))}if(this.error&&!this.loading){return t("div",{class:"error-text-container"},t("p",{class:"bold"},this.getTranslation("ERROR_NO_RESULTS")),t("p",{class:"bold"},this.getTranslation("ERROR_REASON_HEADER")),t("p",null,this.getTranslation("ERROR_REASON_ONE")),t("p",null,this.getTranslation("ERROR_REASON_TWO")))}}render(){return t(r,{key:"26af6aaf9b8679a5a7fafbd512359cd9123005ad"},t("pn-search-field",{key:"ecbd20d488504ba0138582cecc070e7ac73f4208",loading:this.loading,label:this.getTranslation("SEARCH_BUTTON"),button:"icon",buttonLabel:this.getTranslation("SEARCH_BUTTON"),placeholder:this.getTranslation("PLACEHOLDER_TEXT"),onSearch:e=>this.handleSearch(e)}),this.renderSearchResult(),this.renderErrorMessage())}};h.style=o;export{h as pn_ocr_search}; //# sourceMappingURL=p-8f466040.entry.js.map