|
|
@@ -1,3 +1,4 @@
|
|
|
+//static/js/frontend.js
|
|
|
function setLoggedIn(loggedInBoolean) {
|
|
|
let body = document.body;
|
|
|
body.dataset.loggedIn = !!loggedInBoolean;
|
|
|
@@ -83,7 +84,10 @@ function updateVotableDays(daysWithVotes, currentSession, weatherForecasts) {
|
|
|
for (let date in daysWithVotes) {
|
|
|
let votes = daysWithVotes[date];
|
|
|
let weather = weatherForecasts?.[date];
|
|
|
- daysView.append(createOneDayCard({ date, votes }, currentSession, weather))
|
|
|
+ daysView.append(createOneDayCard({
|
|
|
+ date,
|
|
|
+ votes
|
|
|
+ }, currentSession, weather))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -118,7 +122,11 @@ class FrontendState {
|
|
|
}
|
|
|
|
|
|
async refreshVotesState(updateView = true) {
|
|
|
- let { success, data, error } = await getVotesFromBackend()
|
|
|
+ let {
|
|
|
+ success,
|
|
|
+ data,
|
|
|
+ error
|
|
|
+ } = await getVotesFromBackend()
|
|
|
if (success) {
|
|
|
this.daysWithVotes = data;
|
|
|
} else {
|
|
|
@@ -131,7 +139,11 @@ class FrontendState {
|
|
|
}
|
|
|
|
|
|
async refreshWeatherState(updateView = true) {
|
|
|
- let { success, data, error } = await getWeather()
|
|
|
+ let {
|
|
|
+ success,
|
|
|
+ data,
|
|
|
+ error
|
|
|
+ } = await getWeather()
|
|
|
if (success) {
|
|
|
this.weatherForecasts = data;
|
|
|
} else {
|
|
|
@@ -144,7 +156,11 @@ class FrontendState {
|
|
|
}
|
|
|
|
|
|
async refreshSessionState(updateView = true) {
|
|
|
- let { success, data, error } = await getSessionFromBackend()
|
|
|
+ let {
|
|
|
+ success,
|
|
|
+ data,
|
|
|
+ error
|
|
|
+ } = await getSessionFromBackend()
|
|
|
if (success) {
|
|
|
this.currentSession = data;
|
|
|
} else {
|
|
|
@@ -194,23 +210,38 @@ async function handleAuthEvent(event) {
|
|
|
signup: ajaxSignup,
|
|
|
login: ajaxLogin,
|
|
|
logout: ajaxLogout,
|
|
|
- }[authActionName];
|
|
|
+ } [authActionName];
|
|
|
if (authActionFunction) {
|
|
|
let authResult = await authActionFunction(usernameValue, passwordValue);
|
|
|
if (authResult && authResult.success) {
|
|
|
+ if (authActionName === 'logout') {
|
|
|
+ // 登出成功,清除用戶名顯示
|
|
|
+ updateUsernameDisplay('');
|
|
|
+ } else {
|
|
|
+ // 登入或註冊成功,更新用戶名顯示
|
|
|
+ updateUsernameDisplay(authResult.data.username);
|
|
|
+ }
|
|
|
await fes.refreshSessionState();
|
|
|
usernameInput.value = passwordInput.value = '';
|
|
|
} else if (authResult) {
|
|
|
- // yo this is crap and if you want to be better than me, replace it.
|
|
|
- alert(authResult.error)
|
|
|
+ // 處理登入、註冊或登出失敗的情況
|
|
|
+ alert(authResult.error);
|
|
|
} else {
|
|
|
- // yo this is crap and if you want to be better than me, replace it.
|
|
|
- alert("unknown network error")
|
|
|
+ // 處理未知網絡錯誤
|
|
|
+ alert("unknown network error");
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+function updateUsernameDisplay(username) {
|
|
|
+ const usernameSpan = document.querySelector('.username');
|
|
|
+ if (usernameSpan) {
|
|
|
+ usernameSpan.textContent = username || 'nobody'; // 如果username為空,顯示'nobody'
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
const authform = document.querySelector('form.authform')
|
|
|
authform.addEventListener("click", handleAuthEvent);
|
|
|
|
|
|
@@ -225,9 +256,15 @@ async function handleVoteEvent(event) {
|
|
|
|
|
|
if (button) {
|
|
|
let voteVal;
|
|
|
- if (button.classList.contains('yes')) { voteVal = 'yes'; }
|
|
|
- if (button.classList.contains('no')) { voteVal = 'no'; }
|
|
|
- if (button.classList.contains('maybe')) { voteVal = 'maybe'; }
|
|
|
+ if (button.classList.contains('yes')) {
|
|
|
+ voteVal = 'yes';
|
|
|
+ }
|
|
|
+ if (button.classList.contains('no')) {
|
|
|
+ voteVal = 'no';
|
|
|
+ }
|
|
|
+ if (button.classList.contains('maybe')) {
|
|
|
+ voteVal = 'maybe';
|
|
|
+ }
|
|
|
let cardDiv = button.closest("div.card");
|
|
|
if (!voteVal || !cardDiv) {
|
|
|
// console.log({ voteVal, cardDiv })
|