Skip to content

Commit

Permalink
add swiss post
Browse files Browse the repository at this point in the history
  • Loading branch information
ljmerza committed Jan 11, 2021
1 parent f003ef4 commit 9317872
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 107 deletions.
48 changes: 20 additions & 28 deletions dist/tracking-number-card.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/tracking-number-card.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tracking-number-card",
"version": "1.2.8",
"version": "1.3.0",
"description": "A tracking number card for Home Assistant Lovelace UI",
"keywords": [
"home-assistant",
Expand Down
157 changes: 80 additions & 77 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import '@babel/polyfill/noConflict';
import "@babel/polyfill/noConflict";

import { LitElement, html } from 'lit-element';
import { LitElement, html } from "lit-element";

import style from './style';
import defaultConfig from './defaults';


import TrackingNumberCardEditor from './index-editor';
customElements.define('tracking-number-card-editor', TrackingNumberCardEditor);
import style from "./style";
import defaultConfig from "./defaults";

import TrackingNumberCardEditor from "./index-editor";
customElements.define("tracking-number-card-editor", TrackingNumberCardEditor);

const _TRACKING_NUMBER_CARD_URLS = {
ups: 'https://www.ups.com/track?loc=en_US&tracknum=',
usps: 'https://tools.usps.com/go/TrackConfirmAction?tLabels=',
fedex: 'https://www.fedex.com/apps/fedextrack/?tracknumbers=',
dhl: 'https://www.logistics.dhl/us-en/home/tracking/tracking-parcel.html?submit=1&tracking-id=',
}
ups: "https://www.ups.com/track?loc=en_US&tracknum=",
usps: "https://tools.usps.com/go/TrackConfirmAction?tLabels=",
fedex: "https://www.fedex.com/apps/fedextrack/?tracknumbers=",
dhl:
"https://www.logistics.dhl/us-en/home/tracking/tracking-parcel.html?submit=1&tracking-id=",
swiss_post: `https://www.swisspost.ch/track?formattedParcelCodes=`,
};

class TrackingNumberCard extends LitElement {
static get properties() {
Expand All @@ -30,8 +30,9 @@ class TrackingNumberCard extends LitElement {
}

setConfig(config) {
if (!config.entities) throw new Error('Entities is required');
if (config.entities && !Array.isArray(config.entities)) throw new Error('entities must be a list');
if (!config.entities) throw new Error("Entities is required");
if (config.entities && !Array.isArray(config.entities))
throw new Error("entities must be a list");

this.config = { ...defaultConfig, ...config };
}
Expand All @@ -41,8 +42,8 @@ class TrackingNumberCard extends LitElement {
* @return {Number}
*/
getCardSize() {
if (this.config){
const headerSize = (this.config.showHeader && this.config.header) ? 1 : 0;
if (this.config) {
const headerSize = this.config.showHeader && this.config.header ? 1 : 0;
const trackingNumbers = this.getTrackingNumbers();
const bodySize = (trackingNumbers && trackingNumbers.length) || 0;
return headerSize + bodySize;
Expand All @@ -64,15 +65,14 @@ class TrackingNumberCard extends LitElement {
const body = this.createBody();

// if we want to hide and trackers is empty then hide card completely
if(this.config.hideWhenEmpty && this.trackingNumbers && this.trackingNumbers.length === 0)
if (
this.config.hideWhenEmpty &&
this.trackingNumbers &&
this.trackingNumbers.length === 0
)
return html``;

return html`
<ha-card>
${header}
${body}
</ha-card>
`;
return html` <ha-card> ${header} ${body} </ha-card> `;
}

/**
Expand All @@ -82,41 +82,37 @@ class TrackingNumberCard extends LitElement {
createHeader() {
if (this.config.showHeader === false) return html``;

return html`
<div class='track-header'>
${this.config.header}
</div>
`;
return html` <div class="track-header">${this.config.header}</div> `;
}

createBody() {
this.trackingNumbers = this.getTrackingNumbers();
const trackingObjects = this.generateTrackingNumberLinks(this.trackingNumbers);

const table = trackingObjects.map(tracker => {
const trackingObjects = this.generateTrackingNumberLinks(
this.trackingNumbers
);

const table = trackingObjects.map((tracker) => {
// only show origin email if not from tracking company
let linkText = `${tracker.origin}`;
if (tracker.origin.toLowerCase() !== tracker.trackingOrigin.toLowerCase()){
tracker.trackingOrigin = tracker.trackingOrigin.replace(/_/g, ' ');
linkText = `${tracker.origin} (${tracker.trackingOrigin})`;
}
const linkText = tracker.trackingOrigin
? `${tracker.origin} (${tracker.trackingOrigin})`
: `${tracker.origin}`;

return html`
<div class='track-row'>
<div class='track-row__number'>${tracker.number}</div>
<div class='track-row__link'>
<a href='${tracker.link}' target='_blank' rel="nofollow noreferrer noopener">${linkText}</a>
<div class="track-row">
<div class="track-row__number">${tracker.number}</div>
<div class="track-row__link">
<a
href="${tracker.link}"
target="_blank"
rel="nofollow noreferrer noopener"
>${linkText}</a
>
</div>
</div>
`;
});

return html`
<div class='track-body'>
${table}
</div>
`;
return html` <div class="track-body">${table}</div> `;
}

/**
Expand All @@ -125,8 +121,8 @@ class TrackingNumberCard extends LitElement {
*/
getTrackingNumbers() {
const entities = this.config.entities
.map(entity => this.hass.states[entity])
.filter(entity => {
.map((entity) => this.hass.states[entity])
.filter((entity) => {
if (!entity) return false;
if (!entity.attributes) return false;
if (!entity.attributes.tracking_numbers) return false;
Expand All @@ -137,9 +133,9 @@ class TrackingNumberCard extends LitElement {
const trackingNumbers = entities.reduce((acc, entity) => {
const trackingOrigins = Object.keys(entity.attributes.tracking_numbers);

trackingOrigins.forEach(trackingOrigin => {
entity.attributes.tracking_numbers[trackingOrigin].forEach(number => {
acc.push({number, trackingOrigin});
trackingOrigins.forEach((trackingOrigin) => {
entity.attributes.tracking_numbers[trackingOrigin].forEach((number) => {
acc.push({ number, trackingOrigin });
});
});

Expand All @@ -149,67 +145,74 @@ class TrackingNumberCard extends LitElement {
return [...new Set(trackingNumbers)];
}


generateTrackingNumberLinks(trackingNumbers) {
return trackingNumbers.map(trackerNumber => {
let link = '';
let origin = '';
return trackingNumbers.map((trackerNumber) => {
let link = "";
let origin = "";
const number = trackerNumber.number;

const isNumber = !isNaN(number);
const length = isNumber && number.toString().length;
let trackingOrigin = "";

if (/^1Z/.test(number)) {
link = `${_TRACKING_NUMBER_CARD_URLS.ups}${number}`;
origin = 'UPS';

origin = "UPS";
} else if (/CN$/.test(number)) {
link = `${_TRACKING_NUMBER_CARD_URLS.usps}${number}`;
origin = 'USPS';

origin = "USPS";
} else {
switch (trackerNumber.trackingOrigin) {
case 'ups':
case "ups":
link = `${_TRACKING_NUMBER_CARD_URLS.ups}${number}`;
origin = 'UPS';
origin = "UPS";
break;
case 'fedex':

case "fedex":
link = `${_TRACKING_NUMBER_CARD_URLS.fedex}${number}`;
origin = 'FedEx';
origin = "FedEx";
break;

case 'usps':
case "usps":
link = `${_TRACKING_NUMBER_CARD_URLS.usps}${number}`;
origin = 'USPS';
origin = "USPS";
break;

case 'dhl':
case "dhl":
link = `${_TRACKING_NUMBER_CARD_URLS.dhl}${number}`;
origin = 'DHL';
origin = "DHL";
break;

case "swiss_post":
link = `${_TRACKING_NUMBER_CARD_URLS.swiss_post}${number}`;
origin = "Swiss Post";
break;

default:
trackingOrigin = trackerNumber.trackingOrigin.replace(/_/g, " ");

if (isNumber && (length === 12 || length === 15 || length === 20)) {
link = `${_TRACKING_NUMBER_CARD_URLS.fedex}${number}`;
origin = 'FedEx';

origin = "FedEx";
} else if (isNumber && length === 22) {
link = `${_TRACKING_NUMBER_CARD_URLS.usps}${number}`;
origin = 'USPS';

origin = "USPS";
} else if (length > 25) {
link = `${_TRACKING_NUMBER_CARD_URLS.dhl}${number}`;
origin = 'DHL';
origin = "DHL";
}
break;
}
}

return { number, link, origin, trackingOrigin: trackerNumber.trackingOrigin};

return {
number,
link,
origin,
trackingOrigin,
};
});
}

}

customElements.define('tracking-number-card', TrackingNumberCard);
customElements.define("tracking-number-card", TrackingNumberCard);

0 comments on commit 9317872

Please sign in to comment.