From 117981d2f5bad7b7668807ff1c8c68b0b5f5b614 Mon Sep 17 00:00:00 2001 From: alexdbondoc17 Date: Thu, 10 Mar 2022 02:41:03 +0000 Subject: [PATCH] Enhance `doctrines#form` --- app/components/doctrine_form_component.rb | 10 +++ .../doctrine_form_component.html.erb | 32 ++++++++++ app/controllers/doctrines_controller.rb | 4 ++ .../controllers/doctrines_controller.js | 25 ++++---- app/reflexes/doctrines_reflex.rb | 6 ++ app/views/doctrines/edit.html.erb | 12 ++++ app/views/doctrines/new.html.erb | 17 +++++ app/views/document/doctrines/show.html.erb | 2 +- app/views/documents/show.html.erb | 2 +- app/views/shared/_annotation_form.html.erb | 64 +++++++++++++++++++ config/routes.rb | 6 +- 11 files changed, 165 insertions(+), 15 deletions(-) create mode 100644 app/components/doctrine_form_component.rb create mode 100644 app/components/doctrine_form_component/doctrine_form_component.html.erb create mode 100644 app/views/doctrines/edit.html.erb create mode 100644 app/views/doctrines/new.html.erb create mode 100644 app/views/shared/_annotation_form.html.erb diff --git a/app/components/doctrine_form_component.rb b/app/components/doctrine_form_component.rb new file mode 100644 index 0000000..6fcfa81 --- /dev/null +++ b/app/components/doctrine_form_component.rb @@ -0,0 +1,10 @@ +class DoctrineFormComponent < BaseComponent + attr_reader :doctrine, :jurisprudence_id, :opts + + def initialize(current_user:, doctrine:, jurisprudence_id:, opts: {}) + @doctrine = doctrine + @jurisprudence_id = jurisprudence_id + @opts = opts + end +end + \ No newline at end of file diff --git a/app/components/doctrine_form_component/doctrine_form_component.html.erb b/app/components/doctrine_form_component/doctrine_form_component.html.erb new file mode 100644 index 0000000..67e47a9 --- /dev/null +++ b/app/components/doctrine_form_component/doctrine_form_component.html.erb @@ -0,0 +1,32 @@ +
+
+ + <%= hidden_field_tag :document_id, jurisprudence_id, data: { target: "doctrines.document_id" } %> + <%= hidden_field_tag :doctrine_id, nil, data: { target: "doctrines.doctrine_id" } %> + +
Subject/s cannot be blank.
+
+
+ +
+
+ + +
+
+ +
+
+ + <%= text_field_tag :headnote, nil, class: "form-control", placeholder: "Head note", required: true, data: { target: "doctrines.headnote" } %> +
Headnote cannot be blank.
+
+
+ +
+
+ + <%= rich_text_area_tag :content, nil, placeholder: "Doctrine Content", required: true, data: { target: "doctrines.content" } %> +
Content cannot be blank.
+
+
\ No newline at end of file diff --git a/app/controllers/doctrines_controller.rb b/app/controllers/doctrines_controller.rb index d8835de..12818a7 100644 --- a/app/controllers/doctrines_controller.rb +++ b/app/controllers/doctrines_controller.rb @@ -4,6 +4,10 @@ class DoctrinesController < ApplicationController load_and_authorize_resource :doctrine, class: "Doctrine" + def new; end + + def edit; end + def index @search = doctrine_search(search_params) @doctrines = @search.results diff --git a/app/javascript/controllers/doctrines_controller.js b/app/javascript/controllers/doctrines_controller.js index 42d4de3..2be808a 100644 --- a/app/javascript/controllers/doctrines_controller.js +++ b/app/javascript/controllers/doctrines_controller.js @@ -49,21 +49,22 @@ export default class extends ApplicationController { } save () { - var $this = this, $modal = $("#doctrineModal"), document_id = $this.document_idTarget.value, doctrine_id = $this.doctrine_idTarget.value - var params = { subject_ids: $modal.find("select[name='subject_ids[]']").val(), headnote: $this.headnoteTarget.value, + var $this = this, $form = $("#doctrineForm"), document_id = $this.document_idTarget.value, doctrine_id = $this.doctrine_idTarget.value + var params = { subject_ids: $form.find("select[name='subject_ids[]']").val(), headnote: $this.headnoteTarget.value, content: $this.contentTarget.value } + console.log(params) if (!params.subject_ids.length) { - $("#subjects-blank-message").show() + $form.find("#subjects-blank-message").show() } if (!params.headnote.length) { - $("#headnote-blank-message").show() + $form.find("#headnote-blank-message").show() } if (!params.content.length) { - $("#content-blank-message").show() + $form.find("#content-blank-message").show() } if (params.subject_ids.length && params.headnote.length && params.content.length) { @@ -93,7 +94,7 @@ export default class extends ApplicationController { const contrim_alert = confirm("Do you want to create an Annotation?") if (contrim_alert) { - $this.stimulate("DoctrinesReflex#render_annotation_modal_form", document_id, data.id) + $this.stimulate("DoctrinesReflex#render_annotation_form", document_id, data.id) $('.toast').fadeOut(5000) } else { $(".btn-close-x").trigger("click") @@ -132,18 +133,18 @@ export default class extends ApplicationController { } saveAnnotation () { - var $modal = $("#doctrineModal") - const params = { doctrine_id: $modal.find("#doctrine_id").val(), document_id: $modal.find("#document_id").val(), - document_ids: $modal.find("#document_ids").val().split(","), content: $modal.find("#document_title").val(), - phil_rep: $modal.find("#phil_rep").val(), editor_notes: $modal.find("#editor_notes").val(), - annomark_ids: $modal.find("select[name='annomark_ids[]']").val() } + var $form = $("#doctrineForm") + const params = { doctrine_id: $form.find("#doctrine_id").val(), document_id: $form.find("#document_id").val(), + document_ids: $form.find("#document_ids").val().split(","), content: $form.find("#document_title").val(), + phil_rep: $form.find("#phil_rep").val(), editor_notes: $form.find("#editor_notes").val(), + annomark_ids: $form.find("select[name='annomark_ids[]']").val() } $.post("/api/doctrines/" + params.doctrine_id + "/annotations", params, function (result, status) { if(status === "success") { $(".btn-close-x").trigger("click"); $('.toast').addClass('bg-success').show(); $( ".toast-body" ).html( "Submitted Successfully" ); - location.pathname = "/documents/" + $modal.find("#current_document_id").val() + "/doctrines/" + params.doctrine_id + location.pathname = "/documents/" + $form.find("#current_document_id").val() + "/doctrines/" + params.doctrine_id } else { $(".btn-close-x").trigger("click") $('.toast').addClass('bg-danger').show() diff --git a/app/reflexes/doctrines_reflex.rb b/app/reflexes/doctrines_reflex.rb index 46ad6dc..df77e8f 100644 --- a/app/reflexes/doctrines_reflex.rb +++ b/app/reflexes/doctrines_reflex.rb @@ -13,4 +13,10 @@ class DoctrinesReflex < ApplicationReflex document = Cdao::Document.where(reference_number: jurisprudence.reference_number).first morph "#doctrineModal", render(partial: "/shared/annotation_modal_form", locals: { document: document, doctrine_id: doctrine_id, jurisprudence: jurisprudence }) end + + def render_annotation_form(document_id, doctrine_id) + jurisprudence = Cdao::Jurisprudence.find(document_id) + document = Cdao::Document.where(reference_number: jurisprudence.reference_number).first + morph "#doctrineForm", render(partial: "/shared/annotation_form", locals: { annotation: Annotation.new, document: document, doctrine_id: doctrine_id, jurisprudence: jurisprudence }) + end end diff --git a/app/views/doctrines/edit.html.erb b/app/views/doctrines/edit.html.erb new file mode 100644 index 0000000..9ed4d71 --- /dev/null +++ b/app/views/doctrines/edit.html.erb @@ -0,0 +1,12 @@ +
+
+

New Doctrine

+
+ <%= render(DoctrineFormComponent.new(current_user: current_user, doctrine: @doctrine)) %> +
+ +
+
diff --git a/app/views/doctrines/new.html.erb b/app/views/doctrines/new.html.erb new file mode 100644 index 0000000..f0820f4 --- /dev/null +++ b/app/views/doctrines/new.html.erb @@ -0,0 +1,17 @@ +
+
+

New Doctrine

+
+ <%= render(DoctrineFormComponent.new(current_user: current_user, doctrine: @doctrine, jurisprudence_id: params[:jurisprudence_id])) %> +
+ +
+
+
diff --git a/app/views/document/doctrines/show.html.erb b/app/views/document/doctrines/show.html.erb index 1b2b2e4..1e7d41d 100644 --- a/app/views/document/doctrines/show.html.erb +++ b/app/views/document/doctrines/show.html.erb @@ -47,7 +47,7 @@
diff --git a/app/views/documents/show.html.erb b/app/views/documents/show.html.erb index 045181f..4c3ca3e 100644 --- a/app/views/documents/show.html.erb +++ b/app/views/documents/show.html.erb @@ -65,7 +65,7 @@
<%= render(DoctrineModalFormComponent.new(current_user: current_user)) %> diff --git a/app/views/shared/_annotation_form.html.erb b/app/views/shared/_annotation_form.html.erb new file mode 100644 index 0000000..4cfb823 --- /dev/null +++ b/app/views/shared/_annotation_form.html.erb @@ -0,0 +1,64 @@ + +
+

New Annotation

+
+
+
+ <%= label_tag :annotation_marks %> + <%= hidden_field_tag :doctrine_id, doctrine_id %> + <%= hidden_field_tag :current_document_id, jurisprudence.id %> + <%= hidden_field_tag :document_id, document.id %> + <%= hidden_field_tag :document_ids, nil %> + <%= select_tag "annomark_ids[]", options_from_collection_for_select(Annomark.all.order(name: :asc), :id, :name), + class: "form-control default-selectize ps-0", multiple: true, prompt: "Please select" %> +
+
+ +
+
+ <%= label_tag :document_title %> + <%= text_area_tag :document_title, document.display_text, class: "form-control selected-citing-docs" %> +
+
+ +
+
+
+ <%= text_field_tag :q, nil, class: "form-control", placeholder: "Search GR Number, Title or Short Title", data: { target: "annotations.q" } %> +
+
+ +
+
+ +
+ + + + + + + +
Reference No. Title Date
+
+
+ +
+
+ <%= label_tag :phil_rep %> + <%= text_field_tag :phil_rep, document.phil_rep, class: "form-control" %> +
+
+ +
+
+ <%= label_tag :editor_notes %> + <%= rich_text_area_tag :editor_notes, nil, placeholder: "Editor Notes" %> +
+
+
+ +
diff --git a/config/routes.rb b/config/routes.rb index 18439bd..68d0fdf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ Rails.application.routes.draw do root to: "documents#index" - resources :doctrines, only: %i[index show] do + resources :doctrines, only: %i[index show new edit] do get :search, on: :collection scope module: :doctrine do resources :annotations, only: %i[create update destroy] @@ -27,6 +27,10 @@ Rails.application.routes.draw do resources :reports, only: %i[index] + namespace :api, defaults: { format: :html } do + resources :doctrines, only: %i[new edit] + end + namespace :api, defaults: { format: :json } do resources :doctrines do scope module: :doctrine do