@postnord/web-components
Version:
PostNord Web Components
156 lines (155 loc) • 5.8 kB
JavaScript
/*!
* Built with Stencil
* By PostNord.
*/
import { h, Host } from "@stencil/core";
import { translations } from "./translations";
import { sv, uuidv4, awaitTopbar } from "../../../index";
export class PnZipcodeSearch {
hostElement;
/** This language property will update when pnTopbar loads */
zipcodeId = `pn-zipcode-${uuidv4()}`;
validZipCode;
loading = false;
showResult = false;
zipCodeSearchResult = {
delivery: '',
upcoming: '',
city: '',
postalCode: '',
};
error = false;
errorMessage = '';
/** The `language` property will be prioritized before the pnTopbar language. */
language = null;
/** Event fired when search has result */
searchsuccessful;
async componentWillLoad() {
if (this.language === null)
return await awaitTopbar(this.hostElement);
}
getTranslation(key) {
return translations[key][this.language || sv];
}
handleSearch({ detail }) {
this.validateZipCode(detail);
if (this.error) {
this.errorMessage = 'VALIDATION_ERROR_MESSAGE';
return;
}
this.zipCodeSearch();
}
async zipCodeSearch() {
this.loading = true;
await fetch(`https://portal.postnord.com/api/sendoutarrival/closest?postalCode=${this.validZipCode}`)
.then(response => response.json())
.then(data => {
this.showResult = true;
this.zipCodeSearchResult = data;
this.searchsuccessful.emit(true);
})
.catch(() => {
this.error = true;
this.errorMessage = 'SERVER_ERROR_MESSAGE';
this.searchsuccessful.emit(false);
})
.finally(() => {
this.loading = false;
});
}
validateZipCode(value) {
// Removes spaces and dashes
const formatValue = value ? value.replace(/[ -]/gi, '') : '';
const validRegex = /^\d{5}$/;
const valid = validRegex.test(formatValue);
this.error = !valid;
if (valid) {
this.validZipCode = formatValue;
}
}
renderSearchResult() {
if (!this.loading && !this.error && this.showResult) {
return (h("output", { class: "search-results", htmlFor: this.zipcodeId }, h("p", { class: "text-row" }, h("span", null, this.getTranslation('CLOSEST_DELIVERY_DATE')), h("h3", { class: "delivery-date" }, this.zipCodeSearchResult.delivery)), h("p", { class: "text-row" }, h("span", null, this.getTranslation('NEXT_DELIVERY_DATE')), h("h3", { class: "delivery-date" }, this.zipCodeSearchResult.upcoming)), h("p", { class: "text-row" }, h("span", null, this.getTranslation('ZIP_CODE')), h("h3", null, this.zipCodeSearchResult.postalCode, ", ", this.zipCodeSearchResult.city || 'N/A'))));
}
}
renderErrorMessage() {
if (this.error && !this.loading) {
return (h("p", { class: "pn-error-text", role: "alert" }, h("small", null, this.getTranslation(this.errorMessage))));
}
}
render() {
return (h(Host, { key: '517f6030331e323f2e70aec9c29c1acd4f426b49' }, h("pn-search-field", { key: 'ceffe972b39e168f1ecd86f78eea95329a899d0a', label: this.getTranslation('PLACEHOLDER_TEXT'), searchid: this.zipcodeId, loading: this.loading, button: "none", placeholder: this.getTranslation('PLACEHOLDER_TEXT'), onSearch: e => this.handleSearch(e) }), this.renderSearchResult(), this.renderErrorMessage()));
}
static get is() { return "pn-zipcode-search"; }
static get originalStyleUrls() {
return {
"$": ["pn-zipcode-search.scss"]
};
}
static get styleUrls() {
return {
"$": ["pn-zipcode-search.css"]
};
}
static get properties() {
return {
"language": {
"type": "string",
"mutable": false,
"complexType": {
"original": "PnLanguages",
"resolved": "\"\" | \"da\" | \"en\" | \"fi\" | \"no\" | \"sv\"",
"references": {
"PnLanguages": {
"location": "import",
"path": "@/index",
"id": "src/index.ts::PnLanguages"
}
}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "The `language` property will be prioritized before the pnTopbar language."
},
"getter": false,
"setter": false,
"attribute": "language",
"reflect": false,
"defaultValue": "null"
}
};
}
static get states() {
return {
"zipcodeId": {},
"validZipCode": {},
"loading": {},
"showResult": {},
"zipCodeSearchResult": {},
"error": {},
"errorMessage": {}
};
}
static get events() {
return [{
"method": "searchsuccessful",
"name": "searchsuccessful",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "Event fired when search has result"
},
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
}
}];
}
static get elementRef() { return "hostElement"; }
}
//# sourceMappingURL=pn-zipcode-search.js.map