Initial app and server setup
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Weekly Calendar</title>
|
||||
<title>Datefinder</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
@@ -50,6 +50,10 @@
|
||||
<body>
|
||||
<h1>Datefinder</h1>
|
||||
<div class="calendar" id="calendar"></div>
|
||||
<div>
|
||||
<input id="submit-name" type="text" placeholder="Enter your name" />
|
||||
<button id="submit-btn" type="submit" onclick="submit">Submit</button>
|
||||
</div>
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
// Days of the week
|
||||
const daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
|
||||
|
||||
// Select the calendar container
|
||||
const calendar = document.getElementById("calendar");
|
||||
|
||||
// Create the weekly calendar
|
||||
daysOfWeek.forEach((day) => {
|
||||
// Create a column for each day
|
||||
const dayColumn = document.createElement("div");
|
||||
dayColumn.classList.add("day-column");
|
||||
|
||||
// Add a header for the day
|
||||
const dayHeader = document.createElement("div");
|
||||
dayHeader.classList.add("day-header");
|
||||
dayHeader.textContent = day;
|
||||
dayColumn.appendChild(dayHeader);
|
||||
|
||||
// Add hourly blocks for each day
|
||||
for (let hour = 0; hour < 24; hour++) {
|
||||
const hourBlock = document.createElement("div");
|
||||
hourBlock.classList.add("hour-block");
|
||||
hourBlock.textContent = `${hour}:00`;
|
||||
|
||||
// Add a click event listener to toggle color
|
||||
hourBlock.addEventListener("click", () => {
|
||||
hourBlock.classList.toggle("clicked");
|
||||
});
|
||||
|
||||
dayColumn.appendChild(hourBlock);
|
||||
}
|
||||
|
||||
// Append the day column to the calendar
|
||||
calendar.appendChild(dayColumn);
|
||||
});
|
||||
|
||||
const submitName = document.getElementById("submit-name")
|
||||
const submitBtn = document.getElementById("submit-btn")
|
||||
|
||||
submitBtn.onclick = function() {
|
||||
if (submitName.value == "") {
|
||||
console.log("Submitter name is missing.")
|
||||
return
|
||||
}
|
||||
|
||||
const days = calendar.children
|
||||
const choices = Array.from(days).map(
|
||||
day => Array.from(day.children).slice(1, 25).map(
|
||||
hour => hour.classList.contains("clicked")))
|
||||
console.log(`Submitting dates for ${submitName.value}`)
|
||||
console.log(JSON.stringify(choices))
|
||||
|
||||
const url = "http://localhost:8080/api/submit";
|
||||
|
||||
fetch(url, {
|
||||
method: "POST", // Specify the HTTP method
|
||||
headers: {
|
||||
"Content-Type": "application/json", // Inform the server about the data format
|
||||
},
|
||||
body: JSON.stringify(choices), // Convert the data to JSON string
|
||||
})
|
||||
/*
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok ' + response.statusText);
|
||||
}
|
||||
return response.json(); // Parse the JSON response
|
||||
})
|
||||
.then(data => {
|
||||
console.log('Success:', data); // Handle the response data
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error); // Handle errors
|
||||
});
|
||||
*/
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
// Days of the week
|
||||
const daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
|
||||
|
||||
// Select the calendar container
|
||||
const calendar = document.getElementById("calendar");
|
||||
|
||||
// Create the weekly calendar
|
||||
daysOfWeek.forEach((day) => {
|
||||
// Create a column for each day
|
||||
const dayColumn = document.createElement("div");
|
||||
dayColumn.classList.add("day-column");
|
||||
|
||||
// Add a header for the day
|
||||
const dayHeader = document.createElement("div");
|
||||
dayHeader.classList.add("day-header");
|
||||
dayHeader.textContent = day;
|
||||
dayColumn.appendChild(dayHeader);
|
||||
|
||||
// Add hourly blocks for each day
|
||||
for (let hour = 0; hour < 24; hour++) {
|
||||
const hourBlock = document.createElement("div");
|
||||
hourBlock.classList.add("hour-block");
|
||||
hourBlock.textContent = `${hour}:00`;
|
||||
|
||||
// Add a click event listener to toggle color
|
||||
hourBlock.addEventListener("click", () => {
|
||||
hourBlock.classList.toggle("clicked");
|
||||
});
|
||||
|
||||
dayColumn.appendChild(hourBlock);
|
||||
}
|
||||
|
||||
// Append the day column to the calendar
|
||||
calendar.appendChild(dayColumn);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
PORT = "8080"
|
||||
)
|
||||
|
||||
func handleGetPing(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("[*] Received request %v %v\n", r.Method, r.URL)
|
||||
fmt.Fprintf(w, "PONG\n")
|
||||
}
|
||||
|
||||
func handlePostSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("[*] Received request %v %v\n", r.Method, r.URL)
|
||||
log.Printf("[*] %v\n", r.Body)
|
||||
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Origin")
|
||||
|
||||
var choices [][]bool
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&choices)
|
||||
if err != nil {
|
||||
log.Printf("[!] Error decoding request Body %v, error: %s\n", r.Body, err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
log.Printf("TODO: Persist to database %v\n", choices)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func handleOptionsSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Origin")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc(
|
||||
"GET /api/ping",
|
||||
handleGetPing,
|
||||
)
|
||||
|
||||
http.HandleFunc(
|
||||
"POST /api/submit",
|
||||
handlePostSubmit,
|
||||
)
|
||||
|
||||
http.HandleFunc(
|
||||
"OPTIONS /api/submit",
|
||||
handleOptionsSubmit,
|
||||
)
|
||||
|
||||
log.Printf("[*] Datefinder server listening on :%s\n", PORT)
|
||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||
log.Fatalf("[!] Could not start server on %s, error: %s\n", PORT, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user