From bb5b0701ed8d1c510cb9e7856ecc52d5bcef99b4 Mon Sep 17 00:00:00 2001 From: Jose Farias Date: Sat, 12 Oct 2024 23:06:34 -0600 Subject: [PATCH] Simplify Current JS implementation --- .../controllers/thread_controller.js | 3 +-- app/javascript/helpers/current_helpers.js | 25 ------------------- app/javascript/helpers/index.js | 1 - app/javascript/initializers/current.js | 15 +++++++++++ app/javascript/initializers/index.js | 1 + 5 files changed, 17 insertions(+), 28 deletions(-) delete mode 100644 app/javascript/helpers/current_helpers.js create mode 100644 app/javascript/initializers/current.js diff --git a/app/javascript/controllers/thread_controller.js b/app/javascript/controllers/thread_controller.js index 1004505b3..0671c8361 100644 --- a/app/javascript/controllers/thread_controller.js +++ b/app/javascript/controllers/thread_controller.js @@ -1,5 +1,4 @@ import { Controller } from "@hotwired/stimulus" -import { current } from "helpers" export default class extends Controller { static classes = [ "myComment" ] @@ -9,6 +8,6 @@ export default class extends Controller { } get #myComments() { - return this.element.querySelectorAll(`.comment[data-creator-id='${current.user.id}']`) + return this.element.querySelectorAll(`.comment[data-creator-id='${Current.user.id}']`) } } diff --git a/app/javascript/helpers/current_helpers.js b/app/javascript/helpers/current_helpers.js deleted file mode 100644 index 6d2d9fdb2..000000000 --- a/app/javascript/helpers/current_helpers.js +++ /dev/null @@ -1,25 +0,0 @@ -// On-demand JavaScript objects from "current" HTML elements. Example: -// -// -// -// -// >> current.identity -// => { id: "123", timeZoneName: "Central Time (US & Canada)" } -// -// >> current.foo -// => {} - export const current = new Proxy({}, { - get(_target, propertyName) { - const result = {} - const prefix = `current-${propertyName}-` - for (const { name, content } of document.head.querySelectorAll(`meta[name^=${prefix}]`)) { - const key = camelize(name.slice(prefix.length)) - result[key] = content - } - return result - } - }) - - function camelize(string) { - return string.replace(/(?:[_-])([a-z0-9])/g, (_, char) => char.toUpperCase()) - } diff --git a/app/javascript/helpers/index.js b/app/javascript/helpers/index.js index 634fd65a2..c33ab8c40 100644 --- a/app/javascript/helpers/index.js +++ b/app/javascript/helpers/index.js @@ -1,2 +1 @@ -export * from "helpers/current_helpers" export * from "helpers/timing_helpers" diff --git a/app/javascript/initializers/current.js b/app/javascript/initializers/current.js new file mode 100644 index 000000000..96ace04b5 --- /dev/null +++ b/app/javascript/initializers/current.js @@ -0,0 +1,15 @@ +class Current { + get user() { + const currentUserId = this.#extractContentFromMetaTag("current-user-id") + + if (currentUserId) { + return { id: parseInt(currentUserId) } + } + } + + #extractContentFromMetaTag(name) { + return document.head.querySelector(`meta[name="${name}"]`)?.getAttribute("content") + } +} + +window.Current = new Current() diff --git a/app/javascript/initializers/index.js b/app/javascript/initializers/index.js index 9a501b4b4..c86bbda03 100644 --- a/app/javascript/initializers/index.js +++ b/app/javascript/initializers/index.js @@ -1 +1,2 @@ +import "initializers/current" import "initializers/local_time"