-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
205 lines (174 loc) · 7.03 KB
/
script.js
File metadata and controls
205 lines (174 loc) · 7.03 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
const API_BASE_URL = "http://127.0.0.1:5000";
const loginBtn = document.getElementById("loginBtn");
const signupBtn = document.getElementById("signupBtn"); // For the form submit
const roadmapForm = document.getElementById("roadmap-form");
const chatInput = document.getElementById("chatInput");
const chatSendBtn = document.getElementById("chatSendBtn");
function checkAuth() {
const token = localStorage.getItem("auth_token");
if (!token) {
window.location.href = "index.html";
}
return token;
}
if (loginBtn) {
loginBtn.addEventListener("click", async () => {
const u = document.getElementById("username").value;
const p = document.getElementById("password").value;
if (!u || !p) return alert("Please fill in all fields");
try {
loginBtn.textContent = "Logging in...";
const res = await fetch(`${API_BASE_URL}/login`, {
method: "POST", headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username: u, password: p })
});
const data = await res.json();
if (data.status === "success") {
localStorage.setItem("auth_token", data.auth_token);
localStorage.setItem("username", data.username);
window.location.href = "home.html";
} else {
alert(data.message);
}
} catch (e) { alert("Server error"); }
loginBtn.textContent = "Login";
});
}
const signupForm = document.getElementById("signupForm");
if (signupForm) {
signupForm.addEventListener("submit", async (e) => {
e.preventDefault();
const u = document.getElementById("newUsername").value;
const p = document.getElementById("newPassword").value;
try {
const res = await fetch(`${API_BASE_URL}/signup`, {
method: "POST", headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username: u, password: p })
});
const data = await res.json();
if (data.status === "success") {
localStorage.setItem("auth_token", data.auth_token);
localStorage.setItem("username", data.username);
window.location.href = "home.html";
} else {
alert(data.message);
}
} catch (e) { alert("Server error"); }
});
}
if (document.getElementById("welcomeUser")) {
const user = localStorage.getItem("username");
if (!user) window.location.href = "index.html";
// *** UPDATED THIS LINE ***
document.getElementById("welcomeUser").textContent = user;
}
if (roadmapForm) {
checkAuth();
// Load existing plan on load
fetchPlan();
roadmapForm.addEventListener("submit", async (e) => {
e.preventDefault();
const btn = document.getElementById("generate-btn");
const msg = document.getElementById("form-message");
const token = localStorage.getItem("auth_token");
btn.disabled = true;
btn.textContent = "AI is Thinking...";
msg.textContent = "Crafting your strategy...";
try {
const res = await fetch(`${API_BASE_URL}/generate_plan`, {
method: "POST",
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${token}` },
body: JSON.stringify({
career_goal: document.getElementById("careerGoal").value,
yearly_goal: document.getElementById("yearlyGoal").value
})
});
const data = await res.json();
if (data.status === "success") {
renderPlan(data.plan);
msg.textContent = "";
} else {
msg.textContent = data.message;
}
} catch (err) { msg.textContent = "Error connecting to AI."; }
btn.disabled = false;
btn.textContent = "Regenerate Plan";
});
}
async function fetchPlan() {
const token = localStorage.getItem("auth_token");
const username = localStorage.getItem("username");
try {
const res = await fetch(`${API_BASE_URL}/plans/${username}`, {
headers: { "Authorization": `Bearer ${token}` }
});
const data = await res.json();
if (data.status === "success") renderPlan(data.plan);
} catch (e) { console.log("No existing plan"); }
}
function renderPlan(plan) {
const container = document.getElementById("roadmap-display");
if (!container) return; // Guard clause
container.innerHTML = ""; // Clear old
let delay = 0;
for (let i = 1; i <= 12; i++) {
if (plan[i]) {
const card = document.createElement("div");
card.className = "month-card";
card.style.animationDelay = `${delay}s`; // Staggered animation
let listItems = plan[i].weekly.map(task => `<li>${task}</li>`).join("");
card.innerHTML = `
<h4>Month ${i}: ${plan[i].monthly_goal}</h4>
<ul>${listItems}</ul>
`;
container.appendChild(card);
delay += 0.1; // Increase delay for next card
}
}
}
if (chatInput) {
checkAuth();
const chatBox = document.getElementById("chat-box");
function addMsg(text, sender) {
const d = document.createElement("div");
d.className = `msg ${sender}`;
d.textContent = text;
chatBox.appendChild(d);
chatBox.scrollTop = chatBox.scrollHeight;
}
async function sendChat() {
const txt = chatInput.value.trim();
if(!txt) return;
addMsg(txt, "user");
chatInput.value = "";
const loading = document.createElement("div");
loading.className = "msg bot";
loading.textContent = "Thinking...";
chatBox.appendChild(loading);
try {
const res = await fetch(`${API_BASE_URL}/api/chat`, {
method: "POST", headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: txt })
});
const data = await res.json();
chatBox.removeChild(loading);
addMsg(data.bot_message, "bot");
} catch (e) {
chatBox.removeChild(loading);
addMsg("Connection error.", "bot");
}
}
chatSendBtn.addEventListener("click", sendChat);
chatInput.addEventListener("keypress", (e) => { if(e.key==="Enter") sendChat(); });
}
function logout() {
window.location.href = "logout.html";
}
// This logic runs *on* the logout.html page
if (document.getElementById("logout-page-identifier")) {
localStorage.removeItem("auth_token");
localStorage.removeItem("username");
setTimeout(() => {
window.location.href = "index.html";
}, 1500);
}