Merge pull request #7094 from tchak/feat-persisted-preview

feat(dossier): use real dossier for preview
This commit is contained in:
Paul Chavard 2022-04-28 12:25:24 +02:00 committed by GitHub
commit be2318b128
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 69 additions and 42 deletions

View file

@ -44,7 +44,7 @@ module Administrateurs
end
def apercu
@dossier = procedure_without_control.draft_revision.new_dossier
@dossier = procedure_without_control.draft_revision.dossier_for_preview(current_user)
@tab = apercu_tab
end

View file

@ -45,11 +45,9 @@ class Champs::SiretController < ApplicationController
end
def find_etablisement
if params[:champ_id].present?
@champ = policy_scope(Champ).find(params[:champ_id])
@etablissement = @champ&.etablissement
end
@procedure_id = @champ&.dossier&.procedure&.id || 'aperçu'
@etablissement = @champ.etablissement
@procedure_id = @champ.dossier.procedure.id
end
def find_etablissement_with_siret
@ -57,7 +55,7 @@ class Champs::SiretController < ApplicationController
end
def clear_siret_and_etablissement
@champ&.update!(value: '')
@champ.update!(value: '')
@etablissement&.destroy
end

View file

@ -62,7 +62,7 @@ module ApplicationHelper
def render_champ(champ)
champ_selector = "##{champ.input_group_id}"
form_html = render 'shared/dossiers/edit', dossier: champ.dossier, apercu: false
form_html = render 'shared/dossiers/edit', dossier: champ.dossier
champ_html = Nokogiri::HTML.fragment(form_html).at_css(champ_selector).to_s
# rubocop:disable Rails/OutputSafety
raw("document.querySelector('#{champ_selector}').outerHTML = \"#{escape_javascript(champ_html)}\";")

View file

@ -23,7 +23,7 @@ export type DeleteFeatures = (params: {
export function useFeatureCollection(
initialFeatureCollection: FeatureCollection,
{ url, enabled = true }: { url: string; enabled: boolean }
{ url }: { url: string }
) {
const [error, onError] = useError();
const [featureCollection, setFeatureCollection] = useState(
@ -96,9 +96,6 @@ export function useFeatureCollection(
source = SOURCE_SELECTION_UTILISATEUR,
external = false
}) => {
if (!enabled) {
return;
}
try {
const newFeatures: Feature[] = [];
for (const feature of features) {
@ -117,7 +114,7 @@ export function useFeatureCollection(
onError('Le polygone dessiné nest pas valide.');
}
},
[enabled, url, updateFeatureCollection, addFeatures, onError]
[url, updateFeatureCollection, addFeatures, onError]
);
const updateFeatures = useCallback<UpdateFatures>(
@ -126,9 +123,6 @@ export function useFeatureCollection(
source = SOURCE_SELECTION_UTILISATEUR,
external = false
}) => {
if (!enabled) {
return;
}
try {
const newFeatures: Feature[] = [];
for (const feature of features) {
@ -154,14 +148,11 @@ export function useFeatureCollection(
onError('Le polygone dessiné nest pas valide.');
}
},
[enabled, url, updateFeatureCollection, addFeatures, onError]
[url, updateFeatureCollection, addFeatures, onError]
);
const deleteFeatures = useCallback<DeleteFeatures>(
async ({ features, external = false }) => {
if (!enabled) {
return;
}
try {
const deletedFeatures = [];
for (const feature of features) {
@ -183,7 +174,7 @@ export function useFeatureCollection(
onError('Le polygone na pas pu être supprimé.');
}
},
[enabled, url, updateFeatureCollection, removeFeatures, onError]
[url, updateFeatureCollection, removeFeatures, onError]
);
return {

View file

@ -16,19 +16,17 @@ import { FlashMessage } from '../shared/FlashMessage';
export default function MapEditor({
featureCollection: initialFeatureCollection,
url,
options,
preview
options
}: {
featureCollection: FeatureCollection;
url: string;
preview: boolean;
options: { layers: string[] };
}) {
const [cadastreEnabled, setCadastreEnabled] = useState(false);
const { featureCollection, error, ...actions } = useFeatureCollection(
initialFeatureCollection,
{ url, enabled: !preview }
{ url }
);
return (
@ -54,14 +52,14 @@ export default function MapEditor({
<DrawLayer
featureCollection={featureCollection}
{...actions}
enabled={!preview && !cadastreEnabled}
enabled={!cadastreEnabled}
/>
{options.layers.includes('cadastres') ? (
<>
<CadastreLayer
featureCollection={featureCollection}
{...actions}
enabled={!preview && cadastreEnabled}
enabled={cadastreEnabled}
/>
<div className="cadastres-selection-control mapboxgl-ctrl-group">
<button

View file

@ -26,6 +26,7 @@
# last_commentaire_updated_at :datetime
# motivation :text
# private_search_terms :text
# for_procedure_preview :boolean
# processed_at :datetime
# search_terms :text
# state :string
@ -205,13 +206,14 @@ class Dossier < ApplicationRecord
scope :not_archived, -> { where(archived: false) }
scope :hidden_by_user, -> { where.not(hidden_by_user_at: nil) }
scope :hidden_by_administration, -> { where.not(hidden_by_administration_at: nil) }
scope :visible_by_user, -> { where(hidden_by_user_at: nil) }
scope :visible_by_user, -> { where(for_procedure_preview: false).or(where(for_procedure_preview: nil)).where(hidden_by_user_at: nil) }
scope :visible_by_administration, -> {
state_not_brouillon
.where(hidden_by_administration_at: nil)
.merge(visible_by_user.or(state_not_en_construction))
}
scope :visible_by_user_or_administration, -> { visible_by_user.or(visible_by_administration) }
scope :for_procedure_preview, -> { where(for_procedure_preview: true) }
scope :order_by_updated_at, -> (order = :desc) { order(updated_at: order) }
scope :order_by_created_at, -> (order = :asc) { order(depose_at: order, created_at: order, id: order) }
@ -542,7 +544,7 @@ class Dossier < ApplicationRecord
end
def can_transition_to_en_construction?
brouillon? && procedure.dossier_can_transition_to_en_construction?
brouillon? && procedure.dossier_can_transition_to_en_construction? && !for_procedure_preview?
end
def can_repasser_en_instruction?
@ -1244,7 +1246,7 @@ class Dossier < ApplicationRecord
end
def send_draft_notification_email
if brouillon? && !procedure.declarative?
if brouillon? && !procedure.declarative? && !for_procedure_preview?
DossierMailer.notify_new_draft(self).deliver_later
end
end

View file

@ -132,6 +132,19 @@ class ProcedureRevision < ApplicationRecord
)
end
def dossier_for_preview(user)
dossier = Dossier
.create_with(groupe_instructeur: procedure.defaut_groupe_instructeur_for_new_dossier)
.find_or_initialize_by(revision: self, user: user, for_procedure_preview: true, state: Dossier.states.fetch(:brouillon))
if dossier.new_record?
dossier.build_default_individual
dossier.save!
end
dossier
end
private
def compare_attestation_template(from_at, to_at)

View file

@ -13,7 +13,7 @@
active: @tab == 'annotations-privees')
- if @tab == 'dossier'
= render partial: "shared/dossiers/edit", locals: { dossier: @dossier, apercu: true }
= render partial: "shared/dossiers/edit", locals: { dossier: @dossier }
- else
.container
= form_for @dossier, url: '', method: :get, html: { class: 'form' } do |f|

View file

@ -1,7 +1,7 @@
.dossier-edit.container
= render partial: "shared/dossiers/submit_is_over", locals: { dossier: dossier }
- if apercu
- if dossier.for_procedure_preview?
- form_options = { url: '', method: :get }
- elsif dossier.brouillon?
- form_options = { url: brouillon_dossier_url(dossier), method: :patch }
@ -18,7 +18,7 @@
= t('views.shared.dossiers.edit.autosave')
- else
Pour enregistrer votre dossier et le reprendre plus tard, cliquez sur le bouton « Enregistrer le brouillon » en bas à gauche du formulaire.
- if !apercu && dossier.france_connect_information.present?
- if dossier.france_connect_information.present?
= render partial: "shared/dossiers/france_connect_informations", locals: { user_information: dossier.france_connect_information }
- if notice_url(dossier.procedure).present?
= link_to notice_url(dossier.procedure), target: '_blank', rel: 'noopener', class: 'button notice', title: "Pour vous aider à remplir votre dossier, vous pouvez consulter le guide de cette démarche." do
@ -40,7 +40,7 @@
= render partial: "shared/dossiers/editable_champs/editable_champ",
locals: { champ: champ, form: champ_form }
- if !apercu
- if !dossier.for_procedure_preview?
.dossier-edit-sticky-footer
.send-dossier-actions-bar
- if dossier.brouillon?

View file

@ -1,6 +1,4 @@
- preview = !champ.persisted?
- url = champs_carte_features_path(preview ? 'preview' : champ)
= react_component("MapEditor", { featureCollection: champ.to_feature_collection, url: url, preview: preview, options: champ.render_options }, class: "carte-#{champ.id}")
= react_component("MapEditor", { featureCollection: champ.to_feature_collection, url: champs_carte_features_path(champ), options: champ.render_options }, class: "carte-#{champ.id}")
.geo-areas
= render partial: 'shared/champs/carte/geo_areas', locals: { champ: champ, editing: true }

View file

@ -7,6 +7,6 @@
.dossier-container
.dossier-header.sub-header
.container
= render partial: "shared/dossiers/header", locals: { dossier: @dossier, apercu: false }
= render partial: "shared/dossiers/header", locals: { dossier: @dossier }
= render partial: "shared/dossiers/edit", locals: { dossier: @dossier, apercu: false }
= render partial: "shared/dossiers/edit", locals: { dossier: @dossier }

View file

@ -7,4 +7,4 @@
= render partial: 'users/dossiers/show/header', locals: { dossier: @dossier }
.container
= render partial: "shared/dossiers/edit", locals: { dossier: @dossier, apercu: false }
= render partial: "shared/dossiers/edit", locals: { dossier: @dossier }

View file

@ -0,0 +1,6 @@
class AddPreviewToDossiers < ActiveRecord::Migration[6.1]
def change
add_column :dossiers, :for_procedure_preview, :boolean
change_column_default :dossiers, :for_procedure_preview, false
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_04_25_140107) do
ActiveRecord::Schema.define(version: 2022_04_26_140107) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -314,6 +314,7 @@ ActiveRecord::Schema.define(version: 2022_04_25_140107) do
t.datetime "en_construction_close_to_expiration_notice_sent_at"
t.interval "en_construction_conservation_extension", default: "PT0S"
t.datetime "en_instruction_at"
t.boolean "for_procedure_preview", default: false
t.bigint "groupe_instructeur_id"
t.datetime "groupe_instructeur_updated_at"
t.datetime "hidden_at"

View file

@ -0,0 +1,16 @@
namespace :after_party do
desc 'Deployment task: set_default_for_procedure_preview_on_dossiers'
task set_default_for_procedure_preview_on_dossiers: :environment do
puts "Running deploy task 'set_default_for_procedure_preview_on_dossiers'"
Dossier
.where(for_procedure_preview: nil)
.in_batches(of: 5_000)
.update_all(for_procedure_preview: false)
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end

View file

@ -23,7 +23,11 @@ describe Administrateurs::ProceduresController, type: :controller do
get :apercu, params: { id: procedure.id }
end
it { expect(response).to have_http_status(:ok) }
it do
expect(response).to have_http_status(:ok)
expect(procedure.dossiers.visible_by_user).to be_empty
expect(procedure.dossiers.for_procedure_preview).not_to be_empty
end
end
let(:procedure_params) {