diff --git a/app/controllers/template_documents_controller.rb b/app/controllers/template_documents_controller.rb index d18c064f..f70e9ecf 100644 --- a/app/controllers/template_documents_controller.rb +++ b/app/controllers/template_documents_controller.rb @@ -6,6 +6,8 @@ class TemplateDocumentsController < ApplicationController def create return head :unprocessable_entity if params[:blobs].blank? && params[:files].blank? + old_fields_hash = @template.fields.hash + documents = Templates::CreateAttachments.call(@template, params) schema = documents.map do |doc| @@ -14,6 +16,8 @@ class TemplateDocumentsController < ApplicationController render json: { schema:, + fields: old_fields_hash == @template.fields.hash ? nil : @template.fields, + submitters: old_fields_hash == @template.fields.hash ? nil : @template.submitters, documents: documents.as_json( methods: %i[metadata signed_uuid], include: { diff --git a/app/javascript/template_builder/builder.vue b/app/javascript/template_builder/builder.vue index b0bea84a..bb80edc4 100644 --- a/app/javascript/template_builder/builder.vue +++ b/app/javascript/template_builder/builder.vue @@ -971,14 +971,26 @@ export default { this.save() }, - updateFromUpload ({ schema, documents }) { - this.template.schema.push(...schema) - this.template.documents.push(...documents) + updateFromUpload (data) { + this.template.schema.push(...data.schema) + this.template.documents.push(...data.documents) + + if (data.fields) { + this.template.fields = data.fields + } + + if (data.submitters) { + this.template.submitters = data.submitters + + if (!this.template.submitters.find((s) => s.uuid === this.selectedSubmitter?.uuid)) { + this.selectedSubmitter = this.template.submitters[0] + } + } this.$nextTick(() => { this.$refs.previews.scrollTop = this.$refs.previews.scrollHeight - this.scrollIntoDocument(schema[0]) + this.scrollIntoDocument(data.schema[0]) }) if (this.template.name === 'New Document') { @@ -1018,9 +1030,39 @@ export default { this.save() }, - onDocumentReplace ({ replaceSchemaItem, schema, documents }) { + onDocumentReplace (data) { + const { replaceSchemaItem, schema, documents } = data + this.template.schema.splice(this.template.schema.indexOf(replaceSchemaItem), 1, schema[0]) this.template.documents.push(...documents) + + if (data.fields) { + this.template.fields = data.fields + + const removedFieldUuids = [] + + this.template.fields.forEach((field) => { + [...(field.areas || [])].forEach((area) => { + if (area.attachment_uuid === replaceSchemaItem.attachment_uuid) { + field.areas.splice(field.areas.indexOf(area), 1) + + removedFieldUuids.push(field.uuid) + } + }) + }) + + this.template.fields = + this.template.fields.filter((f) => !removedFieldUuids.includes(f.uuid) || f.areas?.length) + } + + if (data.submitters) { + this.template.submitters = data.submitters + + if (!this.template.submitters.find((s) => s.uuid === this.selectedSubmitter?.uuid)) { + this.selectedSubmitter = this.template.submitters[0] + } + } + this.template.fields.forEach((field) => { (field.areas || []).forEach((area) => { if (area.attachment_uuid === replaceSchemaItem.attachment_uuid) { diff --git a/app/javascript/template_builder/field_submitter.vue b/app/javascript/template_builder/field_submitter.vue index 5f479b33..e4446c6c 100644 --- a/app/javascript/template_builder/field_submitter.vue +++ b/app/javascript/template_builder/field_submitter.vue @@ -95,7 +95,7 @@