Skip to content
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

Feat: fetch session occurrences in single call #65

Merged
merged 1 commit into from
Nov 26, 2024
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
13 changes: 10 additions & 3 deletions api/afdb/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import getFetchConfig from '../fetchConfig';
const url = process.env.AF_DB_SERVICE_URL;
const bearerToken = process.env.AF_DB_SERVICE_BEARER_TOKEN || '';

export const getSessionOccurrences = async (sessionId: string) => {
export const getSessionOccurrences = async (sessionIds: string[]) => {
try {
const queryParams = new URLSearchParams();
if (sessionId) queryParams.append('session_id', sessionId.toString());
const urlWithParams = `${url}/session-occurrence?${queryParams.toString()}&is_start_time=today`;

if (sessionIds && sessionIds.length > 0) {
queryParams.append('session_ids', sessionIds.join(','));
}

queryParams.append('is_start_time', 'today');

const urlWithParams = `${url}/session-occurrence?${queryParams.toString()}`;

const response = await fetch(urlWithParams, getFetchConfig(bearerToken));

if (!response.ok) {
Expand Down
35 changes: 10 additions & 25 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,40 +87,25 @@ export default function Home() {
shouldFetchQuizzes ? fetchUserSession(userDbId!, true) : Promise.resolve([])
]);

if (quizSessionData.length > 0) {
const quizData = await Promise.all(quizSessionData.map(async (quiz: Session) => {
const sessionOccurrenceData = await getSessionOccurrences(quiz.session_id);
if (!sessionOccurrenceData) return null;
return sessionOccurrenceData;
}));

const flattenedQuizData = quizData.flat();
const quizSessions = flattenedQuizData.filter(flattenedSessionsData =>
flattenedSessionsData.session.platform === 'quiz'
);
setQuizzes(quizSessions);
}
const sessionIds = [...liveSessionData, ...quizSessionData].map(session => session.session_id);

if (liveSessionData.length > 0) {
const sessionsData = await Promise.all(liveSessionData.map(async (liveSession: Session) => {
const sessionOccurrenceData = await getSessionOccurrences(liveSession.session_id);
if (!sessionOccurrenceData) return null;
return sessionOccurrenceData;
}));
const sessionOccurrences = await getSessionOccurrences(sessionIds);

const flattenedSessionsData = sessionsData.flat();
const liveSessions = flattenedSessionsData.filter(flattenedSessionsData =>
flattenedSessionsData.session.platform === 'meet'
);
setLiveClasses(liveSessions);
}
const quizSessions = sessionOccurrences
.filter((sessionOccurence: SessionOccurrence) => sessionOccurence.session.platform === 'quiz');
setQuizzes(quizSessions);

const liveSessions = sessionOccurrences
.filter((sessionOccurence: SessionOccurrence) => sessionOccurence.session.platform === 'meet');
setLiveClasses(liveSessions);

MixpanelTracking.getInstance().identify(userId!);
} catch (error) {
console.error("Error fetching user sessions:", error);
}
};


const renderLiveClasses = () => {
if (liveClasses.length === 0) {
return <MessageDisplay message="No more live classes are scheduled for today!" />;
Expand Down
Loading