Skip to content

Commit

Permalink
Trick or Trace Voting (#1258)
Browse files Browse the repository at this point in the history
voting!
  • Loading branch information
LimesKey authored Oct 23, 2024
1 parent a7a267c commit d5f1491
Show file tree
Hide file tree
Showing 9 changed files with 1,009 additions and 371 deletions.
23 changes: 12 additions & 11 deletions showcase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,35 @@
"format": "prettier --write ."
},
"devDependencies": {
"@sveltejs/adapter-auto": "^3.2.5",
"@sveltejs/adapter-cloudflare": "^4.7.2",
"@sveltejs/kit": "^2.5.28",
"@sveltejs/adapter-auto": "^3.3.0",
"@sveltejs/adapter-cloudflare": "^4.7.3",
"@sveltejs/kit": "^2.7.2",
"@sveltejs/vite-plugin-svelte": "^3.1.2",
"@types/eslint": "^9.6.1",
"@types/three": "^0.168.0",
"eslint": "^9.11.1",
"eslint": "^9.13.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.44.0",
"globals": "^15.9.0",
"eslint-plugin-svelte": "^2.46.0",
"globals": "^15.11.0",
"prettier": "^3.3.3",
"prettier-plugin-svelte": "^3.2.7",
"svelte": "^4.2.19",
"svelte-check": "^3.8.6",
"typescript": "^5.6.2",
"typescript-eslint": "^8.7.0",
"vite": "^5.4.8"
"typescript": "^5.6.3",
"typescript-eslint": "^8.11.0",
"vite": "^5.4.9"
},
"prepare": "svelte-kit sync",
"type": "module",
"dependencies": {
"@cloudflare/d1": "^1.4.1",
"@hackclub/banner": "^1.0.3",
"@threlte/core": "^7.3.1",
"@threlte/extras": "^8.11.5",
"@threlte/extras": "^8.12.0",
"@threlte/flex": "^1.0.3",
"copy-file": "^11.0.0",
"three": "^0.167.1",
"vite-multiple-assets": "^1.3.1",
"zero-md": "^3.1.5"
"zero-md": "^3.1.6"
}
}
711 changes: 354 additions & 357 deletions showcase/pnpm-lock.yaml

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions showcase/src/lib/components/Slack.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script>
// Your Slack OAuth URL
const slackAuthUrl = `https://slack.com/oauth/v2/authorize?client_id=2210535565.6498112565286&scope=&user_scope=openid,email,profile&redirect_uri=https://trickortrace.hackclub.com/oauth/slack`;
export function handleSlackSignIn() {
// Redirect user to Slack's OAuth URL
window.location.href = slackAuthUrl;
}
</script>

<style>
.slack-login-button {
padding: 12px 24px;
background-color: #4A154B;
color: #fff;
border: none;
border-radius: 5px;
text-decoration: none;
font-size: 1.2rem;
font-family: 'Roboto', sans-serif;
transition: background-color 0.3s ease;
}
.slack-login-button:hover {
background-color: #3a0d3b;
}
</style>

<!-- Slack Sign-in Button -->
<button class="slack-login-button" on:click={handleSlackSignIn}>
Sign in with Slack
</button>

4 changes: 2 additions & 2 deletions showcase/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@
<h2>Design a PCB this October, vote on the best designs, get a second grant</h2>
</div>
<a
href="https://verify.onboard.hackclub.com"
href="https://trickortrace.hackclub.com/showcase"
target="_blank"
class="form-link"
id="form-link"
aria-label="Submit your creation">
aria-label="Vote now!">
<h3>Submit your creation</h3>
<h4>Submissions due on October 21st</h4>
</a>
Expand Down
42 changes: 42 additions & 0 deletions showcase/src/routes/api/submissions/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { json } from '@sveltejs/kit';
import { NEW_AIRTABLE_KEY } from '$env/static/private';

const AIRTABLE_BASE_ID = 'app4Bs8Tjwvk5qcD4';
const SUBMISSIONS_TABLE_NAME = 'Submissions';

export async function GET() {
const airtablekey = NEW_AIRTABLE_KEY;
console.log('GET /api/submissions called with airtablekey');

if (!airtablekey || airtablekey === '' || airtablekey === 'undefined' || airtablekey === 'null') {
console.error('Airtable API key is required');
return json({ error: 'Airtable API key is required' }, { status: 400 });
}

try {
const response = await fetch(
`https://api.airtable.com/v0/${AIRTABLE_BASE_ID}/${SUBMISSIONS_TABLE_NAME}?fields%5B%5D=GitHub%20handle&fields%5B%5D=Description&fields%5B%5D=PR%20Link&fields%5B%5D=Screenshots&filterByFormula=AND(%7BStatus%7D%3D'Approved'%2C%7BTrick%20or%20Trace%7D%3D1)`,
{
headers: {
Authorization: `Bearer ${airtablekey}`,
'Content-Type': 'application/json',
},
},
);

const data: any = await response.json();

// if !data then return the raw response from Airtable
if (!data || !data.records) {
console.warn('No records found in Airtable response');
return json(data, { status: 200 });
}

const rawSubmissions = data.records.map((record: any) => record);

return json(rawSubmissions, { status: 200 });
} catch (error) {
console.error('Error fetching data from Airtable:', error);
return json({ error: 'Failed to fetch data from Airtable' }, { status: 500 });
}
}
46 changes: 46 additions & 0 deletions showcase/src/routes/api/vote/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { json } from '@sveltejs/kit';
import type { RequestEvent } from '@sveltejs/kit';

export async function POST({ request }: RequestEvent) {
try {
const { votes, slackID, hashedSlackID } = await request.json();

if (!votes || !slackID || !hashedSlackID) {
return json({ error: 'Missing required fields' }, { status: 400 });
}

// Submit the votes to the external API
for (const [submissionId, category] of Object.entries(votes)) {
console.log('Sending vote to external API:', { submissionId, category, slackID, hashedSlackID });

const response = await fetch('https://blue-river-03bf.ryandilorenzo264.workers.dev/vote', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
submissionId,
slackID,
hashedSlackID,
category,
}),
});

if (!response.ok) {
const errorData = await response.json();
console.error('Failed to submit vote to external API:', errorData);
throw new Error(errorData.error || 'Failed to submit vote to external API');
}

console.log('Vote submitted to external API:', { submissionId, category });
}

// Log the successful request
console.log('Vote request:', { votes, slackID, hashedSlackID });

return json({ message: 'Votes submitted successfully' }, { status: 200 });
} catch (error) {
console.error('Error submitting votes:', error);
return json({ error: `Failed to submit votes: ${(error as Error).message}` }, { status: 500 });
}
}
43 changes: 43 additions & 0 deletions showcase/src/routes/oauth/slack/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { redirect, json } from '@sveltejs/kit';
import type { RequestEvent } from '@sveltejs/kit';

export async function GET({ url }: RequestEvent) {
const code = url.searchParams.get('code');

if (!code) {
console.error('Missing code');
return json({ error: 'Missing code' }, { status: 400 });
}

console.log('Sending code to OnBoard API...');

const payload = {
slack_code: code,
special_secret: "hello"
};
console.log('Payload:', JSON.stringify(payload));

// Send the code to the OnBoard API
const apiResponse = await fetch('https://api.onboard.limeskey.com', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});

const apiData = await apiResponse.json();
console.log('API response:', apiData);

// Extract relevant info from the API response
const slackID = apiData.slack.slack_id;
const slackUsername = apiData.slack.username;
const hash = apiData.hashed_secret;

console.log('Slack ID:', slackID);
console.log('Hash:', hash);
console.log('Slack username:', slackUsername);

// Redirect back to the voting page with the Slack ID and hash
return redirect(302, `/showcase?slackID=${slackID}&hash=${hash}&slackUsername=${encodeURIComponent(slackUsername)}`);
}
Loading

0 comments on commit d5f1491

Please sign in to comment.