Clean up dead code, unify helpers, move common elements to app.js, add RSVP confirmation
This commit is contained in:
25
main.go
25
main.go
@@ -185,6 +185,16 @@ func authorize(r *http.Request) (string, bool) {
|
||||
return email, true
|
||||
}
|
||||
|
||||
func getRSVP(eventID, email string) (int, float64, error) {
|
||||
var numPeople int
|
||||
var donation float64
|
||||
err := db.QueryRow("SELECT num_people, donation FROM rsvps WHERE event_id = $1 AND google_username = $2", eventID, email).Scan(&numPeople, &donation)
|
||||
if err == sql.ErrNoRows {
|
||||
return 0, 0, nil
|
||||
}
|
||||
return numPeople, donation, err
|
||||
}
|
||||
|
||||
func handleRSVPGet(w http.ResponseWriter, r *http.Request) {
|
||||
eventID := r.PathValue("eventID")
|
||||
email, ok := authorize(r)
|
||||
@@ -193,13 +203,8 @@ func handleRSVPGet(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var numPeople int
|
||||
var donation float64
|
||||
err := db.QueryRow("SELECT num_people, donation FROM rsvps WHERE event_id = $1 AND google_username = $2", eventID, email).Scan(&numPeople, &donation)
|
||||
if err == sql.ErrNoRows {
|
||||
numPeople = 0
|
||||
donation = 0
|
||||
} else if err != nil {
|
||||
numPeople, donation, err := getRSVP(eventID, email)
|
||||
if err != nil {
|
||||
log.Println("[ERROR] failed to query rsvp:", err)
|
||||
http.Error(w, "database error", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -237,10 +242,8 @@ func handleRSVPPost(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
var numPeople int
|
||||
var donation float64
|
||||
err := db.QueryRow("SELECT num_people, donation FROM rsvps WHERE event_id = $1 AND google_username = $2", eventID, email).Scan(&numPeople, &donation)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
numPeople, donation, err := getRSVP(eventID, email)
|
||||
if err != nil {
|
||||
log.Println("[ERROR] failed to query rsvp:", err)
|
||||
http.Error(w, "database error", http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<script>document.documentElement.className = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'wa-dark' : 'wa-light';</script>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
|
||||
<link rel="icon" type="image/png" href="/favicon.png">
|
||||
<link rel="apple-touch-icon" href="/favicon.png">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Applause for a Cause - HCA Tickets</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3/dist-cdn/styles/themes/default.css">
|
||||
<script type="module" src="https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3/dist-cdn/webawesome.loader.js"></script>
|
||||
<style>
|
||||
body {
|
||||
font-family: var(--wa-font-sans);
|
||||
@@ -105,9 +99,6 @@
|
||||
background-color: #FEDE02;
|
||||
color: #000;
|
||||
}
|
||||
.donation-group wa-input::part(prefix) {
|
||||
padding-right: 0;
|
||||
}
|
||||
.donation-section {
|
||||
margin-top: 1rem;
|
||||
padding-top: 1rem;
|
||||
@@ -142,6 +133,9 @@
|
||||
<div id="thank-you" class="thank-you" style="display: none;">
|
||||
Thank you for your donation!
|
||||
</div>
|
||||
<div id="rsvp-confirmed" class="thank-you" style="display: none;">
|
||||
Your RSVP has been updated!
|
||||
</div>
|
||||
<wa-card>
|
||||
<div class="event-header">
|
||||
<img src="/afac26-logo.png" alt="Applause for a Cause">
|
||||
@@ -183,10 +177,6 @@
|
||||
<script type="module">
|
||||
import { auth, logout, api } from '/app.js';
|
||||
|
||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
|
||||
document.documentElement.className = e.matches ? 'wa-dark' : 'wa-light';
|
||||
});
|
||||
|
||||
await auth();
|
||||
document.getElementById('main').style.display = 'block';
|
||||
document.getElementById('logout-btn').addEventListener('click', logout);
|
||||
@@ -283,6 +273,7 @@
|
||||
location.href = data.url;
|
||||
} else {
|
||||
updateUI(data);
|
||||
document.getElementById('rsvp-confirmed').style.display = 'block';
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,32 @@
|
||||
const CLIENT_ID = '{{.env.GOOGLE_CLIENT_ID}}';
|
||||
|
||||
function initHead() {
|
||||
document.documentElement.className = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'wa-dark' : 'wa-light';
|
||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
|
||||
document.documentElement.className = e.matches ? 'wa-dark' : 'wa-light';
|
||||
});
|
||||
|
||||
const head = document.head;
|
||||
const addLink = (rel, type, href) => {
|
||||
const link = document.createElement('link');
|
||||
link.rel = rel;
|
||||
if (type) link.type = type;
|
||||
link.href = href;
|
||||
head.appendChild(link);
|
||||
};
|
||||
addLink('icon', 'image/svg+xml', '/favicon.svg');
|
||||
addLink('icon', 'image/png', '/favicon.png');
|
||||
addLink('apple-touch-icon', null, '/favicon.png');
|
||||
addLink('stylesheet', null, 'https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3/dist-cdn/styles/themes/default.css');
|
||||
|
||||
const script = document.createElement('script');
|
||||
script.type = 'module';
|
||||
script.src = 'https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3/dist-cdn/webawesome.loader.js';
|
||||
head.appendChild(script);
|
||||
}
|
||||
|
||||
initHead();
|
||||
|
||||
function getProfile() {
|
||||
const data = localStorage.getItem('profile');
|
||||
return data ? JSON.parse(data) : null;
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<script>document.documentElement.className = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'wa-dark' : 'wa-light';</script>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
|
||||
<link rel="icon" type="image/png" href="/favicon.png">
|
||||
<link rel="apple-touch-icon" href="/favicon.png">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>HCA Tickets</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3/dist-cdn/styles/themes/default.css">
|
||||
<script type="module" src="https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3/dist-cdn/webawesome.loader.js"></script>
|
||||
<style>
|
||||
body {
|
||||
font-family: var(--wa-font-sans);
|
||||
@@ -78,10 +72,6 @@
|
||||
<script type="module">
|
||||
import { auth, logout } from '/app.js';
|
||||
|
||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
|
||||
document.documentElement.className = e.matches ? 'wa-dark' : 'wa-light';
|
||||
});
|
||||
|
||||
await auth();
|
||||
document.getElementById('main').style.display = 'block';
|
||||
document.getElementById('logout-btn').addEventListener('click', logout);
|
||||
|
||||
Reference in New Issue
Block a user