From 7b89a0af5fcaf04c506752724b2bc357022b2dfe Mon Sep 17 00:00:00 2001 From: Michael Geers Date: Thu, 6 Jul 2023 23:39:21 +0200 Subject: [PATCH] Sessions: ui refinement, mobile table, breadcrumb, filters (#8793) --- assets/js/components/CustomSelect.vue | 48 +++ assets/js/components/Savings.vue | 5 +- assets/js/mixins/formatter.js | 19 +- assets/js/mixins/formatter.test.js | 20 ++ assets/js/views/App.vue | 2 +- assets/js/views/ChargingSessions.vue | 431 +++++++++++++++++--------- i18n/de.toml | 2 +- i18n/en.toml | 5 +- tests/sessions.spec.js | 248 ++++++++++----- 9 files changed, 548 insertions(+), 232 deletions(-) create mode 100644 assets/js/components/CustomSelect.vue diff --git a/assets/js/components/CustomSelect.vue b/assets/js/components/CustomSelect.vue new file mode 100644 index 000000000..b214dbeb3 --- /dev/null +++ b/assets/js/components/CustomSelect.vue @@ -0,0 +1,48 @@ + + + + diff --git a/assets/js/components/Savings.vue b/assets/js/components/Savings.vue index af0f7f0ba..1bbe27743 100644 --- a/assets/js/components/Savings.vue +++ b/assets/js/components/Savings.vue @@ -175,9 +175,8 @@ export default { return Math.round(this.selfConsumptionPercent) || 0; }, effectivePriceFormatted() { - const [value, unit] = this.fmtPricePerKWh(this.effectivePrice, this.currency).split( - " " - ); + const value = this.fmtPricePerKWh(this.effectivePrice, this.currency, false, false); + const unit = this.pricePerKWhUnit(this.currency); return { value, unit }; }, startDate() { diff --git a/assets/js/mixins/formatter.js b/assets/js/mixins/formatter.js index 0d6133d16..f7f55fcbd 100644 --- a/assets/js/mixins/formatter.js +++ b/assets/js/mixins/formatter.js @@ -197,22 +197,31 @@ export default { const symbols = { EUR: "€", USD: "$" }; return symbols[currency] || currency; }, - fmtPricePerKWh: function (amout = 0, currency = "EUR", short = false) { - let unit = currency; + fmtPricePerKWh: function (amout = 0, currency = "EUR", short = false, withUnit = true) { let value = amout; let minimumFractionDigits = 1; let maximumFractionDigits = 3; if (["EUR", "USD"].includes(currency)) { value *= 100; - unit = "ct"; minimumFractionDigits = 1; maximumFractionDigits = 1; } - return `${new Intl.NumberFormat(this.$i18n.locale, { + const price = new Intl.NumberFormat(this.$i18n.locale, { style: "decimal", minimumFractionDigits, maximumFractionDigits, - }).format(value)} ${unit}${short ? "" : "/kWh"}`; + }).format(value); + if (withUnit) { + return `${price} ${this.pricePerKWhUnit(currency, short)}`; + } + return price; + }, + pricePerKWhUnit: function (currency = "EUR", short = false) { + let unit = currency; + if (["EUR", "USD"].includes(currency)) { + unit = "ct"; + } + return `${unit}${short ? "" : "/kWh"}`; }, fmtTimeAgo: function (elapsed) { const units = { diff --git a/assets/js/mixins/formatter.test.js b/assets/js/mixins/formatter.test.js index 653b34960..b4b804eca 100644 --- a/assets/js/mixins/formatter.test.js +++ b/assets/js/mixins/formatter.test.js @@ -26,3 +26,23 @@ describe("fmtKWh", () => { expect(fmt.fmtKWh(56789.123, false, true, 2)).eq("56.789,12 Wh"); }); }); + +describe("fmtPricePerKWh", () => { + test("should format with units", () => { + expect(fmt.fmtPricePerKWh(0.2, "EUR")).eq("20,0 ct/kWh"); + expect(fmt.fmtPricePerKWh(0.2, "EUR", true)).eq("20,0 ct"); + expect(fmt.fmtPricePerKWh(0.234, "USD")).eq("23,4 ct/kWh"); + expect(fmt.fmtPricePerKWh(1234, "SEK")).eq("1.234,0 SEK/kWh"); + expect(fmt.fmtPricePerKWh(0.2, "EUR", false, false)).eq("20,0"); + }); +}); + +describe("pricePerKWhUnit", () => { + test("should return correct unit", () => { + expect(fmt.pricePerKWhUnit("EUR")).eq("ct/kWh"); + expect(fmt.pricePerKWhUnit("EUR", true)).eq("ct"); + expect(fmt.pricePerKWhUnit("USD")).eq("ct/kWh"); + expect(fmt.pricePerKWhUnit("SEK")).eq("SEK/kWh"); + expect(fmt.pricePerKWhUnit("SEK", true)).eq("SEK"); + }); +}); diff --git a/assets/js/views/App.vue b/assets/js/views/App.vue index 52983dfef..c6290c22b 100644 --- a/assets/js/views/App.vue +++ b/assets/js/views/App.vue @@ -1,5 +1,5 @@