-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace repository autocompletes with stimulus controller
- Loading branch information
1 parent
43c851d
commit 6f8de3f
Showing
3 changed files
with
63 additions
and
6 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/api/app/javascript/controllers/repository_autocomplete_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static targets = [ "select" ] | ||
static values = { url: String } | ||
|
||
connect() { | ||
this.selectTarget.options.length = 0; | ||
this.disableSelect(); | ||
} | ||
|
||
disableSelect() { | ||
this.selectTarget.setAttribute('disabled', '') | ||
} | ||
|
||
enableSelect() { | ||
this.selectTarget.removeAttribute('disabled') | ||
} | ||
|
||
emptySelect() { | ||
this.setSelect([]); | ||
} | ||
|
||
setSelect(results) { | ||
this.selectTarget.options.length = 0; | ||
if (results.length < 1) | ||
return this.disableSelect(); | ||
results.forEach((result, key) => { | ||
this.selectTarget[key] = new Option(result, result) | ||
}); | ||
this.enableSelect(); | ||
} | ||
|
||
async setRepositories(event) { | ||
let url = new URL(this.urlValue, window.location.origin); | ||
url.searchParams.set('project', event.detail); | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
const data = await response.text(); | ||
this.setSelect(JSON.parse(data)); | ||
} catch { | ||
console.error("Failed to return results"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters