-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Change form actions to fetch for submit review box #25219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 28 commits
f774943
3c56833
984a30b
2b71734
7d3683b
2318bb3
183dc8e
ed54590
8f57fc1
0dc11e1
4602642
7fa9e1e
9db8855
acdd3cd
f029cfb
8f382f2
cf540cd
bfe1c6b
1a8472f
c3d5bfb
2d7d5d0
f4da606
b37a7db
74e330d
a5c5145
c45a1e1
3b271d7
6023a45
b8f64ca
722d0ed
9f428a3
f43cdc6
713feda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -60,6 +60,57 @@ export function initGlobalButtonClickOnEnter() { | |||||||||||||
}); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
async function fetchFormAction(e) { | ||||||||||||||
if (!e.target.classList.contains('form-fetch-action')) return; | ||||||||||||||
|
||||||||||||||
e.preventDefault(); | ||||||||||||||
const formEl = e.target; | ||||||||||||||
if (formEl.classList.contains('loading')) return; | ||||||||||||||
|
||||||||||||||
// TODO: fine tune the UI feedback | ||||||||||||||
formEl.classList.add('loading'); | ||||||||||||||
e.submitter?.classList.add('loading'); | ||||||||||||||
|
||||||||||||||
const formMethod = formEl.getAttribute('method') || 'get'; | ||||||||||||||
const formActionUrl = formEl.getAttribute('action'); | ||||||||||||||
const formData = new FormData(formEl); | ||||||||||||||
const [submitterName, submitterValue] = [e.submitter?.getAttribute('name'), e.submitter?.getAttribute('value')]; | ||||||||||||||
if (submitterName) { | ||||||||||||||
formData.append(submitterName, submitterValue); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
const req = {method: formMethod.toUpperCase(), headers: {'X-Csrf-Token': csrfToken}}; | ||||||||||||||
if (formMethod === 'GET') { | ||||||||||||||
HesterG marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
// TODO: append to the action URL | ||||||||||||||
} else { | ||||||||||||||
req.body = formData; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
const onError = () => { | ||||||||||||||
// TODO: show error to end users | ||||||||||||||
formEl.classList.remove('loading'); | ||||||||||||||
e.submitter?.classList.remove('loading'); | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
try { | ||||||||||||||
const resp = await fetch(formActionUrl, req); | ||||||||||||||
if (resp.status === 200) { | ||||||||||||||
const {redirect} = await resp.json(); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it is only a nit, I do not want to use If it is a must and most people prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My order of preference is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My order of preference is And all Golang backend code also uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess at some point, I will at some point make a fetch wrapper anyways that returns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am curious is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah let's do it then. Consistency over personal preference. |
||||||||||||||
if (redirect) { | ||||||||||||||
window.location.href = redirect; | ||||||||||||||
} else { | ||||||||||||||
// TODO: remove areYouSure form check | ||||||||||||||
window.reload(); | ||||||||||||||
} | ||||||||||||||
} else { | ||||||||||||||
// TODO: show error to end users | ||||||||||||||
onError(); | ||||||||||||||
} | ||||||||||||||
} catch { | ||||||||||||||
onError(); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
export function initGlobalCommon() { | ||||||||||||||
// Semantic UI modules. | ||||||||||||||
const $uiDropdowns = $('.ui.dropdown'); | ||||||||||||||
|
@@ -114,6 +165,8 @@ export function initGlobalCommon() { | |||||||||||||
if (btn.classList.contains('loading')) return e.preventDefault(); | ||||||||||||||
btn.classList.add('loading'); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
document.addEventListener('submit', fetchFormAction); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
export function initGlobalDropzone() { | ||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.