/
form-wizard-sign-in-step.js
190 lines (168 loc) · 6.84 KB
/
form-wizard-sign-in-step.js
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
179
180
181
182
183
184
185
186
187
188
189
190
import { BaseFormStep } from "sumo/js/form-wizard";
import signInStepStylesURL from "../scss/form-wizard-sign-in-step.styles.scss";
export class SignInStep extends BaseFormStep {
#formEl = null;
#emailEl = null;
#emailErrorEl = null;
get template() {
return `
<template>
<div id="sign-in-step-root">
<h3 class="for-sign-up">${gettext("Sign up for an account")}</h3>
<h3 class="for-sign-in">${gettext("Sign in to your account")}</h3>
<p>
${gettext("You’ll be able to sign in to this account on another device to sync your data.")}
</p>
<form method="get" novalidate>
<input name="service" value="" type="hidden"/>
<input name="action" value="" type="hidden"/>
<input name="context" value="" type="hidden"/>
<input name="flow_id" value="" type="hidden"/>
<input name="flow_begin_time" value="" type="hidden"/>
<input name="utm_source" value="" type="hidden"/>
<input name="utm_medium" value="" type="hidden"/>
<input name="utm_campaign" value="" type="hidden"/>
<input name="entrypoint" value="" type="hidden"/>
<input name="entrypoint_experiment" value="" type="hidden"/>
<input name="entrypoint_variation" value="" type="hidden"/>
<input name="redirect_immediately" value="" type="hidden"/>
<input name="redirect_to" value="" type="hidden"/>
<label for="email">${gettext("Enter your email")}</label>
<div class="tooltip-container">
<aside id="email-error" class="tooltip">${gettext("Valid email required")}</aside>
<input id="email" name="email" type="email" required="true" placeholder="${gettext("user@example.com")}" pending-autofocus="" />
</div>
<button id="continue" class="mzp-c-button mzp-t-product" type="submit" data-event-name="dmw_click">${gettext("Continue")}</button>
</form>
<p class="for-sign-up form-footer">
${gettext("Already have an account?")} <a class='alternative-link' href='#' data-event-name="dmw_click" data-event-parameters='{"dmw_click_target": "sign-in-link"}'>${gettext("Sign in")}</a>
</p>
<p class="for-sign-in form-footer">
${gettext("Don’t have an account?")} <a class='alternative-link' href='#' data-event-name="dmw_click" data-event-parameters='{"dmw_click_target": "signup-link"}'>${gettext("Sign up")}</a>
</p>
</div>
</template>
`;
}
get styles() {
let linkEl = document.createElement("link");
linkEl.rel = "stylesheet";
linkEl.href = signInStepStylesURL;
return linkEl;
}
connectedCallback() {
super.connectedCallback();
this.#formEl = this.shadowRoot.querySelector("form");
this.#emailEl = this.shadowRoot.getElementById("email");
this.#emailErrorEl = this.shadowRoot.getElementById("email-error");
this.#formEl.addEventListener("submit", this);
this.#emailEl.addEventListener("blur", this);
this.#emailEl.addEventListener("input", this);
}
disconnectedCallback() {
super.disconnectedCallback();
this.#formEl.removeEventListener("submit", this);
this.#emailEl.removeEventListener("blur", this);
this.#emailEl.removeEventListener("input", this);
}
deactivate() {
if (this.shadowRoot.activeElement == this.#emailEl) {
this.#emailEl.blur();
this.#emailErrorEl.classList.remove("visible");
}
}
handleEvent(event) {
switch (event.type) {
case "blur": {
if (!this.#emailEl.validity.valid) {
this.#emailErrorEl.classList.add("visible");
}
break;
}
case "input": {
if (this.#emailEl.value?.trim()) {
this.#emailErrorEl.classList.remove("visible");
}
break;
}
case "submit": {
if (!this.#emailEl.validity.valid) {
this.#emailErrorEl.classList.add("visible");
event.preventDefault();
} else {
// The email is valid and we're about to redirect to FxA login.
// Let's quickly stash the email address in session storage so
// that we can prefill the email field in Step 3 for getting
// the reminder email.
try {
sessionStorage.setItem("switching-devices-email", this.#emailEl.value);
} catch (e) {
// We wrap this in a try/catch because session storage methods might
// throw if the user has disabled cookies or other types of site
// data storage, and we want this to be non-fatal.
}
}
break;
}
}
}
render() {
let root = this.shadowRoot.querySelector("#sign-in-step-root");
let continueBtn = this.shadowRoot.querySelector("#continue");
if (this.state.email) {
// If the user somehow has managed to type something into the email
// field before we were able to get the email address, let's not
// overwrite what the user had typed in.
if (!this.#emailEl.value) {
this.#emailEl.value = this.state.email;
}
root.setAttribute("mode", "sign-in");
continueBtn.dataset.eventParameters = '{"dmw_click_target": "sign-in-button"}';
} else {
root.setAttribute("mode", "sign-up");
continueBtn.dataset.eventParameters = '{"dmw_click_target": "signup-button"}';
}
let form = this.shadowRoot.querySelector("form");
form.action = this.state.fxaRoot;
const STATE_FIELDS = [
"service",
"action",
"context",
"flow_id",
"flow_begin_time",
"utm_source",
"utm_medium",
"utm_campaign",
"entrypoint",
"entrypoint_experiment",
"entrypoint_variation",
"redirect_to",
"redirect_immediately",
];
for (let fieldName of STATE_FIELDS) {
let fieldEl = this.shadowRoot.querySelector(`input[name=${fieldName}]`);
if (this.state[fieldName]) {
fieldEl.disabled = false;
fieldEl.value = this.state[fieldName];
} else {
fieldEl.disabled = true;
}
}
// If we've reached the point where we've got hasUpdatedFxAState set true,
// this means that we've confirmed that we're running on a non-disqualified
// version of Firefox Desktop. If focus hasn't moved anywhere else in the
// meantime, we'll move focus to the email field if it's never been moved
// there before.
if (this.state.hasUpdatedFxAState && this.#emailEl.hasAttribute("pending-autofocus")) {
this.#emailEl.removeAttribute("pending-autofocus");
if (document.activeElement == document.body) {
this.#emailEl.focus();
}
}
let alternativeLinks = this.shadowRoot.querySelectorAll(".alternative-link");
for (let link of alternativeLinks) {
link.href = this.state.linkHref;
}
}
}
customElements.define("sign-in-step", SignInStep);