Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
joypan1 committed Feb 26, 2024
2 parents 0694fee + e915b6c commit 7944246
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 73 deletions.
40 changes: 21 additions & 19 deletions pages/teamdetails.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,29 @@ <h1> Team Details </h1>
</header>
<br>


<form class="teaminfo">
<label for="teamname"> Name: </label>
<input type="text" id="teamname" name="teamname">
<!-- <div id="team-details"> -->
<form id="dataForm" class="teaminfo">
<label for="teamname"> Name: </label>
<input type="text" id="teamname" name="teamname">

<br>
<br>

<label for="teamnumber"> Team Number: </label>
<input type="text" id="teamnumber" name="teamnumber" maxlength="4">

<br>
<label for="teamnumber"> Team Number: </label>
<input type="text" id="teamnumber" name="teamnumber" maxlength="4">


<br>

<label for="teamschool"> Based In: </label>
<input type="text" id="teamschool" name="teamschool">
<label for="teamschool"> Based In: </label>
<input type="text" id="teamschool" name="teamschool">

<br><br>

<label for="moreinfo"> More Info: </label><br>
<textarea id="moreinfo" name="moreinfo" rows="8" cols="50" placeholder="big fat textbox" style="resize: none;"></textarea>
<br><br>
<label for="moreinfo"> More Info: </label><br>
<textarea id="moreinfo" name="moreinfo" rows="8" cols="50" placeholder="big fat textbox" style="resize: none;"></textarea>

</form>
</form>
<!-- </div> -->

<br>

Expand All @@ -73,10 +75,10 @@ <h2> Abilities </h2>
<input type="submit" class="submit">


<h1>Team Details</h1>
<!-- <h1>Team Details</h1>
<div id="team-details">
<!-- Team details will be displayed here -->
</div>
Team details will be displayed here
</div> -->

<input type="submit" class="submit" id="submit">

Expand Down
171 changes: 117 additions & 54 deletions pages/teamdetails.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,123 @@
// Initialize IndexedDB database
document.addEventListener('DOMContentLoaded', async () => {
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id');

const nameInput = document.getElementById('teamnumber');
const emailInput = document.getElementById('email');

// Check if ID exists, if yes, populate the form fields
if (id) {
try {
const response = await fetch(`/getData?id=${id}`);
const data = await response.json();

// Populate the form fields with existing data
nameInput.value = data.teamnumber || '';
emailInput.value = data.email || '';
} catch (error) {
console.error('Error fetching data:', error);
}
}

const form = document.getElementById('dataForm');
form.addEventListener('submit', async (event) => {
event.preventDefault();

const formData = {
name: nameInput.value,
email: emailInput.value
};

try {
// Handle submitting new information
// Here you can send the formData to your server
console.log('Submitting data:', formData);

// Optionally, you can also save the submitted data to IndexedDB
// This ensures that the form is pre-filled the next time it's accessed with the same ID
await saveDataToIndexedDB(formData, id);

// Reset the form after submission
form.reset();
} catch (error) {
console.error('Error submitting data:', error);
}
});
});

async function saveDataToIndexedDB(data, id) {
const db = await openDB('myIndexedDB', 1);
const tx = db.transaction('storeName', 'readwrite');
const store = tx.objectStore('storeName');

// If an ID exists, update existing data; otherwise, add new data
if (id) {
await store.put(data, id);
} else {
await store.add(data);
}

await tx.done;
}

//NOTE: we need to rename all of the variables in here from the "To-Do List" template to our "Teams"

//NOTE: I added ChatGPT-based explanations to break down this code because otherwise it's unintelligible lol
// // Initialize IndexedDB database

// QUESTION: Are teams that are deleted from the site also deleted from the local DB?
// //NOTE: we need to rename all of the variables in here from the "To-Do List" template to our "Teams"

const db = new Dexie("Todo App");
db.version(1).stores({ todos: "++id, todo, globalid" });
// //NOTE: I added ChatGPT-based explanations to break down this code because otherwise it's unintelligible lol

// the database sets up an object "todos" (need to rename) that consists of:
// ++id - auto-incrementing primary key/field named "id"
// todo - a field called "todo"
// globalid - a field called "globalid," used to identify teams
// // QUESTION: Are teams that are deleted from the site also deleted from the local DB?

// Function to extract query parameter from URL
function getQueryParam(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
}

// URLs look like ... /scouting2024/pages/teamdetails.html?globalid=____
// the ? denotes the start of URL/query parameters (which are used to pass data to web page thru URL)
// and globalid has a value (unique UTC time) that identifies the team (i.e. 1708014161661)

// Function to fetch team details using globalid
async function fetchTeamDetails(globalid) {
const team = await db.todos.where({ globalid: Number(globalid) }).first();
return team;
}

// fetchTeamDetails is where info is pulled from local database

// Fetch globalid from URL query parameter
const globalid = getQueryParam("globalid");

// searches the URL parameters for a key called globalid and saved value in constant

/*document.getElementById("submit").addEventListener("submit", function(event){
print("hello world")
})*/

// Fetch and display team details
if (globalid) { // if globalid isn't null, undef, empty, etc.
fetchTeamDetails(globalid).then(team => {
if (team) { // if team details successfully fetched
const teamDetailsDiv = document.getElementById("team-details"); // there is a div with id "team-details" on team details page
teamDetailsDiv.innerHTML = `<p>Team Name: ${team.todo}</p>`;
} else { // team details not fetched
const teamDetailsDiv = document.getElementById("team-details");
teamDetailsDiv.innerHTML = `Team not found`;
console.error("Team not found");
}
}).catch(error => {
console.error("Error fetching team details:", error);
});q
} else {
console.error("Global ID not found in URL");
}
// const db = new Dexie("Todo App");
// db.version(1).stores({ todos: "++id, todo, globalid" });

// // the database sets up an object "todos" (need to rename) that consists of:
// // ++id - auto-incrementing primary key/field named "id"
// // todo - a field called "todo"
// // globalid - a field called "globalid," used to identify teams

// // Function to extract query parameter from URL
// function getQueryParam(name) {
// const urlParams = new URLSearchParams(window.location.search);
// return urlParams.get(name);
// }

// // URLs look like ... /scouting2024/pages/teamdetails.html?globalid=____
// // the ? denotes the start of URL/query parameters (which are used to pass data to web page thru URL)
// // and globalid has a value (unique UTC time) that identifies the team (i.e. 1708014161661)

// // Function to fetch team details using globalid
// async function fetchTeamDetails(globalid) {
// const team = await db.todos.where({ globalid: Number(globalid) }).first();
// return team;
// }

// // fetchTeamDetails is where info is pulled from local database

// // Fetch globalid from URL query parameter
// const globalid = getQueryParam("globalid");

// // searches the URL parameters for a key called globalid and saved value in constant

// /*document.getElementById("submit").addEventListener("submit", function(event){
// print("hello world")
// })*/

// // Fetch and display team details
// if (globalid) { // if globalid isn't null, undef, empty, etc.
// fetchTeamDetails(globalid).then(team => {
// if (team) { // if team details successfully fetched
// const teamDetailsDiv = document.getElementById("team-details"); // there is a div with id "team-details" on team details page
// teamDetailsDiv.innerHTML = `<p>Team Name: ${team.todo}</p>`;
// } else { // team details not fetched
// const teamDetailsDiv = document.getElementById("team-details");
// teamDetailsDiv.innerHTML = `Team not found`;
// console.error("Team not found");
// }
// }).catch(error => {
// console.error("Error fetching team details:", error);
// });q
// } else {
// console.error("Global ID not found in URL");
// }

0 comments on commit 7944246

Please sign in to comment.