@postnord/web-components
Version:
PostNord Web Components
176 lines (175 loc) • 6.59 kB
JavaScript
/*!
* Built with Stencil
* By PostNord.
*/
import { h, Host } from "@stencil/core";
import { translations } from "./translations";
import { sv, uuidv4, awaitTopbar } from "../../../index";
/** Check the delivery date for letters, newspapers and direct mail that are distributed every second day. */
export class PnZipcodeSearch {
constructor() {
this.validZipCode = undefined;
this.loading = false;
this.showResult = false;
this.zipCodeSearchResult = {
delivery: '',
upcoming: '',
city: '',
postalCode: '',
};
this.error = false;
this.errorMessage = undefined;
this.language = null;
this.value = undefined;
}
/** This language property will update when pnTopbar loads */
searchId = `pn-zipcode-${uuidv4()}`;
hostElement;
/** Event fired when search has result */
searchsuccessful;
async componentWillLoad() {
if (this.language === null)
await awaitTopbar(this.hostElement);
if (!!this.value)
this.handleSearch(this.value);
}
translation(key) {
return translations[key][this.language || sv];
}
handleSearch(value) {
this.value = value;
this.validateZipCode();
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() {
// Removes spaces and dashes
const formatValue = this.value ? this.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: "pn-search-results", htmlFor: this.searchId }, h("p", { class: "text-row" }, h("span", null, this.translation('CLOSEST_DELIVERY_DATE')), h("h3", { class: "delivery-date" }, this.zipCodeSearchResult.delivery)), h("p", { class: "text-row" }, h("span", null, this.translation('NEXT_DELIVERY_DATE')), h("h3", { class: "delivery-date" }, this.zipCodeSearchResult.upcoming)), h("p", { class: "text-row" }, h("span", null, this.translation('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.translation(this.errorMessage))));
}
}
render() {
return (h(Host, { key: '6ced0525306ff0ec238e08786e482e8101bff4eb' }, h("pn-search-field", { key: '3dd3987230908a344c0535497a0f957c02873c7a', label: this.translation('PLACEHOLDER_TEXT'), value: this.value, searchid: this.searchId, loading: this.loading, button: "icon", buttonLabel: this.translation('SEARCH'), placeholder: this.translation('PLACEHOLDER_TEXT'), onSearch: e => this.handleSearch(e.detail) }), 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": "PnZipCodeSearchLanguages",
"resolved": "\"\" | \"en\" | \"sv\"",
"references": {
"PnZipCodeSearchLanguages": {
"location": "import",
"path": "@/index",
"id": "src/index.ts::PnZipCodeSearchLanguages"
}
}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "The `language` property will be prioritized before the pnTopbar language."
},
"attribute": "language",
"reflect": false,
"defaultValue": "null"
},
"value": {
"type": "string",
"mutable": true,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Set a pre initialized value."
},
"attribute": "value",
"reflect": false
}
};
}
static get states() {
return {
"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