2026-01-15 11:37:50 -05:00
|
|
|
import { Controller } from "@hotwired/stimulus";
|
|
|
|
|
|
|
|
|
|
export default class extends Controller {
|
2026-03-05 11:30:24 -05:00
|
|
|
static values = {
|
|
|
|
|
logoType: String,
|
|
|
|
|
employerName: String
|
2026-03-06 10:56:20 -05:00
|
|
|
}
|
2026-03-13 08:47:13 -04:00
|
|
|
static targets = ["preview", "previewContainer", "logoSelect", "logoIdField", "logoNameField", "initialLogoFile"];
|
2026-01-15 11:37:50 -05:00
|
|
|
|
|
|
|
|
async connect() {
|
2026-03-03 22:53:21 -05:00
|
|
|
console.log('in connect');
|
2026-03-13 08:47:13 -04:00
|
|
|
this.setPreviewImage()
|
|
|
|
|
// Remember to revoke the URL when the controller is disconnected if necessary
|
|
|
|
|
// this.disconnect = () => URL.revokeObjectURL(objectUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async setPreviewImage() {
|
|
|
|
|
const initValue = this.logoIdFieldTarget.value
|
2026-03-05 11:30:24 -05:00
|
|
|
console.log(initValue)
|
2026-03-13 08:47:13 -04:00
|
|
|
console.log(this.logoTypeValue)
|
2026-03-05 11:30:24 -05:00
|
|
|
if (initValue) {
|
|
|
|
|
const response = await fetch(`/id_card/${this.logoTypeValue}_logos/${initValue}/image`); // Fetch the binary data
|
2026-03-13 08:47:13 -04:00
|
|
|
const logoType = this.logoTypeValue
|
|
|
|
|
if (logoType == "employer") {
|
|
|
|
|
const contentDisposition = response.headers.get('Content-Disposition');
|
|
|
|
|
const filename = contentDisposition.match(/filename="?([^"]+)"?/)[1];
|
|
|
|
|
this.logoNameFieldTarget.value = filename;
|
|
|
|
|
}
|
2026-01-15 11:37:50 -05:00
|
|
|
const blob = await response.blob();
|
|
|
|
|
const objectUrl = URL.createObjectURL(blob);
|
|
|
|
|
this.previewTarget.src = objectUrl;
|
|
|
|
|
this.previewContainerTarget.classList.remove("hidden");
|
2026-03-13 08:47:13 -04:00
|
|
|
} else {
|
|
|
|
|
this.previewContainerTarget.classList.add("hidden");
|
2026-01-15 11:37:50 -05:00
|
|
|
}
|
2026-03-13 08:47:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setSelectPreview(event) {
|
|
|
|
|
this.setPreviewImage()
|
2026-01-15 11:37:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uploadLogo(event) {
|
|
|
|
|
console.log('in uploadLogo');
|
|
|
|
|
event.preventDefault()
|
2026-03-06 10:56:20 -05:00
|
|
|
let logoFile = event.target.files[0];
|
|
|
|
|
if (!logoFile) return;
|
2026-01-15 11:37:50 -05:00
|
|
|
|
2026-03-05 11:30:24 -05:00
|
|
|
const logoType = this.logoTypeValue
|
2026-01-15 11:37:50 -05:00
|
|
|
|
2026-03-06 10:56:20 -05:00
|
|
|
let newFileName = logoFile.name
|
2026-01-15 11:37:50 -05:00
|
|
|
if (logoType == "network") {
|
|
|
|
|
console.log("n " + newFileName);
|
2026-03-06 10:56:20 -05:00
|
|
|
newFileName = this.determineNetworkFilename(logoFile)
|
2026-01-15 11:37:50 -05:00
|
|
|
} else if (logoType == "employer") {
|
2026-03-06 10:56:20 -05:00
|
|
|
newFileName = this.determineEmployerFilename(logoFile)
|
2026-01-15 11:37:50 -05:00
|
|
|
}
|
2026-03-13 08:47:13 -04:00
|
|
|
logoFile = new File([logoFile], newFileName)
|
2026-03-06 10:56:20 -05:00
|
|
|
|
|
|
|
|
this.uploadLogoToServer(logoFile)
|
|
|
|
|
.then((result) => {
|
|
|
|
|
console.log(result);
|
|
|
|
|
const logoId = result.id
|
|
|
|
|
this.previewFile(logoFile);
|
2026-03-13 08:47:13 -04:00
|
|
|
if (logoType == "network") {
|
|
|
|
|
this.addOptionToSelect(newFileName, logoId)
|
|
|
|
|
} else {
|
|
|
|
|
this.logoNameFieldTarget.value = newFileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.logoIdFieldTarget.value = logoId;
|
|
|
|
|
|
|
|
|
|
|
2026-03-06 10:56:20 -05:00
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
// Handle any errors that occurred
|
|
|
|
|
console.error(error);
|
|
|
|
|
});
|
2026-01-15 11:37:50 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:56:20 -05:00
|
|
|
previewFile(logoFile) {
|
2026-01-15 11:37:50 -05:00
|
|
|
console.log('in previewFile');
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onload = (e) => {
|
|
|
|
|
this.previewTarget.src = e.target.result;
|
|
|
|
|
this.previewContainerTarget.classList.remove("hidden");
|
|
|
|
|
};
|
2026-03-06 10:56:20 -05:00
|
|
|
reader.readAsDataURL(logoFile);
|
2026-01-15 11:37:50 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:56:20 -05:00
|
|
|
async uploadLogoToServer(logoFile) {
|
2026-01-15 11:37:50 -05:00
|
|
|
console.log('in uploadLogoToServer');
|
|
|
|
|
const formData = new FormData();
|
2026-03-06 10:56:20 -05:00
|
|
|
formData.append(`id_card_${this.logoTypeValue}_logo[logo_file]`, logoFile);
|
2026-01-15 11:37:50 -05:00
|
|
|
|
|
|
|
|
const csrfToken = document.querySelector("meta[name='csrf-token']").content;
|
|
|
|
|
|
|
|
|
|
try {
|
2026-03-05 11:30:24 -05:00
|
|
|
const response = await fetch(`/id_card/${this.logoTypeValue}_logos/`, {
|
2026-01-15 11:37:50 -05:00
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"X-CSRF-Token": csrfToken
|
|
|
|
|
},
|
|
|
|
|
body: formData
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
|
console.log('Upload successful!')
|
2026-03-06 10:56:20 -05:00
|
|
|
const data = await response.json();
|
|
|
|
|
return data;
|
2026-01-15 11:37:50 -05:00
|
|
|
} else {
|
2026-03-06 10:56:20 -05:00
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
2026-01-15 11:37:50 -05:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Network error:", error);
|
2026-03-06 10:56:20 -05:00
|
|
|
throw new Error("Network error:", error);
|
2026-01-15 11:37:50 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-13 08:47:13 -04:00
|
|
|
addOptionToSelect(name, id) {
|
2026-01-15 11:37:50 -05:00
|
|
|
const blankOptionIndex = 0;
|
2026-03-13 08:47:13 -04:00
|
|
|
const newOption = new Option(name, id, true, true)
|
2026-01-15 11:37:50 -05:00
|
|
|
|
2026-03-13 08:47:13 -04:00
|
|
|
if (this.logoIdFieldTarget.options.length > blankOptionIndex + 1) {
|
|
|
|
|
this.logoIdFieldTarget.insertBefore(newOption, this.logoIdFieldTarget.options[blankOptionIndex + 1]);
|
2026-01-15 11:37:50 -05:00
|
|
|
} else {
|
2026-03-13 08:47:13 -04:00
|
|
|
this.logoIdFieldTarget.appendChild(newOption);
|
2026-01-15 11:37:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:56:20 -05:00
|
|
|
determineNetworkFilename(logoFile) {
|
|
|
|
|
const fileExtension = logoFile.name.split('.').pop();
|
2026-01-15 11:37:50 -05:00
|
|
|
const primaryNetworkName = prompt("Enter the name for the primary network (Usually 'Cigna' or 'MedCost':");
|
2026-03-13 08:47:13 -04:00
|
|
|
const secondaryNetworkName = prompt("Enter the name for the partner network (ex: Health Partners):");
|
2026-01-15 11:37:50 -05:00
|
|
|
const logoFilename = this.titleizeText(primaryNetworkName).concat(this.titleizeText(secondaryNetworkName)).concat("Logo.").concat(fileExtension).replaceAll(' ', '');
|
|
|
|
|
|
|
|
|
|
return logoFilename
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:56:20 -05:00
|
|
|
determineEmployerFilename(logoFile) {
|
|
|
|
|
const fileExtension = logoFile.name.split('.').pop();
|
2026-03-05 11:30:24 -05:00
|
|
|
const employerName = this.employerNameValue
|
2026-01-15 11:37:50 -05:00
|
|
|
const logoFilename = this.titleizeText(employerName).concat("Logo.").concat(fileExtension).replaceAll(' ', '');
|
|
|
|
|
|
|
|
|
|
return logoFilename
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
titleizeText(text) {
|
|
|
|
|
const titleized = text.toLowerCase();
|
|
|
|
|
|
|
|
|
|
return titleized.replace(/(^|\s)\S/g, function(match) {
|
|
|
|
|
return match.toUpperCase();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-06 10:56:20 -05:00
|
|
|
|