Files
openfoodnetwork/app/webpacker/controllers/modal_link_controller.js
Ahmed Ejaz 6659ffe530 12398: update modal-link-controller to dynamically set
form action as per the selected product to delete
2024-05-24 01:16:14 +05:00

49 lines
1.4 KiB
JavaScript

import { Controller } from "stimulus";
export default class extends Controller {
static values = { target: String, modalDataset: Object };
open() {
let modal = document.getElementById(this.targetValue);
let modalController = this.application.getControllerForElementAndIdentifier(
modal,
this.getIdentifier()
);
modalController.open();
}
setModalDataSetOnConfirm(event) {
try {
const modalId = this.targetValue;
const moodalConfirmButtonQuery = `#${modalId} #modal-confirm-button`;
const confirmButton = document.querySelector(moodalConfirmButtonQuery);
this.#setPathToFormAction(confirmButton);
Object.keys(this.modalDatasetValue).forEach((datasetKey) => {
confirmButton.setAttribute(datasetKey, this.modalDatasetValue[datasetKey]);
});
} catch (e) {
// In case of any type of error in setting the dataset value, stop the further actions i.e. opening the modal
event.stopImmediatePropagation();
throw e;
}
}
getIdentifier() {
return "modal";
}
#setPathToFormAction(confirmButton) {
const isSubmitButton = confirmButton.type === 'submit';
const path = this.modalDatasetValue['data-path'];
if(isSubmitButton && path){
const form = confirmButton.parentElement;
form.setAttribute('action', path);
delete this.modalDatasetValue['data-path'];
}
}
}