Skip to content

feat: submission of projects #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/forms/selector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function Selector({ param, title, options }) {
--Please choose an option--
</option>
{options.map((option) => (
<option key={option}>{option}</option>
<option key={option.key} value={option.key}>{option.value}</option>
))}
</select>
</div>
Expand Down
8 changes: 8 additions & 0 deletions src/components/header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ const headerClass = sticky
Register
</a>
</li>
<li>
<a
href="/projects"
class="rounded-full px-2.5 py-1 text-xs font-semibold text-white shadow-sm ring-1 ring-inset ring-white hover:ring-secondary"
>
Upload Project
</a>
</li>
</ul>
</nav>
<button
Expand Down
42 changes: 40 additions & 2 deletions src/components/projectSubmission.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import { useState } from "react";
import FormsTemplate from "./forms/formsTemplate.jsx";
import TextInput from "~/components/forms/textInput.jsx";
import TextBox from "~/components/forms/textBox.jsx";
import Selector from "~/components/forms/selector.jsx";
import ConfirmationModal from "~/components/confirmationModal.jsx";
import InformationModal from "~/components/informationModal.jsx";
import Button from "./button.jsx";
import ErrorBox from "~/components/forms/errorBox.jsx";
import themes from "~/data/themes.json";

export default function ProjectDelivery() {
const [responseErrors, setResponseErrors] = useState([]);
const [loadingState, setLoadingState] = useState(false);
const [showModal, setShowModal] = useState(false);
const [showInfoModal, setShowInfoModal] = useState(false);

async function submit(e) {
console.log("submit");
Expand All @@ -28,7 +32,8 @@ export default function ProjectDelivery() {
setResponseErrors(data.message.errors);
setLoadingState(false);
} else {
window.location.href = "/";
setShowInfoModal(true);
setLoadingState(false);
}
}

Expand All @@ -40,6 +45,15 @@ export default function ProjectDelivery() {
setShowModal(false);
}

function goBack() {
closeInfoModal();
window.location.href = "/";
}

function closeInfoModal() {
setShowInfoModal(false);
}

return (
<FormsTemplate
title="Project Submission"
Expand All @@ -65,6 +79,16 @@ export default function ProjectDelivery() {
title="Project link"
placeholder="Insert your project repo link"
/>

<Selector
param="theme"
title="Project Theme"
options={themes.map((theme) => ({
key: theme.company,
value: `${theme.company}: ${theme.theme}`
}))}
/>

<TextBox
param="description"
title="Project description"
Expand All @@ -83,10 +107,24 @@ export default function ProjectDelivery() {
/>
{showModal && (
<ConfirmationModal
placeHolder="Are you sure you want to submit?"
title="Are you sure you want to submit?"
content="This is a unique submission and cannot be undone."
closeModal={closeModal}
/>
)}
{showInfoModal && (
<InformationModal
title="Project submitted!"
content={
<>
Your project has been successfully submitted!
Thank you for participating in <span class="text-primary">Hackathon Bugsbyte</span>. Good luck!
</>
}
closeModal={goBack}
/>
)}

</form>
</FormsTemplate>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/registerForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default function Form() {
<Selector
param="university"
title="University"
options={universities}
options={universities.map((uni) => ({ key: uni, value: uni }))}
/>

<TextInput
Expand Down
14 changes: 14 additions & 0 deletions src/data/themes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"company": "McSonae",
"theme": "Price optimization tool"
},
{
"company": "Uphold",
"theme": "Crypto basket price predictor"
},
{
"company": "SingleStore",
"theme": "Real-Time GenAI: Build Smarter, Faster AI Apps"
}
]
58 changes: 51 additions & 7 deletions src/pages/api/projects/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const supabase = createClient(
import.meta.env.SUPABASE_ANON_KEY,
);

const apiGithub = "https://api.github.com/repos/";
// TODO: Change this date to the contest start date
const beginContestDate = new Date("2022-03-28T18:00:00Z");

export const POST: APIRoute = async ({ request }) => {
const formData = await request.formData();

Expand All @@ -31,6 +35,7 @@ export const POST: APIRoute = async ({ request }) => {
name: formData.get("name")?.toString() ?? "",
description: formData.get("description")?.toString() ?? "",
link: formData.get("link")?.toString() ?? "",
theme: formData.get("theme")?.toString() ?? "",
};

const insertion_msg = await supabase.from("projects").insert([data]).select();
Expand All @@ -57,15 +62,27 @@ export const POST: APIRoute = async ({ request }) => {
};

const validateForms = async (formData: FormData, errors: String[]) => {
const team_code = formData.get("team_code")?.toString().replace("#", "");
const link = formData.get("link")?.toString();

if (!team_code || !link) {
errors.push("All fields are required.");
return false
}

const valid = (await validateTeamCode(team_code, errors) && await validateLink(link, errors))

return valid;
};


const validateTeamCode = async (team_code: string, errors: String[]) => {
let valid = true;

// check if there's already a project with the same team code
// if there is one, return an error
const team_code = formData.get("team_code")?.toString().replace("#", "");
let { data: projects, error } = await supabase
.from("projects")
.select("*")
.eq("team_code", team_code);
.from("projects")
.select("*")
.eq("team_code", team_code);

if (error) {
errors.push(
Expand All @@ -78,4 +95,31 @@ const validateForms = async (formData: FormData, errors: String[]) => {
}

return valid;
};
}

const validateLink = async (link: string, errors: String[]) => {
const githubLinkRegex = /^https:\/\/github\.com\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)$/;

const match = link.match(githubLinkRegex);
if (!match) {
errors.push("The link must be a valid GitHub repository URL.");
return false;
}

const [, username, repositoryName] = match;

const response = await fetch(apiGithub + `${username}/${repositoryName}`);
if (!response.ok) {
errors.push("The repository doesn't exist or is private.");
return false;
}

const data = await response.json();
const creationDate = new Date(data.created_at);
if (creationDate < beginContestDate) {
errors.push("The repository must be created after the contest start date.");
return false;
}

return true;
}
File renamed without changes.
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface SubmitProjectItem {
name: string;
description: string;
link: string;
theme: string;
}

export interface SideBarOption {
Expand Down
11 changes: 11 additions & 0 deletions supabase/migrations/20250327150742_modify_projects_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Step 1: Drop the current primary key constraint
ALTER TABLE public.projects
DROP CONSTRAINT projects_pkey;

-- Step 2: Drop the id column
ALTER TABLE public.projects
DROP COLUMN id;

-- Step 3: Set the team_code column as the new primary key
ALTER TABLE public.projects
ADD CONSTRAINT projects_pkey PRIMARY KEY (team_code);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE public.projects
ADD COLUMN theme text;