From 4cb7e5fb2dc6c35d97830735ea39b0244ea1fc4f Mon Sep 17 00:00:00 2001 From: briri Date: Tue, 21 May 2024 13:30:54 -0700 Subject: [PATCH] reverted the TimeagoFactory.js.erb to a pure .js file without Rails markup --- app/javascript/src/answers/edit.js | 2 +- app/javascript/src/notes/index.js | 2 +- app/javascript/src/utils/timeagoFactory.js | 25 ++++++++++ .../src/utils/timeagoFactory.js.erb | 46 ------------------- 4 files changed, 27 insertions(+), 48 deletions(-) create mode 100644 app/javascript/src/utils/timeagoFactory.js delete mode 100644 app/javascript/src/utils/timeagoFactory.js.erb diff --git a/app/javascript/src/answers/edit.js b/app/javascript/src/answers/edit.js index 998aef1bc9..e996804ca0 100644 --- a/app/javascript/src/answers/edit.js +++ b/app/javascript/src/answers/edit.js @@ -6,7 +6,7 @@ import { import debounce from '../utils/debounce'; import { updateSectionProgress, getQuestionDiv } from '../utils/sectionUpdate'; import datePicker from '../utils/datePicker'; -import TimeagoFactory from '../utils/timeagoFactory.js.erb'; +import TimeagoFactory from '../utils/timeagoFactory'; $(() => { if ($('form.form-answer').length > 0) { diff --git a/app/javascript/src/notes/index.js b/app/javascript/src/notes/index.js index 394aca9bf9..00666b8ac0 100644 --- a/app/javascript/src/notes/index.js +++ b/app/javascript/src/notes/index.js @@ -1,5 +1,5 @@ import { isObject, isString } from '../utils/isType'; -import TimeagoFactory from '../utils/timeagoFactory.js.erb'; +import TimeagoFactory from '../utils/timeagoFactory'; $(() => { const defaultViewSelector = (questionId) => `#note_new${questionId}`; diff --git a/app/javascript/src/utils/timeagoFactory.js b/app/javascript/src/utils/timeagoFactory.js new file mode 100644 index 0000000000..7432101224 --- /dev/null +++ b/app/javascript/src/utils/timeagoFactory.js @@ -0,0 +1,25 @@ +import getConstant from './constants'; +import { format, register } from 'timeago.js'; + +const TimeagoFactory = (() => { + const defaultLocale = 'en' + + const convertTime = (currentLocale, el) => { + return format($(el).attr('datetime'), `${currentLocale}`); + }; + + return { + render: (el) => { + if (el.length > 1) { + // If we were passed an array of JQuery elements then handle each one + $(el).each((idx, e) => { + $(e).text(convertTime(defaultLocale, e)); + }); + } else { + $(el).text(convertTime(defaultLocale, el)); + } + }, + }; +})(); + +export default TimeagoFactory; diff --git a/app/javascript/src/utils/timeagoFactory.js.erb b/app/javascript/src/utils/timeagoFactory.js.erb deleted file mode 100644 index ae2396670b..0000000000 --- a/app/javascript/src/utils/timeagoFactory.js.erb +++ /dev/null @@ -1,46 +0,0 @@ -import getConstant from './constants'; -import { format, register } from 'timeago.js'; - -// Dynamically register all of the available locales if they have a timeago file -<% available = [] %> -<% LocaleService.available_locales.each do |locale| %> - <% name = LocaleService.to_gettext(locale: locale) %> - <% if File.exist?("app/javascript/src/locale/#{name}/timeago.js") %> - <% available << name %> - import <%= name %> from '../locale/<%= name %>/timeago'; - <% else %> - // Issue warnings about the missing Timeago file for the locale - <% Rails.logger.warn "Warning, no Timeago JS for #{name}" %> - console.log('No Timeago registered for <%= name %>'); - <% end %> -<% end %> - -const TimeagoFactory = (() => { - // Register all of the locales with Timeago - <% available.each do |locale| %> - register('<%= locale %>', <%= locale %>); - <% end %> - - const defaultLocale = '<%= LocaleService.to_gettext(locale: LocaleService.default_locale) %>' - - const convertTime = (currentLocale, el) => { - return format($(el).attr('datetime'), `${currentLocale}`); - }; - - return { - render: (el) => { - const currentLocale = getConstant('CURRENT_LOCALE') || defaultLocale; - - if (el.length > 1) { - // If we were passed an array of JQuery elements then handle each one - $(el).each((idx, e) => { - $(e).text(convertTime(currentLocale, e)); - }); - } else { - $(el).text(convertTime(currentLocale, el)); - } - }, - }; -})(); - -export default TimeagoFactory;