Skip to content

Commit

Permalink
updated solution for #19
Browse files Browse the repository at this point in the history
  • Loading branch information
garimabhayanaa committed Oct 8, 2024
1 parent ad2ddb5 commit e01b5cd
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 29 deletions.
6 changes: 5 additions & 1 deletion src/app/[username]/page.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use client';
import { generateUserTagline } from "../../../ai/generateTagline";
import { useSearchParams, useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import {contributions} from '../api/ai/route'

function UserPage({ params }) {
const { username } = params;
Expand Down Expand Up @@ -34,12 +36,14 @@ function UserPage({ params }) {
router.push(`/${username}${queryString}`);
};

const tagline=generateUserTagline(username,contributions);

return (
<div
className="flex flex-col justify-center items-center h-screen"
style={{ backgroundColor: theme, color: textColor }}
>
<h1 className="text-2xl font-bold mb-4">Welcome, {username}!</h1>
<h1 className="text-2xl font-bold mb-4">{tagline}!</h1>
<div className="flex space-x-4">
<div>
<label className="block mb-2">Background Color</label>
Expand Down
75 changes: 75 additions & 0 deletions src/app/api/ai/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { NextResponse } from "next/server";
const GITHUB_API_URL = "https://api.github.com/graphql";

export async function POST(req) {
const { username } = await req.json();

if (!username) {
return new NextResponse("Username required", { status: 400 });
}

const query = `
query ($login: String!) {
user(login: $login) {
login
contributionsCollection {
totalCommitContributions
contributionCalendar {
totalContributions
}
pullRequestContributionsByRepository {
repository {
name
}
totalCount
}
}
repositories(first: 100, orderBy: {field: STARGAZERS, direction: DESC}) {
nodes {
primaryLanguage {
name
}
}
}
}
}
`;

try {
const response = await fetch(GITHUB_API_URL, {
method: "POST",
headers: {
Accept: "application/json",
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
query,
variables: { login: username },
}),
});

if (!response.ok) {
return new NextResponse(await response.text(), {
status: response.status,
});
}

const data = await response.json();
console.log(data);
if (data.data.user) {
const contributions = data.data.user.contributionsCollection;
return NextResponse.json({
exists: true,
totalContributions: contributions.totalCommitContributions,
activeCodingDays: contributions.contributionCalendar.totalContributions
});
} else {
return NextResponse.json({ exists: false });
}
} catch (error) {
console.error("Error checking GitHub username:", error);
return new NextResponse("Internal Server Error", { status: 500 });
}
}
export default contributions;
29 changes: 1 addition & 28 deletions src/app/api/user/route.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { NextResponse } from "next/server";
import { generateUserTagline } from "../../../../ai/generateTagline";

const GITHUB_API_URL = "https://api.github.com/graphql";

Expand All @@ -14,25 +13,6 @@ export async function POST(req) {
query ($login: String!) {
user(login: $login) {
login
contributionsCollection {
totalCommitContributions
contributionCalendar {
totalContributions
}
pullRequestContributionsByRepository {
repository {
name
}
totalCount
}
}
repositories(first: 100, orderBy: {field: STARGAZERS, direction: DESC}) {
nodes {
primaryLanguage {
name
}
}
}
}
}
`;
Expand Down Expand Up @@ -60,14 +40,7 @@ export async function POST(req) {
const data = await response.json();
console.log(data);
if (data.data.user) {
const contributions = data.data.user.contributionsCollection;
const tagline = await generateUserTagline(username, contributions);
return NextResponse.json({
exists: true,
totalContributions: contributions.totalCommitContributions,
activeCodingDays: contributions.contributionCalendar.totalContributions,
tagline,
});
return NextResponse.json({ exists: true });
} else {
return NextResponse.json({ exists: false });
}
Expand Down

0 comments on commit e01b5cd

Please sign in to comment.