Skip to content

Commit

Permalink
fix: human-readable date according to the current locale
Browse files Browse the repository at this point in the history
  • Loading branch information
bgatellier committed Jan 23, 2024
1 parent 5246e32 commit 2c60335
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
20 changes: 14 additions & 6 deletions assets/js/components/SiteAnalysisResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@ class SiteAnalysisResult {
/**
* Create a site analysis result page with updated dom from api data
* @param {Element} el
* @param {string} currentLocale
*/
constructor(el) {
constructor(el, currentLocale) {
this.el = el;

/** @type {ResultRelativeTextData} */
this.resultRelativeTextData = resultRelativeTextData;

this._init();
this._init(currentLocale);
}

/**
* Init
*
* Checks if an analysis results object exists. If yes, use it.
* Otherwise, fetches data from server.
*
*
* @param {string} currentLocale
*/
async _init() {
async _init(currentLocale) {
const urlParams = new URLSearchParams(window.location.search);
const langSwitchersElements = document.querySelectorAll(".language-switcher a.lang")
const screenshotImgElement = document.querySelector(".result-screenshot")
Expand Down Expand Up @@ -86,10 +88,16 @@ class SiteAnalysisResult {
// update page size from ko to mo
pageResultData.size = Math.round(pageResultData.size) / 1000;

// set page result title
// set page result title and message
pageResultData.grade_title = this.resultRelativeTextData.verdictTitles[pageResultData.grade];

pageResultData.grade_message = this.resultRelativeTextData.verdictMessages[pageResultData.grade];

// convert the date from an UTC string in to a human-readable string according to the current locale
const date = new Date(pageResultData.date)
pageResultData.date = Intl.DateTimeFormat(currentLocale, {
dateStyle: "full",
timeStyle: "medium",
}).format(date)

// set page result params binary scores (0/1 : good/bad)
pageResultData = {
Expand Down
9 changes: 6 additions & 3 deletions assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import SiteAnalysisResult from "./components/SiteAnalysisResult";
// ------------------------------------------------------------------------- INIT APP

function initApp() {
// pick the current locale from the DOM once the application starts: aavoid several picking in the different components
const currentLocale = document.getElementsByTagName("html")[0].getAttribute("lang")
initMenu();
initCollapses();
initPageResult();
initPageResult(currentLocale);
initSubmitUrlForm();
initButtonRemakeAnalysis();
initButtonShareURL();
Expand Down Expand Up @@ -47,11 +49,12 @@ function initCollapses() {

/**
* Init page result interactive data from api or url params
* @param {string} currentLocale
*/
function initPageResult() {
function initPageResult(currentLocale) {
const resultPageContentEl = document.querySelector(".js-result-container");
if (!resultPageContentEl) return;
new SiteAnalysisResult(resultPageContentEl);
new SiteAnalysisResult(resultPageContentEl, currentLocale);
}

/**
Expand Down

0 comments on commit 2c60335

Please sign in to comment.