@@ -75,11 +75,14 @@ function displayMissionModal($modal, id) {
7575 return ;
7676 }
7777
78+ const url = window . location . pathname . indexOf ( 'organizations' ) >= 0 ? `/organizations/missions/${ id } /modal` : `/user/availability/missions/${ id } /modal` ;
79+ displayAjaxModal ( $modal , url ) ;
80+ }
81+
82+ function displayAjaxModal ( $modal , url ) {
7883 const $loading = $modal . find ( '.loading' ) . show ( ) ;
7984 const $content = $modal . find ( '.content' ) . html ( '' ) ;
8085
81- const url = window . location . pathname . indexOf ( 'organizations' ) >= 0 ? `/organizations/missions/${ id } /modal` : `/user/availability/missions/${ id } /modal` ;
82-
8386 $ . ajax ( {
8487 method : 'GET' ,
8588 url,
@@ -96,7 +99,33 @@ function displayMissionModal($modal, id) {
9699 $modal . modal ( 'show' ) ;
97100}
98101
99- export function fetchMissions ( url ) {
102+ function addUserToMission ( url ) {
103+ $ ( '.mission-choose' ) . prop ( 'disabled' , true ) ;
104+
105+ $ . ajax ( {
106+ method : 'POST' ,
107+ dataType : 'json' ,
108+ url,
109+ success : function ( ) {
110+ $ ( '#modal-add-mission' ) . modal ( 'hide' ) ;
111+ fetchMissions ( ) ;
112+ } ,
113+ error : function ( ) {
114+ window . alert ( 'Une erreur est survenue pendant la requête' ) ;
115+ $ ( '.mission-choose' ) . prop ( 'disabled' , false ) ;
116+ } ,
117+ } ) ;
118+ }
119+
120+ export function fetchMissions ( ) {
121+ let url ;
122+
123+ if ( $ ( '.planning' ) . length ) {
124+ url = '/organizations/missions/find' + window . location . search ;
125+ } else {
126+ url = window . location . pathname + '/missions' ;
127+ }
128+
100129 $ . ajax ( {
101130 method : 'GET' ,
102131 dataType : 'json' ,
@@ -122,4 +151,34 @@ export function initMissionsEvents() {
122151 $modal . find ( '.loading' ) . show ( ) ;
123152 $modal . find ( '.content' ) . html ( '' ) ;
124153 } ) ;
154+
155+ $ ( '#modal-add-mission' )
156+ . on ( 'show.bs.modal' , function ( event ) {
157+ const $modal = $ ( this ) ;
158+ const $link = $ ( event . relatedTarget ) ;
159+ const url = $link . data ( 'href' ) ;
160+
161+ displayAjaxModal ( $modal , url ) ;
162+ } )
163+ . on ( 'hidden.bs.modal' , function ( ) {
164+ const $modal = $ ( this ) ;
165+ $modal . find ( '.loading' ) . show ( ) ;
166+ $modal . find ( '.content' ) . html ( '' ) ;
167+ } ) ;
168+
169+ $ ( document ) . on ( 'click' , '.mission-choose' , function ( ) {
170+ addUserToMission ( $ ( this ) . data ( 'href' ) ) ;
171+ } ) ;
172+
173+ // Allow modals stacking
174+ $ ( document ) . on ( 'show.bs.modal' , '.modal' , function ( ) {
175+ const zIndex = 1040 + 10 * $ ( '.modal:visible' ) . length ;
176+ $ ( this ) . css ( 'z-index' , zIndex ) ;
177+ setTimeout ( function ( ) {
178+ $ ( '.modal-backdrop' )
179+ . not ( '.modal-stack' )
180+ . css ( 'z-index' , zIndex - 1 )
181+ . addClass ( 'modal-stack' ) ;
182+ } ) ;
183+ } ) ;
125184}
0 commit comments