|
1 | 1 | // This is a manifest file that'll be compiled into application.js, which will include all the files |
2 | 2 | // listed below. |
3 | 3 | // |
4 | | -// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, |
5 | | -// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. |
6 | 4 | // |
7 | 5 | // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the |
8 | 6 | // compiled file. |
|
19 | 17 | // Required by Blacklight |
20 | 18 | //= require blacklight/blacklight |
21 | 19 | //= require babel/polyfill |
| 20 | +//= require csrf_form_helper |
22 | 21 |
|
23 | | -// Wait for the modal to open |
24 | 22 | document.addEventListener('show.blacklight.blacklight-modal', function () { |
25 | | - // Attach a vanilla submit handler to modal forms that submits via fetch |
26 | | - // and hides the Blacklight modal on a successful response. We mark forms |
27 | | - // that we've attached to so we don't double-bind when the modal reopens. |
| 23 | + console.log('Modal is going to be shown'); |
28 | 24 | document.querySelectorAll('.modal_form').forEach(function (form) { |
29 | | - if (form.dataset.vanillaHandlerAdded) return; |
30 | | - form.dataset.vanillaHandlerAdded = 'true'; |
| 25 | + form.addEventListener( |
| 26 | + 'submit', |
| 27 | + function (e) { |
| 28 | + e.preventDefault(); |
31 | 29 |
|
32 | | - form.addEventListener('submit', function (e) { |
33 | | - e.preventDefault(); |
| 30 | + var action = form.getAttribute('action') || window.location.href; |
| 31 | + var method = (form.getAttribute('method') || 'GET').toUpperCase(); |
| 32 | + var fetchOptions = { method: method }; |
34 | 33 |
|
35 | | - var action = form.getAttribute('action') || window.location.href; |
36 | | - var method = (form.getAttribute('method') || 'GET').toUpperCase(); |
37 | | - var fetchOptions = { method: method, credentials: 'same-origin' }; |
| 34 | + var formData = new FormData(form); |
| 35 | + console.log( |
| 36 | + `method: ${method}, action: ${action}, formData:, ${formData}` |
| 37 | + ); |
| 38 | + console.log(formData); |
| 39 | + if (method !== 'GET') { |
| 40 | + fetchOptions.body = formData; |
| 41 | + } |
38 | 42 |
|
39 | | - var formData = new FormData(form); |
40 | | - |
41 | | - if (method === 'GET') { |
42 | | - // Append form data to query string for GET |
43 | | - var params = new URLSearchParams(formData); |
44 | | - action += (action.indexOf('?') === -1 ? '?' : '&') + params.toString(); |
45 | | - } else { |
46 | | - fetchOptions.body = formData; |
47 | | - } |
48 | | - |
49 | | - fetch(action, fetchOptions) |
50 | | - .then(function (response) { |
51 | | - if (response.ok) { |
52 | | - Blacklight.Modal.hide(); |
53 | | - } else { |
| 43 | + // otherwise we get 422 error due to missing CSRF token |
| 44 | + Orangelight.CsrfFormHelper.fetch(action, fetchOptions) |
| 45 | + .then(function (response) { |
54 | 46 | return response.text().then(function (body) { |
55 | | - console.error( |
56 | | - 'Modal form submission failed', |
57 | | - response.status, |
58 | | - body |
59 | | - ); |
| 47 | + if (response.ok) { |
| 48 | + var modalEl = |
| 49 | + document.querySelector('.modal') || |
| 50 | + document.querySelector('.blacklight-modal') || |
| 51 | + document.getElementById('blacklight-modal'); |
| 52 | + if (modalEl) { |
| 53 | + modalEl.innerHTML = body; |
| 54 | + } else { |
| 55 | + var wrapper = document.createElement('div'); |
| 56 | + wrapper.innerHTML = body; |
| 57 | + document.body.appendChild(wrapper); |
| 58 | + } |
| 59 | + } else { |
| 60 | + console.error( |
| 61 | + 'Modal form submission failed', |
| 62 | + response.status, |
| 63 | + body |
| 64 | + ); |
| 65 | + } |
60 | 66 | }); |
61 | | - } |
62 | | - }) |
63 | | - .catch(function (err) { |
64 | | - console.error('Modal form submission error', err); |
65 | | - }); |
66 | | - }); |
| 67 | + }) |
| 68 | + .catch(function (err) { |
| 69 | + console.error('Modal form submission error', err); |
| 70 | + }); |
| 71 | + }, |
| 72 | + { once: true } |
| 73 | + ); |
67 | 74 | }); |
68 | 75 | }); |
0 commit comments