diff --git a/postnl-card.js b/postnl-card.js index 28f24c6..23479bd 100755 --- a/postnl-card.js +++ b/postnl-card.js @@ -1,4 +1,4 @@ - const LitElement = Object.getPrototypeOf( +const LitElement = Object.getPrototypeOf( customElements.get("ha-panel-lovelace") ); const html = LitElement.prototype.html; @@ -36,7 +36,7 @@ function renderStyles() { font-size: var(--paper-font-body1_-_font-size); font-weight: var(--paper-font-body1_-_font-weight); line-height: var(--paper-font-body1_-_line-height); - padding-bottom: 16px); + padding-bottom: 16px; } ha-card.no-header { padding: 16px 0; @@ -126,15 +126,15 @@ function renderStyles() { margin: 0; align-self: left; } + + footer { + padding: 16px; + color: red; + } ` } -function isToday(dateParameter) { - var today = new Date(); - return dateParameter.getDate() === today.getDate() && dateParameter.getMonth() === today.getMonth() && dateParameter.getFullYear() === today.getFullYear(); -} - class PostNL extends LitElement { static get properties() { return { @@ -157,9 +157,14 @@ class PostNL extends LitElement { super() this._hass = null - this.delivery = null - this.distribution = null - this.letters = null + this.deliveryObject = null + this.distributionObject = null + this.letterObject = null + this.delivery_enroute = [] + this.delivery_delivered = [] + this.distribution_enroute = [] + this.distribution_delivered = [] + this.letters = [] this.icon = null this.name = null this.date_format = null @@ -173,15 +178,15 @@ class PostNL extends LitElement { this._hass = hass if (this.config.delivery) { - this.delivery = hass.states[this.config.delivery] + this.deliveryObject = hass.states[this.config.delivery] } if (this.config.distribution) { - this.distribution = hass.states[this.config.distribution] + this.distributionObject = hass.states[this.config.distribution] } if (this.config.letters) { - this.letters = hass.states[this.config.letters] + this.letterObject = hass.states[this.config.letters] } if (this.config.hide) { @@ -219,6 +224,53 @@ class PostNL extends LitElement { } this._language = hass.language + + this.delivery_enroute = [] + this.delivery_delivered = [] + this.distribution_enroute = [] + this.distribution_delivered = [] + + // Format letters + Object.entries(this.letterObject.attributes.letters).sort((a, b) => new Date(b[1].delivery_date) - new Date(a[1].delivery_date)).map(([key, letter]) => { + if (window.moment) { + if (moment(letter.delivery_date).isBefore(moment().subtract(this.past_days, 'days').startOf('day'))) { + return; + } + } + + this.letters.push(letter); + }); + + // Format deliveries + Object.entries(this.deliveryObject.attributes.enroute).sort((a, b) => new Date(b[1].planned_date) - new Date(a[1].planned_date)).map(([key, shipment]) => { + this.delivery_enroute.push(shipment); + }); + + Object.entries(this.deliveryObject.attributes.delivered).sort((a, b) => new Date(b[1].delivery_date) - new Date(a[1].delivery_date)).map(([key, shipment]) => { + if (window.moment) { + if (shipment.delivery_date != null && moment(shipment.delivery_date).isBefore(moment().subtract(this.past_days, 'days').startOf('day'))) { + return; + } + } + + + this.delivery_delivered.push(shipment); + }); + + // Format distribution + Object.entries(this.distributionObject.attributes.enroute).sort((a, b) => new Date(b[1].planned_date) - new Date(a[1].planned_date)).map(([key, shipment]) => { + this.distribution_enroute.push(shipment); + }); + + Object.entries(this.distributionObject.attributes.delivered).sort((a, b) => new Date(b[1].delivery_date) - new Date(a[1].delivery_date)).map(([key, shipment]) => { + if (window.moment) { + if (shipment.delivery_date != null && moment(shipment.delivery_date).isBefore(moment().subtract(this.past_days, 'days').startOf('day'))) { + return; + } + } + + this.distribution_delivered.push(shipment); + }); } render({ _hass, _hide, _values, config, delivery, distribution, letters } = this) { @@ -244,6 +296,7 @@ class PostNL extends LitElement { ${this.renderLetters()} ${this.renderDelivery()} ${this.renderDistribution()} + ${this.renderWarning()} ` } @@ -259,19 +312,29 @@ class PostNL extends LitElement { ` } + renderWarning() { + if (window.moment) return '' + + return html` + + ` + } + renderLettersInfo() { if (!this.letters) return '' return html`