Skip to content

Commit

Permalink
add link builder to enable dynamic link building
Browse files Browse the repository at this point in the history
  • Loading branch information
afogel committed Jul 9, 2023
1 parent 082985b commit 14a1f5c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
// ./bin/rails generate stimulus controllerName

import { application } from "./application"

import LinkBuilderController from "./link_builder_controller"
application.register("link-builder", LinkBuilderController)
14 changes: 14 additions & 0 deletions app/javascript/controllers/link_builder_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="link-builder"
export default class extends Controller {
static targets = ['source', 'destination']
static values = { param: String }

updateDestinationParams() {
const url = new URL(this.destinationTarget.href);
const params = new URLSearchParams(url.search);
params.set(this.paramValue, this.sourceTarget.value)
this.destinationTarget.href = `${url.origin}${url.pathname}?${params}`;
}
}

0 comments on commit 14a1f5c

Please sign in to comment.