Skip to content
Snippets Groups Projects
Commit ce643939 authored by Johann Ruiz's avatar Johann Ruiz
Browse files

Implemented showing subscriptions on profile page, and allowing subscribe and...

Implemented showing subscriptions on profile page, and allowing subscribe and unsubscribe on the lab page'
parent 96f11c54
No related branches found
No related tags found
1 merge request!2Subscribing
Pipeline #10794 canceled
This commit is part of merge request !2. Comments created here will be created in the context of that merge request.
import React from 'react'; import React from 'react';
import './LabPopup.css'; import './LabPopup.css';
import {subscriptionsAPI} from "../utils.ts";
import { ResearchLabItem } from '../types'; import { ResearchLabItem } from '../types';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import axios from 'axios'; import axios from 'axios';
...@@ -11,26 +12,28 @@ interface LabPopupProps { ...@@ -11,26 +12,28 @@ interface LabPopupProps {
} }
const LabPopup: React.FC<LabPopupProps> = ({ lab, onClose }) => { const LabPopup: React.FC<LabPopupProps> = ({ lab, onClose }) => {
const {student} = useStudentUserContext(); const {student, updateStudentSubscriptions} = useStudentUserContext();
if (!lab || !student) return null; if (!lab || !student) return null;
const handleSubscribe = async () => { const isSubscribed = student.subscriptions.includes(lab.name);
const subscriptionData = {
studentId: student.id,
labId: lab.id,
};
const handleSubscribe = async () => {
try { try {
const response = await axios.post(`/subscribe`, subscriptionData, { const endpoint = isSubscribed ? 'unsubscribe' : 'subscribe';
headers: { const response = await axios.post(`${subscriptionsAPI}/${endpoint}?studentId=${student.id}&labName=${lab.name}`);
'Content-Type': 'application/json' console.log(`${isSubscribed ? 'Unsubscribed' : 'Subscribed'} successfully:`, response.data);
// Update the local subscriptions list
if (response.status === 200) {
if (isSubscribed) {
updateStudentSubscriptions(student.subscriptions.filter(name => name !== lab.name));
} else {
updateStudentSubscriptions([...student.subscriptions, lab.name]);
} }
}); }
console.log('Subscribed successfully:', response.data);
// Optionally, you can add more logic here after successful subscription
} catch (error) { } catch (error) {
console.error('Error subscribing:', error); console.error(`Error ${isSubscribed ? 'unsubscribing' : 'subscribing'}:`, error);
} }
}; };
...@@ -61,7 +64,7 @@ const LabPopup: React.FC<LabPopupProps> = ({ lab, onClose }) => { ...@@ -61,7 +64,7 @@ const LabPopup: React.FC<LabPopupProps> = ({ lab, onClose }) => {
</button> </button>
</Link> </Link>
<button className="primary-button" onClick={handleSubscribe}> <button className="primary-button" onClick={handleSubscribe}>
Subscribe {isSubscribed ? 'Unsubscribe' : 'Subscribe'}
</button> </button>
</div> </div>
</div> </div>
......
...@@ -6,10 +6,12 @@ import {NO_EMAIL} from "../utils.ts"; ...@@ -6,10 +6,12 @@ import {NO_EMAIL} from "../utils.ts";
interface StudentUserContextType { interface StudentUserContextType {
student: StudentUser | null; student: StudentUser | null;
updateStudentSubscriptions: (subscriptions: string[]) => void;
} }
const StudentUserContext = createContext<StudentUserContextType>({ const StudentUserContext = createContext<StudentUserContextType>({
student: null student: null,
updateStudentSubscriptions: () => {} // Default no-op function
} }
); );
...@@ -32,13 +34,18 @@ export const StudentUserProvider = ({children}: React.PropsWithChildren) => { ...@@ -32,13 +34,18 @@ export const StudentUserProvider = ({children}: React.PropsWithChildren) => {
} }
}, [email, token]); }, [email, token]);
const updateStudentSubscriptions = (subscriptions: string[]) => {
setStudent(prevStudent => prevStudent ? { ...prevStudent, subscriptions } : null);
};
return ( return (
<StudentUserContext.Provider value={{student}}> <StudentUserContext.Provider value={{student, updateStudentSubscriptions}}>
{children} {children}
</StudentUserContext.Provider> </StudentUserContext.Provider>
); );
} }
export const useStudentUserContext = () => { export const useStudentUserContext = () => {
const context = useContext(StudentUserContext); const context = useContext(StudentUserContext);
if (!context) { if (!context) {
......
...@@ -14,6 +14,7 @@ interface ProfileUser { ...@@ -14,6 +14,7 @@ interface ProfileUser {
year?: string; // STUDENT year?: string; // STUDENT
aboutMe?: string; // STUDENT aboutMe?: string; // STUDENT
major?: MajorItem; // STUDENT major?: MajorItem; // STUDENT
subscriptions?: string[];
college?: string; // PROFESSOR college?: string; // PROFESSOR
isPending?: boolean; // PROFESSOR isPending?: boolean; // PROFESSOR
lab?: ResearchLabItem; // PROFESSOR lab?: ResearchLabItem; // PROFESSOR
...@@ -96,6 +97,18 @@ export default function ProfilePage() { ...@@ -96,6 +97,18 @@ export default function ProfilePage() {
</div> </div>
</> </>
)} )}
{role === STUDENT_ROLE && profileUser.subscriptions && (
<>
<div className='profile-section'>
<h2>Subscriptions</h2>
<ul>
{profileUser.subscriptions.map((sub, index) => (
<li key={index}>{sub}</li>
))}
</ul>
</div>
</>
)}
{role === PROFESSOR_ROLE && ( {role === PROFESSOR_ROLE && (
<div className='profile-section'> <div className='profile-section'>
<h2>Academic Information</h2> <h2>Academic Information</h2>
......
...@@ -10,6 +10,8 @@ export const collegesAPI = `${baseUrl}/colleges`; ...@@ -10,6 +10,8 @@ export const collegesAPI = `${baseUrl}/colleges`;
export const majorsAPI = `${baseUrl}/majors`; export const majorsAPI = `${baseUrl}/majors`;
export const studentsAPI = `${baseUrl}/students`; export const studentsAPI = `${baseUrl}/students`;
export const professorsAPI = `${baseUrl}/professors`; export const professorsAPI = `${baseUrl}/professors`;
export const subscriptionsAPI = `${baseUrl}/subscriptions`;
// ROUTES // ROUTES
export const welcomePagePath = '/'; export const welcomePagePath = '/';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment