feat(dossier): implement champ polling

This commit is contained in:
Paul Chavard 2023-05-30 11:39:53 +02:00
parent 52545dc86c
commit a9f431caa5
5 changed files with 64 additions and 4 deletions

View file

@ -25,15 +25,35 @@ class EditableChamp::EditableChampComponent < ApplicationComponent
"hidden": !@champ.visible?
),
id: @champ.input_group_id,
data: { controller: stimulus_controller, **data_dependent_conditions }
data: { controller: stimulus_controller, **data_dependent_conditions, **stimulus_values }
}
end
def stimulus_values
if @champ.fetch_external_data_pending?
{ turbo_poll_url_value: }
else
{}
end
end
def turbo_poll_url_value
if @champ.private?
annotation_instructeur_dossier_path(@champ.dossier.procedure, @champ.dossier, @champ)
else
champ_dossier_path(@champ.dossier, @champ)
end
end
def stimulus_controller
if autosave_enabled?
# This is an editable champ. Lets find what controllers it might need.
controllers = ['autosave']
if @champ.fetch_external_data_pending?
controllers << 'turbo-poll'
end
controllers.join(' ')
end
end

View file

@ -302,6 +302,20 @@ module Instructeurs
render layout: "print"
end
def annotation
@dossier = dossier_with_champs(pj_template: false)
annotation = @dossier.champs_private_all.find(params[:annotation_id])
respond_to do |format|
format.turbo_stream do
@to_show, @to_hide = []
@to_update = [annotation]
render :update_annotations
end
end
end
def telecharger_pjs
files = ActiveStorage::DownloadableFile.create_list_from_dossiers(Dossier.where(id: dossier.id), with_champs_private: true, include_infos_administration: true)
cleaned_files = ActiveStorage::DownloadableFile.cleanup_list_from_dossier(files)

View file

@ -6,11 +6,11 @@ module Users
layout 'procedure_context', only: [:identite, :update_identite, :siret, :update_siret]
ACTIONS_ALLOWED_TO_ANY_USER = [:index, :recherche, :new, :transferer_all]
ACTIONS_ALLOWED_TO_OWNER_OR_INVITE = [:show, :destroy, :demande, :messagerie, :brouillon, :submit_brouillon, :submit_en_construction, :modifier, :modifier_legacy, :update, :create_commentaire, :papertrail, :restore]
ACTIONS_ALLOWED_TO_OWNER_OR_INVITE = [:show, :destroy, :demande, :messagerie, :brouillon, :submit_brouillon, :submit_en_construction, :modifier, :modifier_legacy, :update, :create_commentaire, :papertrail, :restore, :champ]
before_action :ensure_ownership!, except: ACTIONS_ALLOWED_TO_ANY_USER + ACTIONS_ALLOWED_TO_OWNER_OR_INVITE
before_action :ensure_ownership_or_invitation!, only: ACTIONS_ALLOWED_TO_OWNER_OR_INVITE
before_action :ensure_dossier_can_be_updated, only: [:update_identite, :update_siret, :brouillon, :submit_brouillon, :submit_en_construction, :modifier, :modifier_legacy, :update]
before_action :ensure_dossier_can_be_updated, only: [:update_identite, :update_siret, :brouillon, :submit_brouillon, :submit_en_construction, :modifier, :modifier_legacy, :update, :champ]
before_action :ensure_dossier_can_be_filled, only: [:brouillon, :modifier, :submit_brouillon, :submit_en_construction, :update]
before_action :ensure_dossier_can_be_viewed, only: [:show]
before_action :forbid_invite_submission!, only: [:submit_brouillon]
@ -274,6 +274,20 @@ module Users
@dossier = current_user.dossiers.includes(:procedure).find(params[:id])
end
def champ
@dossier = dossier_with_champs(pj_template: false)
champ = @dossier.champs_public_all.find(params[:champ_id])
respond_to do |format|
format.turbo_stream do
@to_show, @to_hide = []
@to_update = [champ]
render :update, layout: false
end
end
end
def create_commentaire
@commentaire = CommentaireService.create(current_user, dossier, commentaire_params)
@ -465,7 +479,7 @@ module Users
end
def dossier_scope
if action_name == 'update'
if action_name == 'update' || action_name == 'champ'
Dossier.visible_by_user.or(Dossier.for_procedure_preview).or(Dossier.for_editing_fork)
elsif action_name == 'restore'
Dossier.hidden_by_user

View file

@ -223,6 +223,16 @@ class Champ < ApplicationRecord
false
end
def poll_external_data?
false
end
def fetch_external_data_pending?
# We don't have a good mechanism right now to know if the last fetch has errored. So, in order
# to ensure we don't poll to infinity, we stop after 5 minutes no matter what.
fetch_external_data? && poll_external_data? && external_id.present? && data.nil? && updated_at > 5.minutes.ago
end
def fetch_external_data
raise NotImplemented.new(:fetch_external_data)
end

View file

@ -322,6 +322,7 @@ Rails.application.routes.draw do
get 'modifier', to: 'dossiers#modifier'
post 'modifier', to: 'dossiers#submit_en_construction'
patch 'modifier', to: 'dossiers#modifier_legacy'
get 'champs/:champ_id', to: 'dossiers#champ', as: :champ
get 'merci'
get 'demande'
get 'messagerie'
@ -434,6 +435,7 @@ Rails.application.routes.draw do
get 'avis'
get 'avis_new'
get 'personnes-impliquees' => 'dossiers#personnes_impliquees'
get 'annotations/:annotation_id', to: 'dossiers#annotation', as: :annotation
patch 'follow'
patch 'unfollow'
patch 'archive'