66 organismeStore.organismeCourant.typeOrganisme === 'personne_physique'
77 "
88 ref =" personnePhysiqueRef"
9+ :init-organisme =" props.initOrganisme"
10+ :init-agrement =" props.initAgrement"
911 />
1012 <hr class =" fr-my-2w" />
1113 <AgrementPersonneMorale
1214 v-if ="
1315 organismeStore.organismeCourant.typeOrganisme === 'personne_morale'
1416 "
1517 ref =" personneMoraleRef"
18+ :init-organisme =" props.initOrganisme"
19+ :init-agrement =" props.initAgrement"
1620 />
1721 </div >
1822
1923 <hr class =" fr-mt-4w" />
2024
21- <AgrementCommentaire ref =" commentaireRef" class =" fr-my-2w" />
25+ <AgrementCommentaire
26+ ref =" commentaireRef"
27+ :init-commentaire =" props.initAgrement.commentaire"
28+ class =" fr-my-2w"
29+ />
30+
31+ <DsfrFileUpload
32+ v-model =" fileProcesVerbal"
33+ label =" Dernier procès verbal d'assemblée générale"
34+ :modifiable =" true"
35+ />
36+
37+ <hr class =" fr-mt-4w" />
2238
2339 <DsfrAlert
2440 v-if =" personnePhysiqueError"
4056 {{ personneMoraleError }}
4157 </DsfrAlert >
4258
43- <div v-if =" props.showButtons" >
44- <UtilsNavigationButtons
45- :show-buttons =" props.showButtons"
46- @next =" onNext"
47- />
48- </div >
59+ <DsfrButton @click.prevent =" onNext" >Suivant</DsfrButton >
4960 </div >
5061</template >
5162
5263<script setup>
5364import { ref } from " vue" ;
5465
5566const props = defineProps ({
67+ initAgrement: { type: Object , required: true },
68+ initOrganisme: { type: Object , required: true },
5669 modifiable: { type: Boolean , default: true },
57- showButtons: { type: Boolean , default: true },
5870});
59- const emit = defineEmits ([" next" ]);
71+ const emit = defineEmits ([" next" , " update " ]);
6072
6173const toaster = useToaster ();
62- const agrementStore = useAgrementStore ();
6374const organismeStore = useOrganismeStore ();
6475
6576const personnePhysiqueRef = ref ();
6677const personneMoraleRef = ref ();
6778const commentaireRef = ref ();
6879const personnePhysiqueError = ref (" " );
6980const personneMoraleError = ref (" " );
81+ const fileProcesVerbal = ref (null );
7082
71- // Validation et sauvegarde de l’étape
7283async function saveAgrement () {
84+ let commentaire;
85+ if (commentaireRef .value ? .getComment ) {
86+ commentaire = await commentaireRef .value .getComment ();
87+ }
88+
89+ const agrementValues = {
90+ ... (fileProcesVerbal .value ? { procesVerbal: fileProcesVerbal .value } : {}),
91+ ... (commentaire ? { commentaire } : {}),
92+ };
93+
94+ emit (" update" , agrementValues);
95+ }
96+
97+ async function validatePersonne (ref , saveMethod , errorRef , errorMsg ) {
98+ if (ref .value ? .[saveMethod]) {
99+ const data = await ref .value [saveMethod]();
100+ if (! data) {
101+ errorRef .value = errorMsg;
102+ return null ;
103+ } else {
104+ try {
105+ if (saveMethod === " savePersonneMorale" ) {
106+ await organismeStore .updatePersonneMorale ({
107+ ... organismeStore .organismeCourant .personneMorale ,
108+ ... data,
109+ });
110+ } else if (saveMethod === " savePersonnePhysique" ) {
111+ await organismeStore .updatePersonnePhysique ({
112+ ... organismeStore .organismeCourant .personnePhysique ,
113+ ... data,
114+ });
115+ }
116+ return data;
117+ } catch (err) {
118+ errorRef .value =
119+ " Erreur lors de la sauvegarde : " +
120+ (err? .message || " Erreur inconnue." );
121+ return null ;
122+ }
123+ }
124+ }
125+ return null ;
126+ }
127+
128+ async function saveCoordonneesStep () {
73129 personnePhysiqueError .value = " " ;
74130 personneMoraleError .value = " " ;
75131 let valid = true ;
76132
77- if (personnePhysiqueRef .value ? .validateAndSave ) {
78- const ok = await personnePhysiqueRef .value .validateAndSave ();
79- if (! ok) {
80- personnePhysiqueError .value =
81- " Veuillez corriger les erreurs dans le formulaire de la personne physique." ;
82- valid = false ;
83- }
133+ const personnePhysiqueData = await validatePersonne (
134+ personnePhysiqueRef,
135+ " savePersonnePhysique" ,
136+ personnePhysiqueError,
137+ " Veuillez corriger les erreurs dans le formulaire de la personne physique." ,
138+ );
139+ if (personnePhysiqueRef .value && ! personnePhysiqueData) {
140+ valid = false ;
141+ return ;
84142 }
85143
86- if (personneMoraleRef .value ? .validateAndSave ) {
87- const ok = await personneMoraleRef .value .validateAndSave ();
88- if (! ok) {
89- personneMoraleError .value =
90- " Veuillez corriger les erreurs dans le formulaire de la personne morale." ;
91- valid = false ;
92- }
144+ const personneMoraleData = await validatePersonne (
145+ personneMoraleRef,
146+ " savePersonneMorale" ,
147+ personneMoraleError,
148+ " Veuillez corriger les erreurs dans le formulaire de la personne morale." ,
149+ );
150+ if (personneMoraleRef .value && ! personneMoraleData) {
151+ valid = false ;
152+ return ;
93153 }
94154
95- if (commentaireRef .value ? .getComment ()) {
96- const commentaire = commentaireRef .value .getComment ();
97- await agrementStore .postAgrement ({
98- agrement: { commentaire: commentaire || " " },
99- organismeId: organismeStore .organismeCourant ? .organismeId ,
100- });
101- }
155+ saveAgrement ();
102156
103157 if (! valid) {
104158 return toaster .error ({
@@ -108,16 +162,12 @@ async function saveAgrement() {
108162 });
109163 }
110164
111- toaster .success ({
112- titleTag: " h2" ,
113- description: " Les informations ont été enregistrées avec succès." ,
114- });
115165 emit (" next" );
116166}
117167
118168function onNext () {
119- saveAgrement ();
169+ saveCoordonneesStep ();
120170}
121171
122- defineExpose ({ saveAgrement });
172+ defineExpose ({ saveCoordonneesStep });
123173< / script>
0 commit comments