Skip to content
Open
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 next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" />
import "./.next/types/routes.d.ts";
import "./.next/dev/types/routes.d.ts";

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

133 changes: 133 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ html {
--text-muted: #999;
--dropdown-border: #454545;
--tab-inactive: #3F3F46;
--form-card-bg: #27262B;
--form-input-white: #3f3f46;
--success-green: #22C55E;
}

Expand All @@ -48,6 +50,8 @@ html {
--text-muted: #555;
--dropdown-border: #ccc;
--tab-inactive: #e0e0e0;
--form-card-bg: #fff;
--form-input-white: #e0e0e0;
--success-green: #16A34A;
}

Expand All @@ -60,6 +64,135 @@ body {
margin: 0;
}

.add-subject-container {
max-width: 800px;
margin: 0 auto;
width: 100%;
padding-bottom: 40px;
}

.form-title {
text-align: center;
margin-bottom: 40px;
font-weight: 800;
font-size: 2rem;
color: var(--text-color);
}

.subject-form-layout {
display: flex;
flex-direction: column;
gap: 20px;
}

.form-section-card {
background: var(--form-card-bg);
border: 1px solid var(--border-color);
padding: 24px;
border-radius: 8px;
display: flex;
flex-direction: column;
color: #fff;
box-sizing: border-box;
}

.light-mode .form-section-card {
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.form-label {
font-weight: 700;
font-size: 1.2rem;
margin-bottom: 8px;
display: block;
color: var(--text-color);
}

.form-description {
font-size: 0.9rem;
line-height: 1.5;
color: #818181;
margin-bottom: 16px;
}

.form-white-input {
background: var(--form-input-white);
border: none;
border-radius: 4px;
height: 48px;
padding: 0 16px;
font-size: 1rem;
width: 100%;
outline: none;
transition: box-shadow 0.2s;
color: var(--text-color);
box-sizing: border-box;
}

.form-white-input:focus {
box-shadow: 0 0 0 2px var(--primary-purple);
}


.input-error {
border: 2px solid #ff4d4f;
}

.input-error:focus {
box-shadow: 0 0 0 2px rgba(255, 77, 79, 0.3);
}


.weights-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 12px;
}

.weight-item {
display: flex;
flex-direction: column;
gap: 8px;
}

.weight-label {
font-size: 0.85rem;
font-weight: 600;
color: var(--text-color);
}

.weight-input {
text-align: center;
padding: 0;
}

.form-actions {
display: flex;
justify-content: center;
margin-top: 20px;
}

.btn-submit-form {
background: var(--primary-purple);
color: white;
border: none;
padding: 14px 40px;
border-radius: 8px;
font-weight: 800;
font-size: 1.1rem;
cursor: pointer;
transition: transform 0.1s, opacity 0.2s;
}

.btn-submit-form:hover {
opacity: 0.9;
transform: translateY(-1px);
}

.btn-submit-form:active {
transform: translateY(0);
}

html.light-mode,
body.light-mode {
background-color: #ffffff !important;
Expand Down
130 changes: 130 additions & 0 deletions src/app/api/create-course-pr/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { NextRequest, NextResponse } from "next/server";
const owner = process.env.GITHUB_OWNER!;
const repo = process.env.GITHUB_REPO!;
const token = process.env.GITHUB_TOKEN!;
const baseBranch = process.env.GITHUB_BASE_BRANCH || "main";

const filePath = "src/assets/courses_weighted.json";

export async function POST(req: NextRequest) {
try {
const newCourse = await req.json();

const headers = {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github+json",
};

const fileRes = await fetch(
`https://api.github.com/repos/${owner}/${repo}/contents/${filePath}?ref=${baseBranch}`,
{ headers }
);

if (!fileRes.ok) {
const err = await fileRes.text();
throw new Error("Cannot read file: " + err);
}

const fileData = await fileRes.json();
const sha = fileData.sha;

const content = Buffer.from(fileData.content, "base64").toString("utf-8");

try {
const parsed = JSON.parse(content);
if (
Array.isArray(parsed) &&
parsed.some((c: any) => c.courseCode === newCourse.courseCode)
) {
return NextResponse.json(
{ error: "Course đã tồn tại" },
{ status: 400 }
);
}
} catch {
throw new Error("File JSON không hợp lệ");
}

let updatedText = content.trim();

const newItemText = JSON.stringify(newCourse, null, 2);

if (updatedText.endsWith("]")) {
updatedText =
updatedText.slice(0, -1).trimEnd() +
(updatedText.length > 2 ? ",\n" : "\n") +
newItemText +
"\n]";
} else {
throw new Error("File không phải JSON array");
}

const updatedContent = Buffer.from(updatedText).toString("base64");

const branchName = `add-course-${Date.now()}`;

const refRes = await fetch(
`https://api.github.com/repos/${owner}/${repo}/git/ref/heads/${baseBranch}`,
{ headers }
);

const refData = await refRes.json();

await fetch(
`https://api.github.com/repos/${owner}/${repo}/git/refs`,
{
method: "POST",
headers,
body: JSON.stringify({
ref: `refs/heads/${branchName}`,
sha: refData.object.sha,
}),
}
);

const updateRes = await fetch(
`https://api.github.com/repos/${owner}/${repo}/contents/${filePath}`,
{
method: "PUT",
headers,
body: JSON.stringify({
message: `Add course ${newCourse.courseCode}`,
content: updatedContent,
sha,
branch: branchName,
}),
}
);

if (!updateRes.ok) {
const err = await updateRes.text();
throw new Error("Commit failed: " + err);
}

const prRes = await fetch(
`https://api.github.com/repos/${owner}/${repo}/pulls`,
{
method: "POST",
headers,
body: JSON.stringify({
title: `Add course ${newCourse.courseCode}`,
head: branchName,
base: baseBranch,
body: "Added via web form",
}),
}
);

const prData = await prRes.json();

return NextResponse.json({
url: prData.html_url,
});
} catch (err: any) {
console.error(err);
return NextResponse.json(
{ error: err.message || "Failed to create PR" },
{ status: 500 }
);
}
}
Loading