Skip to content

Commit

Permalink
fix dhl
Browse files Browse the repository at this point in the history
  • Loading branch information
ljmerza committed Jan 6, 2021
1 parent b299cb5 commit d86c193
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 96 deletions.
30 changes: 11 additions & 19 deletions dist/tracking-number-card.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.6",
"version": "1.2.7",
"description": "A tracking number card for Home Assistant Lovelace UI",
"keywords": [
"home-assistant",
Expand Down
148 changes: 73 additions & 75 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
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=",
};

class TrackingNumberCard extends LitElement {
static get properties() {
Expand All @@ -30,8 +29,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 +41,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 +64,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 +81,41 @@ 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, ' ');
if (
tracker.origin.toLowerCase() !== tracker.trackingOrigin.toLowerCase()
) {
tracker.trackingOrigin = tracker.trackingOrigin.replace(/_/g, " ");
linkText = `${tracker.origin} (${tracker.trackingOrigin})`;
}

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 +124,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 +136,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 +148,66 @@ 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;

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;

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

} else if (isNumber && length === 22) {
origin = "FedEx";
} else if (isNumber && (length === 22 || length === 26)) {
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: trackerNumber.trackingOrigin,
};
});
}

}

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

0 comments on commit d86c193

Please sign in to comment.