Skip to content

Commit

Permalink
oops quick bug fix (#1269)
Browse files Browse the repository at this point in the history
  • Loading branch information
LimesKey authored Oct 24, 2024
1 parent 1c2d2cc commit 9a86a44
Showing 1 changed file with 54 additions and 35 deletions.
89 changes: 54 additions & 35 deletions showcase/src/routes/showcase/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
renderer.image = () => '';
onMount(async () => {
console.log('Component mounted, starting data fetch...');
const urlParams = new URLSearchParams(window.location.search);
const slackIDParam = urlParams.get('slackID');
const hashParam = urlParams.get('hash');
const slackUsernameParam = urlParams.get('slackUsername');
if (slackIDParam && hashParam && slackUsernameParam) {
console.log('Storing URL parameters in local storage...');
localStorage.setItem('slackID', slackIDParam);
localStorage.setItem('slackUsername', slackUsernameParam);
localStorage.setItem('hash', hashParam);
Expand All @@ -59,6 +61,7 @@
slackUsername = localStorage.getItem('slackUsername');
try {
console.log('Fetching submissions from API...');
const response = await fetch('/api/submissions');
const data = await response.json();
Expand All @@ -67,7 +70,9 @@
if (Array.isArray(data)) {
submissions = data;
maxVotes = Math.ceil(submissions.length * 0.1);
console.log(`Submissions loaded successfully. Max votes set to: ${maxVotes}`);
} else {
console.error('Response is not an array:', data);
throw new Error('Response is not an array');
}
} catch (err) {
Expand All @@ -77,41 +82,46 @@
});
function handleVote(submission: Submission) {
if (!slackID) {
showModal.set(true);
return;
}
const previousVote = Object.keys(votes).find((id) => votes[id] === selectedCategory);
console.log(`Attempting to vote for submission ID: ${submission.id}`);
if (previousVote) {
return;
}
if (!slackID) {
console.warn('Slack ID is not available. Showing modal...');
showModal.set(true);
return;
}
votes[submission.id] = selectedCategory;
currentVotes += 1;
hasVoted = true;
const previousVote = Object.keys(votes).find((id) => votes[id] === selectedCategory);
if (previousVote) {
console.log(`Previous vote found for submission ID: ${previousVote}. Ignoring new vote.`);
return;
}
const gridItem = document.querySelector(`.grid-item[data-id="${submission.id}"]`);
if (gridItem) {
gridItem.classList.add('voted');
setTimeout(() => {
gridItem.classList.remove('voted');
}, 1000);
}
votes[submission.id] = selectedCategory;
currentVotes += 1;
hasVoted = true;
console.log(`Voted for ${submission.fields['GitHub handle']} in category: ${selectedCategory}`);
// Scroll to the top after voting
window.scrollTo({ top: 0, behavior: 'smooth' });
const gridItem = document.querySelector(`.grid-item[data-id="${submission.id}"]`);
if (gridItem) {
gridItem.classList.add('voted');
setTimeout(() => {
gridItem.classList.remove('voted');
console.log(`Voted class removed for submission ID: ${submission.id}`);
}, 1000);
}
if (currentVotes >= maxVotes) {
// Scroll to the top after voting
console.log('Scrolling to the top after voting...');
window.scrollTo({ top: 0, behavior: 'smooth' });
if (currentVotes <= maxVotes) {
setTimeout(nextCategory, 2000);
}
}
} }
async function submitAllVotes() {
try {
const hash = localStorage.getItem('hash');
console.log('Submitting all votes...', { votes, slackID, hashedSlackID: hash });
const response = await fetch('/api/vote', {
method: 'POST',
headers: {
Expand All @@ -126,15 +136,17 @@
if (!response.ok) {
const errorData = await response.json();
console.error('Failed to submit votes:', errorData);
throw new Error(errorData.error || 'Failed to submit votes');
}
console.log('All votes submitted');
console.log('All votes submitted successfully');
voteSubmitted = true;
successMessage = 'Success';
showSuccessModal.set(true);
setTimeout(() => {
console.log('Redirecting to the homepage...');
window.location.href = 'https://onboard.hackclub.com';
}, 10000);
} catch (error) {
Expand All @@ -146,26 +158,33 @@
}
function nextCategory() {
if (categoryIndex < categories.length - 1) {
categoryIndex += 1;
selectedCategory = categories[categoryIndex];
currentVotes = 0;
hasVoted = false;
} else {
showConfirmation.set(true);
}
}
console.log(`Current votes: ${currentVotes}, Max votes: ${maxVotes}, Has voted: ${hasVoted}`);
if (categoryIndex < categories.length - 1) {
categoryIndex += 1;
selectedCategory = categories[categoryIndex];
currentVotes = 0;
hasVoted = false;
console.log(`Switched to next category: ${selectedCategory}`);
} else {
console.log("All categories completed. Displaying confirmation modal.");
showConfirmation.set(true);
}
}
function resetVotes() {
console.log('Resetting votes...');
window.location.reload();
}
function confirmVotes() {
console.log('Confirming votes...');
showConfirmation.set(false);
submitAllVotes();
}
function toggleShowMore(id: string) {
console.log(`Toggling show more for submission ID: ${id}`);
showMore.update((current) => ({
...current,
[id]: !current[id]
Expand Down

0 comments on commit 9a86a44

Please sign in to comment.