From f188ae29b72e508fdd4c1cd94109ca96e55303f9 Mon Sep 17 00:00:00 2001 From: Cam Saul <1455846+camsaul@users.noreply.github.com> Date: Mon, 9 Nov 2020 17:00:16 -0800 Subject: [PATCH] Fix formatting temporal values with custom day of week (#13740) * Fix formatting temporal values as day of week with custom day of week (#13604) * Pretty * PR feedback * Need to update when user locale changes as well * Update Moment start of week in i18n.js instead * Make it nice * Remove some unneeded stuff --- frontend/src/metabase/lib/i18n.js | 42 +++++++++++++++++++ frontend/src/metabase/lib/time.js | 2 +- .../admin/settings/localization.cy.spec.js | 2 +- jsconfig.json | 1 + 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 jsconfig.json diff --git a/frontend/src/metabase/lib/i18n.js b/frontend/src/metabase/lib/i18n.js index d90a115665b..79df86d869e 100644 --- a/frontend/src/metabase/lib/i18n.js +++ b/frontend/src/metabase/lib/i18n.js @@ -1,6 +1,8 @@ import { addLocale, useLocale } from "ttag"; import moment from "moment-timezone"; +import MetabaseSettings from "metabase/lib/settings"; + // note this won't refresh strings that are evaluated at load time export async function loadLocalization(locale) { // we need to be sure to set the initial localization before loading any files @@ -23,6 +25,45 @@ export async function loadLocalization(locale) { setLocalization(translationsObject); } +// Tell Moment.js to use the value of the start-of-week Setting for its current locale +function updateMomentStartOfWeek() { + const startOfWeekDayName = MetabaseSettings.get("start-of-week"); + if (!startOfWeekDayName) { + return; + } + + const START_OF_WEEK_DAYS = [ + "sunday", + "monday", + "tuesday", + "wednesday", + "thursday", + "friday", + "saturday", + ]; + + const startOfWeekDayNumber = START_OF_WEEK_DAYS.indexOf(startOfWeekDayName); + if (startOfWeekDayNumber === -1) { + return; + } + console.log( + "Setting moment.js start of week for Locale", + moment.locale(), + "to", + startOfWeekDayName, + ); + + moment.updateLocale(moment.locale(), { + week: { + // Moment.js dow range Sunday (0) - Saturday (6) + dow: startOfWeekDayNumber, + }, + }); +} + +// if the start of week Setting is updated, update the moment start of week +MetabaseSettings.on("start-of-week", updateMomentStartOfWeek); + export function setLocalization(translationsObject) { const locale = translationsObject.headers.language; @@ -33,6 +74,7 @@ export function setLocalization(translationsObject) { useLocale(locale); moment.locale(locale); + updateMomentStartOfWeek(locale); } // Format a fixed timestamp in local time to see if the current locale defaults diff --git a/frontend/src/metabase/lib/time.js b/frontend/src/metabase/lib/time.js index cfb56c7675f..f68559fb94f 100644 --- a/frontend/src/metabase/lib/time.js +++ b/frontend/src/metabase/lib/time.js @@ -16,7 +16,7 @@ const NUMERIC_UNIT_FORMATS = { .startOf("hour"), "day-of-week": value => moment() - .day(value - 1) + .weekday(value - 1) .startOf("day"), "day-of-month": value => moment("2016-01-01") // initial date must be in month with 31 days to format properly diff --git a/frontend/test/metabase/scenarios/admin/settings/localization.cy.spec.js b/frontend/test/metabase/scenarios/admin/settings/localization.cy.spec.js index 5de16a81eb4..ad180115312 100644 --- a/frontend/test/metabase/scenarios/admin/settings/localization.cy.spec.js +++ b/frontend/test/metabase/scenarios/admin/settings/localization.cy.spec.js @@ -41,7 +41,7 @@ describe("scenarios > admin > permissions", () => { cy.get(".axis.x").contains("April 25, 2016"); }); - it.skip("should display days on X-axis correctly when grouped by 'Day of the Week' (metabase#13604)", () => { + it("should display days on X-axis correctly when grouped by 'Day of the Week' (metabase#13604)", () => { withSampleDataset(({ ORDERS }) => { cy.request("POST", "/api/card", { name: "13604", diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 00000000000..fb52ce6717a --- /dev/null +++ b/jsconfig.json @@ -0,0 +1 @@ +{"include": ["frontend/src/**", "enterprise/frontent/src/**"]} -- GitLab