mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-12 18:36:49 +00:00
51 lines
1.2 KiB
JavaScript
51 lines
1.2 KiB
JavaScript
import { Controller } from "stimulus";
|
|
|
|
export default class extends Controller {
|
|
static values = { initialPermalink: String, url: String };
|
|
static targets = ["spinner", "permalinkField", "available", "unavailable"];
|
|
|
|
initialize() {
|
|
this.validate = _.debounce(this.validate, 300);
|
|
}
|
|
|
|
async validate() {
|
|
this.hideAvailability();
|
|
this.showSpinner();
|
|
|
|
const response = await fetch(this.urlValue + `?permalink="${this.permalinkFieldTarget.value}"`);
|
|
const result = await response.text();
|
|
|
|
if (this.initialPermalinkValue == result) {
|
|
this.permalinkFieldTarget.value = result;
|
|
this.hideSpinner();
|
|
return;
|
|
}
|
|
|
|
this.displayAvailability(response);
|
|
|
|
this.hideSpinner();
|
|
this.permalinkFieldTarget.value = result;
|
|
}
|
|
|
|
displayAvailability(response) {
|
|
if (response.ok) {
|
|
this.availableTarget.classList.remove("hidden");
|
|
} else {
|
|
this.unavailableTarget.classList.remove("hidden");
|
|
}
|
|
}
|
|
|
|
hideAvailability() {
|
|
this.availableTarget.classList.add("hidden");
|
|
this.unavailableTarget.classList.add("hidden");
|
|
}
|
|
|
|
showSpinner() {
|
|
this.spinnerTarget.classList.remove("hidden");
|
|
}
|
|
|
|
hideSpinner() {
|
|
this.spinnerTarget.classList.add("hidden");
|
|
}
|
|
}
|