Ronak0 commited on
Commit
9e81be2
·
verified ·
1 Parent(s): 8a52f41

Upload script.js

Browse files
Files changed (1) hide show
  1. script.js +230 -0
script.js ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* ======================= SHORTCUT ======================= */
2
+ const el = (id) => document.getElementById(id);
3
+
4
+ /* ======================= FIREBASE CONFIG ======================= */
5
+ const firebaseConfig = {
6
+ apiKey: "AIzaSyDVwJCmDIEV4cIPDEEzxCLOnDF1f3m3YbA",
7
+ authDomain: "success-predictor-fire.firebaseapp.com",
8
+ projectId: "success-predictor-fire",
9
+ };
10
+ firebase.initializeApp(firebaseConfig);
11
+
12
+ const auth = firebase.auth();
13
+
14
+ /* ======================= LOGIN FUNCTION ======================= */
15
+ async function handleGoogleLogin() {
16
+ const provider = new firebase.auth.GoogleAuthProvider();
17
+
18
+ try {
19
+ const result = await auth.signInWithPopup(provider);
20
+ const idToken = await result.user.getIdToken();
21
+
22
+ await fetch("/api/login", {
23
+ method: "POST",
24
+ headers: { "Content-Type": "application/json" },
25
+ credentials: "include", // 🔥 MUST
26
+ body: JSON.stringify({ token: idToken }) // ✅ FIXED
27
+ });
28
+
29
+ updateAuthUI();
30
+ } catch (err) {
31
+ console.error("Login error:", err);
32
+ alert("Login failed");
33
+ }
34
+ }
35
+
36
+ /* ======================= LOGOUT FUNCTION ======================= */
37
+ async function handleLogout() {
38
+ await fetch("/api/logout", {
39
+ method: "POST",
40
+ credentials: "include"
41
+ });
42
+ await auth.signOut();
43
+ updateAuthUI();
44
+ }
45
+
46
+
47
+ /* ======================= UPDATE LOGIN UI ======================= */
48
+ async function updateAuthUI() {
49
+ const openGoogle = el("openGoogle");
50
+ const userSection = el("userSection");
51
+ const userPhoto = el("userPhoto");
52
+ const userDropdown = el("userDropdown");
53
+ const userName = el("userName");
54
+ const userEmail = el("userEmail");
55
+
56
+ try {
57
+ const res = await fetch("/api/me", {
58
+ credentials: "include" // 🔥 MOST IMPORTANT
59
+ });
60
+
61
+ const j = await res.json();
62
+
63
+ if (j.authenticated) {
64
+ // (A) HIDE LOGIN BUTTON
65
+ openGoogle.classList.add("hidden");
66
+
67
+ // (B) SHOW PROFILE SECTION
68
+ userSection.classList.remove("hidden");
69
+ userName.textContent = j.name || "User";
70
+ userEmail.textContent = j.email || "";
71
+ userPhoto.src = j.picture || "https://i.imgur.com/4ZQZ4ZQ.png";
72
+
73
+ el("openDeep").disabled = false;
74
+ } else {
75
+ // (A) SHOW LOGIN
76
+ openGoogle.classList.remove("hidden");
77
+
78
+ // (B) HIDE PROFILE
79
+ userSection.classList.add("hidden");
80
+
81
+ openGoogle.onclick = handleGoogleLogin;
82
+ el("openDeep").disabled = true;
83
+ }
84
+ } catch (e) {
85
+ console.warn("Auth check failed:", e);
86
+ }
87
+ }
88
+ updateAuthUI();
89
+
90
+ /* ======================= DROPDOWN TOGGLE ======================= */
91
+ el("userSection").addEventListener("click", () => {
92
+ el("userDropdown").classList.toggle("hidden");
93
+ });
94
+
95
+ document.addEventListener("click", (e) => {
96
+ if (!el("userSection").contains(e.target)) {
97
+ el("userDropdown").classList.add("hidden");
98
+ }
99
+ });
100
+
101
+ /* ======================= SMOOTH SCROLL TO CARDS ======================= */
102
+ document.querySelectorAll("#openFast, #openDeep").forEach((btn) => {
103
+ btn.addEventListener("click", () => {
104
+ document
105
+ .getElementById("analysisSection")
106
+ .scrollIntoView({ behavior: "smooth" });
107
+ });
108
+ });
109
+
110
+ /* ======================= OPEN MODAL ======================= */
111
+ function openModal(mode) {
112
+ el("inputModal").classList.remove("hidden");
113
+ el("modalTitle").textContent =
114
+ mode === "deep" ? "Start Deep Analysis" : "Start Fast Analysis";
115
+ el("analyzeBtn").dataset.mode = mode;
116
+ el("formStatus").textContent = "";
117
+ }
118
+
119
+ /* Fast Analysis Card Button */
120
+ document.querySelectorAll(".start").forEach((b) => {
121
+ b.addEventListener("click", () => {
122
+ openModal(b.dataset.mode);
123
+ });
124
+ });
125
+
126
+ /* Deep Button Auth Guard */
127
+ el("openDeep").addEventListener("click", async () => {
128
+ const me = await fetch("/api/me", { credentials: "include" })
129
+ .then(r => r.json());
130
+
131
+ if (!me.authenticated) {
132
+ alert("Please login first.");
133
+ return;
134
+ }
135
+ });
136
+
137
+
138
+ /* Close Modal */
139
+ el("closeModal").onclick = () => el("inputModal").classList.add("hidden");
140
+ el("cancelBtn").onclick = () => el("inputModal").classList.add("hidden");
141
+
142
+ /* ======================= ANALYZE ======================= */
143
+ el("analyzeBtn").addEventListener("click", async function () {
144
+ const mode = this.dataset.mode;
145
+
146
+ const name = el("name").value.trim();
147
+ const pitch = el("pitch").value.trim();
148
+ const description = el("description").value.trim();
149
+ const industry = el("industry").value.trim();
150
+
151
+ if (!name && !pitch && !description) {
152
+ el("formStatus").textContent = "Please fill at least one field.";
153
+ return;
154
+ }
155
+
156
+ el("formStatus").textContent = "Running AI analysis...";
157
+
158
+ try {
159
+ const resp = await fetch("/api/analyze", {
160
+ method: "POST",
161
+ headers: { "Content-Type": "application/json" },
162
+ body: JSON.stringify({
163
+ name,
164
+ pitch,
165
+ description,
166
+ industry,
167
+ mode,
168
+ }),
169
+ });
170
+
171
+ const j = await resp.json();
172
+
173
+ if (!resp.ok) {
174
+ el("formStatus").textContent = j.error || "Error!";
175
+ return;
176
+ }
177
+
178
+ const id = j.id;
179
+
180
+ el("formStatus").innerHTML = `
181
+ Analysis Ready 🎉 <br>
182
+ <a href="/api/doc/${id}" target="_blank">View JSON</a> •
183
+ <a href="/api/pdf/${id}" target="_blank">Download TXT</a>
184
+ `;
185
+
186
+ fetchHistory();
187
+ } catch (err) {
188
+ el("formStatus").textContent = "Network error.";
189
+ }
190
+ });
191
+
192
+ /* ======================= HISTORY LOADER ======================= */
193
+ async function fetchHistory() {
194
+ const historyList = el("historyList");
195
+
196
+ try {
197
+ historyList.innerHTML = "Loading...";
198
+
199
+ const r = await fetch("/api/history");
200
+ const j = await r.json();
201
+
202
+ historyList.innerHTML = "";
203
+
204
+ if (!j.items || j.items.length === 0) {
205
+ historyList.innerHTML = '<div class="muted">No recent analyses</div>';
206
+ return;
207
+ }
208
+
209
+ j.items.forEach((it) => {
210
+ const div = document.createElement("div");
211
+ div.className = "history-item";
212
+
213
+ div.innerHTML = `
214
+ <strong>${it.name || it.pitch || "Unnamed"}</strong>
215
+ <span class="muted">(${it.industry || "General"})</span>
216
+ <div class="small muted">
217
+ Mode: ${it.mode} •
218
+ <a href="/api/doc/${it.id}" target="_blank">json</a> •
219
+ <a href="/api/pdf/${it.id}" target="_blank">txt</a>
220
+ </div>
221
+ `;
222
+
223
+ historyList.appendChild(div);
224
+ });
225
+ } catch (e) {
226
+ console.error(e);
227
+ }
228
+ }
229
+
230
+ fetchHistory();