forked from authgear/authgear-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.ts
178 lines (154 loc) · 4.84 KB
/
form.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import Turbolinks from "turbolinks";
import axios, { Method } from "axios";
import {
disableAllButtons,
hideProgressBar,
showProgressBar,
progressEventHandler,
} from "./loading";
import { handleAxiosError } from "./error";
import { Controller } from "@hotwired/stimulus";
export class XHRSubmitFormController extends Controller {
revertDisabledButtons: { (): void } | null = null;
forms: HTMLFormElement[] = [];
// Revert disabled buttons before turbolinks cache the page
// To avoid flickering in the UI
beforeCache = () => {
if (this.revertDisabledButtons) {
this.revertDisabledButtons();
}
};
onSubmit = (e: Event) => {
this.submitForm(e);
};
async submitForm(e: Event) {
const form = e.currentTarget as HTMLFormElement;
if (form.querySelector('[data-form-xhr="false"]')) {
return;
}
if (e.defaultPrevented) {
return;
}
e.preventDefault();
// Do not stop propagation so that GTM can recognize the event as Form Submission trigger.
// e.stopPropagation();
const formData = new FormData(form);
const params = new URLSearchParams();
formData.forEach((value, name) => {
params.set(name, value as string);
});
// FormData does not include any submit button's data:
// include them manually, since we have at most one submit button per form.
const submitButtons = form.querySelectorAll('button[type="submit"]');
for (let i = 0; i < submitButtons.length; i++) {
const button = submitButtons[i] as HTMLButtonElement;
params.set(button.name, button.value);
}
if (form.id) {
const el = document.querySelector(
`button[type="submit"][form="${form.id}"]`
);
if (el) {
const button = el as HTMLButtonElement;
params.set(button.name, button.value);
}
}
this.revertDisabledButtons = disableAllButtons();
showProgressBar();
try {
const resp = await axios(form.action, {
method: form.method as Method,
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"X-Authgear-XHR": "true",
},
data: params,
onUploadProgress: progressEventHandler,
onDownloadProgress: progressEventHandler,
});
const { redirect_uri, action } = resp.data;
Turbolinks.clearCache();
switch (action) {
case "redirect":
// Perform full redirect.
window.location = redirect_uri;
break;
case "replace":
case "advance":
Turbolinks.visit(redirect_uri, { action });
break;
}
} catch (e: unknown) {
handleAxiosError(e);
// revert is only called for error branch because
// The success branch also loads a new page.
// Keeping the buttons in disabled state reduce flickering in the UI.
if (this.revertDisabledButtons) {
this.revertDisabledButtons();
this.revertDisabledButtons = null;
}
} finally {
hideProgressBar();
}
}
connect() {
const elems = document.querySelectorAll("form");
for (let i = 0; i < elems.length; i++) {
if (elems[i].querySelector('[data-form-xhr="false"]')) {
continue;
}
this.forms.push(elems[i] as HTMLFormElement);
}
for (const form of this.forms) {
form.addEventListener("submit", this.onSubmit);
}
document.addEventListener("turbolinks:before-cache", this.beforeCache);
}
disconnect() {
for (const form of this.forms) {
form.removeEventListener("submit", this.onSubmit);
}
document.removeEventListener("turbolinks:before-cache", this.beforeCache);
}
}
export class RestoreFormController extends Controller {
static targets = ["metaTag"];
declare metaTagTarget: HTMLMetaElement;
connect() {
const metaTag = this.metaTagTarget;
const content = metaTag.content;
if (content === "") {
return;
}
// Clear the content to avoid restoring twice.
metaTag.content = "";
const formDataJSON = JSON.parse(content);
// Find the form.
let form: HTMLFormElement | null = null;
const xAction = formDataJSON["x_action"];
const elementsWithXAction = document.querySelectorAll(`[name="x_action"]`);
for (let i = 0; i < elementsWithXAction.length; i++) {
const elem = elementsWithXAction[i];
if (elem instanceof HTMLButtonElement && elem.value === xAction) {
form = elem.form;
break;
}
}
if (form == null) {
return;
}
for (let i = 0; i < form.elements.length; i++) {
const elem = form.elements[i];
if (
elem instanceof HTMLInputElement ||
elem instanceof HTMLSelectElement ||
elem instanceof HTMLTextAreaElement
) {
const value = formDataJSON[elem.name];
if (value != null) {
elem.value = value;
}
}
}
}
}