Skip to content

Commit

Permalink
Merge pull request #107 from IEEEUCSC/main
Browse files Browse the repository at this point in the history
Teams List
  • Loading branch information
nsavinda authored Jan 30, 2024
2 parents 7a66bdd + 6106631 commit 0770350
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 2 deletions.
3 changes: 3 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
MONGO=
ORIGIN=
PORT=

AUTH_KEY=
35 changes: 35 additions & 0 deletions backend/src/Controllers/team.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import Team from "../Model/team";
import{ Request, Response } from "express";
import dotenv from "dotenv";


dotenv.config();

const AUTH_KEY = process.env.AUTH_KEY || "";


const expectTeamCount = 50;
Expand Down Expand Up @@ -98,3 +104,32 @@ export const count = (req: Request, res: Response): void => {
};



// get teams , autherization key is required

export const getTeams = async (req: Request, res: Response) => {
const authKey = req.headers.authorization;
if (authKey !== AUTH_KEY) {
res.status(401).json({
success: false,
message: "Unauthorized",
});
return;
}
try {
const teams = await Team.find({});
res.status(200).json({
success: true,
message: "Teams fetched successfully",
data: teams,
});
} catch (err) {
res.status(500).json({
success: false,
message: "Error fetching teams",
data: err,
});
}
};


3 changes: 2 additions & 1 deletion backend/src/Routes/team.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import express from "express";
import { register,count } from "../Controllers/team";
import { register,count, getTeams } from "../Controllers/team";

const router = express.Router();

router.post("/register", register);
router.get("/count", count);
router.get("/getTeams", getTeams);

export default router;
2 changes: 2 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const URL = process.env.MONGO || "mongodb://localhost:27017/";
const ORIGIN = process.env.ORIGIN || "http://localhost:3000";
const PORT = process.env.PORT || 4000;

const AUTH_KEY = process.env.AUTH_KEY || "123456";

const app: Express = express();
app.use(express.json());
app.use(cors(
Expand Down
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@types/react": "^18.2.43",
"@types/react-dom": "^18.2.17",
"@types/react-vertical-timeline-component": "^3.3.6",
"axios": "^1.6.7",
"bootstrap": "^5.3.2",
"framer-motion": "^10.16.16",
"react": "^18.2.0",
Expand Down
36 changes: 36 additions & 0 deletions frontend/pnpm-lock.yaml

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

7 changes: 6 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ import Intro from "./Components/Introduction/Intro";
import ContactUS from "./Components/ContactUs/ContactUs";


import Teams from './Components/Admin/Teams'


const App = () => {
return (
<div className="app-container">
<NavBar/>
<Routes>
<Route path="/register" element={<Registration/>} />
{/* <Route path="/register" element={<Registration/>} /> */}
<Route path="/team" element={<TeamRegistration/>} />

<Route path="/admin/teams/:key" element={<Teams/>} />

<Route path="/*" element={
<div>
<Hero/>
Expand Down
97 changes: 97 additions & 0 deletions frontend/src/Components/Admin/Teams.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { useNavigate, useParams } from 'react-router-dom';



interface Team {
teamName: string;
university: string;
other: string;
leaderName: string;
leaderYear: string;
leaderWhatsapp: string;
leaderEmail: string;
leaderNIC: string;
member1Name: string;
member1Year: string;
member1Whatsapp: string;
member1Email: string;
member1NIC: string;
member2Name: string;
member2Year: string;
member2Whatsapp: string;
member2Email: string;
member2NIC: string;

// not required
member3Name?: string;
member3Year?: string;
member3Whatsapp?: string;
member3Email?: string;
member3NIC?: string;

}






const Teams: React.FC = () => {
const { key } = useParams();
const [authKey, setAuthKey] = useState<string>('');
const [teams, setTeams] = useState<Team[]>([]);
const [error, setError] = useState<string>('');
const navigate = useNavigate();

useEffect(() => {
// Extracting the auth key from URL
console.log(key);
if (key) {
setAuthKey(key);
fetchTeams(key);

} else {
// Redirect to home page if auth key is not found
navigate('/');
}
}, [navigate]);

const fetchTeams = async (key: string) => {
try {
const response = await axios.get(process.env.REACT_APP_BACKEND_URL + '/api/team/getTeams', {
headers: {
Authorization: key
}
});
setTeams(response.data.data); // Assuming the data is in response.data.data
} catch (err: any) {
setError(err.message || 'Error fetching teams');
// Redirect to home page if auth key is wrong
navigate('/');
}
};

if (!authKey) {
return <div>Loading or no auth key present...</div>;
}

return (
<div className='pt-5'>
<div className="container mt-5 pt-5">
{/* Count */}
<h1>Count: {teams.length}</h1>
{error && <p>Error: {error}</p>}
<h1>Teams</h1>
<ul>
{teams.map((team, index) => (
<li key={index}>{team.teamName} - {team.university}</li>
))}
</ul>
</div>
</div>
);
};

export default Teams;

0 comments on commit 0770350

Please sign in to comment.