-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscout_reg.js
More file actions
216 lines (185 loc) · 7.71 KB
/
scout_reg.js
File metadata and controls
216 lines (185 loc) · 7.71 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
class SimScoutRegister extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
connectedCallback() {
this.supabaseUrl = this.getAttribute('supabase-url');
this.supabaseKey = this.getAttribute('supabase-key');
this.tenantId = this.getAttribute('tenant-id');
this.targetUrl = this.getAttribute('target-url');
if (!this.supabaseUrl || !this.supabaseKey || !this.tenantId || !this.targetUrl) {
console.error('SIM-SCOUT-REGISTER: Missing required attributes.');
this.shadowRoot.innerHTML = '<p style="color:red; font-family:sans-serif;">Error: Missing configuration.</p>';
return;
}
this.ensureSupabaseLoaded();
this.render();
}
async ensureSupabaseLoaded() {
if (window.supabase) return; // Already loaded
// Check if script tag exists
if (!document.querySelector('script[src*="supabase-js"]')) {
const script = document.createElement('script');
script.type = 'module';
// We use a small inline module shim to expose createClient globally if needed,
// but since we are inside a module environment usually, we might need a different approach.
// Simplified: We load from esm.sh and attach to window for this specific use case if possible
// or just import it dynamically in the logic.
}
}
render() {
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
--primary-color: #00b4d8;
--primary-hover: #0096b4;
--bg-color: #ffffff;
--text-color: #333333;
--input-bg: #f9f9f9;
--input-border: #e0e0e0;
--radius: 12px;
}
.container {
background: var(--bg-color);
border: 1px solid var(--input-border);
border-radius: var(--radius);
padding: 2rem;
max-width: 400px;
margin: 0 auto;
box-shadow: 0 4px 20px rgba(0,0,0,0.05);
}
h2 {
margin: 0 0 1rem 0;
font-size: 1.5rem;
color: var(--text-color);
text-align: center;
font-weight: 700;
}
form {
display: flex;
flex-direction: column;
gap: 1rem;
}
.form-group {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
label {
font-size: 0.9rem;
font-weight: 600;
color: #666;
}
input {
padding: 0.8rem 1rem;
border: 1px solid var(--input-border);
border-radius: 8px;
font-size: 1rem;
background: var(--input-bg);
transition: all 0.2s ease;
}
input:focus {
outline: none;
border-color: var(--primary-color);
box-shadow: 0 0 0 3px rgba(0, 180, 216, 0.1);
}
button {
margin-top: 0.5rem;
background: var(--primary-color);
color: white;
border: none;
padding: 1rem;
border-radius: 8px;
font-size: 1rem;
font-weight: 700;
cursor: pointer;
transition: background 0.2s ease, transform 0.1s ease;
}
button:hover {
background: var(--primary-hover);
}
button:active {
transform: translateY(1px);
}
button:disabled {
opacity: 0.7;
cursor: not-allowed;
}
.error {
color: #e63946;
font-size: 0.85rem;
text-align: center;
margin-top: 0.5rem;
display: none;
}
</style>
<div class="container">
<h2>Werde Scout & trainiere gratis</h2>
<form id="scout-form">
<div class="form-group">
<label for="first_name">Vorname</label>
<input type="text" id="first_name" name="first_name" placeholder="Max" required>
</div>
<div class="form-group">
<label for="last_name">Nachname</label>
<input type="text" id="last_name" name="last_name" placeholder="Mustermann" required>
</div>
<div class="form-group">
<label for="email">Deine E-Mail</label>
<input type="email" id="email" name="email" placeholder="max@beispiel.de" required>
</div>
<button type="submit" id="submit-btn">Jetzt anmelden</button>
<div class="error" id="error-msg"></div>
</form>
</div>
`;
this.shadowRoot.getElementById('scout-form').addEventListener('submit', this.handleSubmit.bind(this));
}
async handleSubmit(e) {
e.preventDefault();
const btn = this.shadowRoot.getElementById('submit-btn');
const errorMsg = this.shadowRoot.getElementById('error-msg');
const firstName = this.shadowRoot.getElementById('first_name').value;
const lastName = this.shadowRoot.getElementById('last_name').value;
const email = this.shadowRoot.getElementById('email').value;
btn.disabled = true;
btn.textContent = 'Lädt...';
errorMsg.style.display = 'none';
try {
// Dynamically import Supabase here to avoid global namespace pollution / requirement
const { createClient } = await import('https://esm.sh/@supabase/supabase-js@2');
const supabase = createClient(this.supabaseUrl, this.supabaseKey);
const { data, error } = await supabase
.from('scouts')
.insert({
tenant_id: this.tenantId,
tenant_id: this.tenantId,
first_name: firstName,
last_name: lastName,
email: email,
stats: {}
})
.select()
.single();
if (error) throw error;
if (data) {
// Success! Redirect.
const redirectUrl = new URL(this.targetUrl);
redirectUrl.searchParams.set('id', data.id);
window.location.href = redirectUrl.toString();
} else {
throw new Error('Keine ID zurückerhalten.');
}
} catch (err) {
console.error(err);
errorMsg.textContent = 'Fehler: ' + (err.message || 'Verbindung fehlgeschlagen');
errorMsg.style.display = 'block';
btn.disabled = false;
btn.textContent = 'Jetzt anmelden';
}
}
}
customElements.define('sim-scout-register', SimScoutRegister);