Skip to content

Commit

Permalink
fix slug not appearing on first load
Browse files Browse the repository at this point in the history
  • Loading branch information
progapandist committed Jun 9, 2024
1 parent c1e4a78 commit a1c4a71
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions app/javascript/controllers/random_image_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,21 @@ export default class extends Controller {
"Hello from RandomImageController, imgproxyUrl:",
this.imgproxyUrlValue
);

this.imageDisplayTarget.src = this.imgproxyUrlValue;

this.resize();
// Add event listener to intercept clicks on the viewport
document.addEventListener("click", this.handleClick.bind(this));

// Update the page URL with the current slug value while keeping all query parameters
const currentSlug = this.slugValue;
const currentPath = window.location.pathname.replace(
/\/works\/\w+/,
`/works/${currentSlug}`
);
const currentURL = `${currentPath}${window.location.search}${window.location.hash}`;
window.history.replaceState({}, "", currentURL);
console.log("slug value ", currentSlug);

// Add event listener to intercept clicks on the viewport
document.addEventListener("click", this.handleClick.bind(this));
}
const currentPath = `/works/${currentSlug}${window.location.pathname}`;
console.log("currentPath", currentPath);

resize() {
window.addEventListener("resize", () => {
this.imageDisplayTarget.style.width = `${window.innerWidth}px`;
this.imageDisplayTarget.style.height = `${window.innerHeight}px`;
});
const newURL = `${currentPath}${window.location.search}${window.location.hash}`;
window.history.replaceState({}, "", newURL);
}

handleClick(event) {
Expand Down

0 comments on commit a1c4a71

Please sign in to comment.