diff --git a/app/controllers/api/templates_documents_controller.rb b/app/controllers/api/templates_documents_controller.rb index dc6995ce..2956091c 100644 --- a/app/controllers/api/templates_documents_controller.rb +++ b/app/controllers/api/templates_documents_controller.rb @@ -26,11 +26,11 @@ module Api def del_image template = Template.find(params[:template_id]) - attachment_id = params[:attachment_id] - document_id = params[:documentId] - page_number = template.documents.find(document_id).preview_images.find_index { |pic| pic.id == attachment_id } + document = template.documents.find(params[:documentId]) + img_attachment_id = params[:attachment_id] + page_number = document.preview_images.find_index { |pic| pic.id == img_attachment_id } if page_number - Templates::ProcessDocument.delete_picture(template, attachment_id, page_number) + Templates::ProcessDocument.delete_picture(template, document, img_attachment_id, page_number) render json: { success: true, message: 'image deleted successfully' } else page_number = "No image found for deletion" diff --git a/app/javascript/pdf_loader.js b/app/javascript/pdf_loader.js index 3dff5b13..464ecf2d 100644 --- a/app/javascript/pdf_loader.js +++ b/app/javascript/pdf_loader.js @@ -1,15 +1,17 @@ -document.addEventListener("DOMContentLoaded", function () { - function showLoading() { - document.getElementById("loader").style.display = "block"; +document.addEventListener('DOMContentLoaded', function () { + function showLoading () { + document.getElementById('loader').style.display = 'block' } - var editTemplateLink = document.getElementById("edit-template-link"); - editTemplateLink.addEventListener("click", function (event) { - event.preventDefault(); - showLoading(); + const editTemplateLink = document.getElementById('edit-template-link') + if (editTemplateLink) { + editTemplateLink.addEventListener('click', function (event) { + event.preventDefault() + showLoading() - var template = this.getAttribute("data-template"); - setTimeout(function () { - window.location.href = template; - }, 3000); - }); -}); + const template = this.getAttribute('data-template') + setTimeout(function () { + window.location.href = template + }, 3000) + }) + } +}) diff --git a/app/javascript/template_builder/builder.vue b/app/javascript/template_builder/builder.vue index ff4b4d6b..f1551d78 100644 --- a/app/javascript/template_builder/builder.vue +++ b/app/javascript/template_builder/builder.vue @@ -452,7 +452,6 @@ export default { scrollIntoDocument (item, page) { const documentRef = this.documentRefs.find((e) => e.document.uuid === item.attachment_uuid) documentRef.scrollIntoDocument(page) - // pageRef.$el.scrollIntoView({ behavior: 'smooth', block: 'start' }) }, onKeyUp (e) { if (e.code === 'Escape') { @@ -648,7 +647,7 @@ export default { this.save() }, onDocumentRemove (item) { - if (window.confirm('Are you sure?')) { + if (window.confirm('Are you sure you want to delete the document?')) { this.template.schema.splice(this.template.schema.indexOf(item), 1) } @@ -744,58 +743,46 @@ export default { }) }, removeImage (item, imageId) { - // console.log(this.template.documents[0].preview_secured_images) - // output: 0: {id: 26, name: 'preview_images', uuid: 'db2de68e-c52f-41e0-a743-550a5ba26ec0', record_type: 'ActiveStorage::Attachment', record_id: 25, …} 1 : {id: 27, name: 'preview_images', uuid: '4f2cd882-95fb-4344-8571-78c9bb5a20aa', record_type: 'ActiveStorage::Attachment', record_id: 25, …} 2: {id: 28, name: 'preview_images', uuid: '28bb656a-0799-4a73-b665-692aab2c690b', record_type: 'ActiveStorage::Attachment', record_id: 25, …} length: 3, imageId: 27 - // const indexToRemove = this.template.documents.findIndex(item => item.id === imageId) - this.template.documents.forEach((document) => { - // Check if the document has a preview_images array - if (Array.isArray(document.preview_images)) { - // Find the index of the preview image with the matching id - const indexToRemove = document.preview_images.findIndex( - (previewImage) => previewImage.id === imageId - ) - // console.log('simple preview', document.preview_images, ': secured', document.preview_secured_images) - console.log(indexToRemove) - if (indexToRemove !== -1) { - const confirmed = window.confirm('Are you sure you want to delete this image?') - if (confirmed) { - const documentId = document.id - const apiUrl = `/api/templates/${this.template.id}/documents/${documentId}/del_image` - fetch(apiUrl, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - template: this.template.id, - attachment_id: imageId, - documentId - // page_number: document.preview_images.find(x => x.id === imageId).name - }) + const document = this.template.documents.find((e) => e.uuid === item.attachment_uuid) + if (Array.isArray(document.preview_images)) { + const indexToRemove = document.preview_images.findIndex((previewImage) => previewImage.id === imageId) + console.log(indexToRemove) + if (indexToRemove !== -1) { + const confirmed = window.confirm('Are you sure you want to delete this image?') + if (confirmed) { + const documentId = document.id + const apiUrl = `/api/templates/${this.template.id}/documents/${documentId}/del_image` + fetch(apiUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + template: this.template.id, + attachment_id: imageId, + documentId + }) + }) + .then((response) => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`) + } + return response.json() + }) + .then((data) => { + document.preview_images.splice(indexToRemove, 1) + window.location.reload() + console.log('Success:', data) + }) + .catch((error) => { + console.error('Error:', error) }) - .then((response) => { - if (!response.ok) { - throw new Error(`HTTP error! Status: ${response.status}`) - } - return response.json() - }) - .then((data) => { - // remove from frontend - document.preview_images.splice(indexToRemove, 1) - console.log('Success:', data) - }) - .catch((error) => { - console.error('Error:', error) - }) - } } } - }) + } }, addBlankPage (item) { - const documentRef = this.documentRefs.find( - (e) => e.document.uuid === item.attachment_uuid - ) + const documentRef = this.documentRefs.find((e) => e.document.uuid === item.attachment_uuid) const confirmed = window.confirm('Are you sure you want to create new image?') if (confirmed) { const documentId = documentRef.document.id @@ -817,6 +804,7 @@ export default { return response.json() }) .then((data) => { + window.location.reload() console.log('Success: ---', data) }) .catch((error) => { diff --git a/app/javascript/template_builder/preview.vue b/app/javascript/template_builder/preview.vue index c66f7a49..52b13e5f 100644 --- a/app/javascript/template_builder/preview.vue +++ b/app/javascript/template_builder/preview.vue @@ -19,7 +19,18 @@ v-if="editable && index==0" class="flex justify-between w-full" > -
+