Skip to content

Commit

Permalink
Merge branch 'AP/no-notifications-husky-page' of https://github.com/s…
Browse files Browse the repository at this point in the history
…andboxnu/searchneu into AP/no-notifications-husky-page
  • Loading branch information
cherman23 committed Dec 4, 2024
2 parents 77461c7 + 59c37ef commit 7ebacba
Showing 1 changed file with 140 additions and 127 deletions.
267 changes: 140 additions & 127 deletions pages/subscriptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,100 @@ import { gqlClient } from '../utils/courseAPIClient';
import useUserInfo from '../utils/useUserInfo';
import { EmptyCard } from '../components/SubscriptionsPage/EmptyCard';

async function fetchCourseNotifs(classMapping, courseIds) {
for (const courseId of courseIds) {
const result = await gqlClient.getCourseInfoByHash({
hash: courseId,
});

// Creates a string of subject and id like CS2500
const courseCode = result.classByHash.subject + result.classByHash.classId;

const subject = result.classByHash.subject;
const classId = result.classByHash.classId;
const host = result.classByHash.host;
const termId = result.classByHash.termId;

// The subscription page should only show sections that we can subscribe to.
// We identify such sections as those with less than 5 seats remaining.
const filteredSections = result.classByHash.sections
.filter((s) => {
return s.seatsRemaining <= 5;
})
.map((s) => {
return {
...s,
online: false,
subject: subject,
classId: classId,
host: host,
termId: termId,
};
});

classMapping.set(courseCode, {
subject: subject,
classId: classId,
termId: termId,
host: host,
name: result.classByHash.name,
lastUpdateTime: result.classByHash.lastUpdateTime,
sections: filteredSections,
});
}
}

async function fetchSectionNotifs(classMapping, sectionIds) {
for (const sectionId of sectionIds) {
const result = await gqlClient.getSectionInfoByHash({
hash: sectionId,
});
const courseCode =
result.sectionByHash.subject + result.sectionByHash.classId;

// If course has already been found in fetchCourseNotifs(), continue
if (!classMapping.has(courseCode)) {
const sectionHashSlice = sectionId.split('/');
const courseHash = sectionHashSlice.slice(0, -1).join('/');

const courseResult = await gqlClient.getCourseInfoByHash({
hash: courseHash,
});

const subject = courseResult.classByHash.subject;
const classId = courseResult.classByHash.classId;
const host = courseResult.classByHash.host;
const termId = courseResult.classByHash.termId;

const filteredSections = courseResult.classByHash.sections
.filter((s) => {
return s.seatsRemaining <= 5;
})
.map((s) => {
return {
...s,
online: false,
subject: subject,
classId: classId,
host: host,
termId: termId,
};
});
classMapping.set(courseCode, {
subject: subject,
classId: classId,
termId: termId,
host: host,
name: courseResult.classByHash.name,
lastUpdateTime: courseResult.classByHash.lastUpdateTime,
sections: filteredSections,
});
}
}
}

export default function SubscriptionsPage(): ReactElement {
const router = useRouter();
const {
userInfo,
isUserInfoLoading,
Expand All @@ -22,7 +115,6 @@ export default function SubscriptionsPage(): ReactElement {
const [isFetching, setIsFetching] = useState(true);
// is the user subscribed to at least one class
const [isSubscribed, setIsSubscribed] = useState(false);
const router = useRouter();

useEffect(() => {
if (isUserInfoLoading) {
Expand All @@ -36,105 +128,10 @@ export default function SubscriptionsPage(): ReactElement {
}

const classMapping = new Map<string, SubscriptionCourse>();

// This uses subscribed course ids to find the associated courses and their sections
const fetchCourseNotifs = async (): Promise<void> => {
for (const courseId of userInfo.courseIds) {
const result = await gqlClient.getCourseInfoByHash({
hash: courseId,
});

// Creates a string of subject and id like CS2500
const courseCode =
result.classByHash.subject + result.classByHash.classId;

const subject = result.classByHash.subject;
const classId = result.classByHash.classId;
const host = result.classByHash.host;
const termId = result.classByHash.termId;

// The subscription page should only show sections that we can subscribe to.
// We identify such sections as those with less than 5 seats remaining.
const filteredSections = result.classByHash.sections
.filter((s) => {
return s.seatsRemaining <= 5;
})
.map((s) => {
return {
...s,
online: false,
subject: subject,
classId: classId,
host: host,
termId: termId,
};
});
classMapping.set(courseCode, {
subject: subject,
classId: classId,
termId: termId,
host: host,
name: result.classByHash.name,
lastUpdateTime: result.classByHash.lastUpdateTime,
sections: filteredSections,
});
}
};

// This uses subscribed section ids to find the associated section and the associated course for that section
const fetchSectionNotifs = async (): Promise<void> => {
for (const sectionId of userInfo.sectionIds) {
const result = await gqlClient.getSectionInfoByHash({
hash: sectionId,
});
const courseCode =
result.sectionByHash.subject + result.sectionByHash.classId;

// If course has already been found in fetchCourseNotifs(), continue
if (!classMapping.has(courseCode)) {
const sectionHashSlice = sectionId.split('/');
const courseHash = sectionHashSlice.slice(0, -1).join('/');

const courseResult = await gqlClient.getCourseInfoByHash({
hash: courseHash,
});

const subject = courseResult.classByHash.subject;
const classId = courseResult.classByHash.classId;
const host = courseResult.classByHash.host;
const termId = courseResult.classByHash.termId;

const filteredSections = courseResult.classByHash.sections
.filter((s) => {
return s.seatsRemaining <= 5;
})
.map((s) => {
return {
...s,
online: false,
subject: subject,
classId: classId,
host: host,
termId: termId,
};
});
classMapping.set(courseCode, {
subject: subject,
classId: classId,
termId: termId,
host: host,
name: courseResult.classByHash.name,
lastUpdateTime: courseResult.classByHash.lastUpdateTime,
sections: filteredSections,
});
}
}
};

const fetchSubscriptions = async (): Promise<void> => {
try {
await fetchCourseNotifs();
await fetchSectionNotifs();
await fetchCourseNotifs(classMapping, userInfo.courseIds);
await fetchSectionNotifs(classMapping, userInfo.sectionIds);
setClasses(classMapping);
setIsFetching(false);
// are there classes the user is subscribed to?
Expand All @@ -146,8 +143,28 @@ export default function SubscriptionsPage(): ReactElement {
}
};
fetchSubscriptions();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [userInfo?.phoneNumber, isUserInfoLoading]); // Only depends on userInfo data
}, [userInfo?.phoneNumber, isUserInfoLoading]);

if (isFetching) {
return (
<>
<div>
<Header
title={`Subscriptions`}
// TODO (sam 11-04-2024: maybe we can get the previous campus/termid if we wnt to preserve this behavior)
campus={null}
termId={null}
searchData={null}
userInfo={userInfo}
onSignIn={onSignIn}
onSignOut={onSignOut}
/>
</div>

<PacmanLoader loading={isFetching} size={30} />
</>
);
}

return (
<>
Expand All @@ -163,36 +180,32 @@ export default function SubscriptionsPage(): ReactElement {
onSignOut={onSignOut}
/>
</div>
{isFetching ? (
<PacmanLoader loading={isFetching} size={30} />
) : (
{isSubscribed ? (
<>
{isSubscribed ? (
<>
<div className="Results_Container">
<div className="Results_MainWrapper">
<div className="Results_Main">
<h2>Subscriptions</h2>
{Array.from(classes).map(([courseCode, course]) => {
return (
<ClassCard
key={courseCode}
course={course}
sections={course.sections}
userInfo={userInfo}
fetchUserInfo={fetchUserInfo}
onSignIn={onSignIn}
/>
);
})}
</div>
</div>
<div className="Results_Container">
<div className="Results_MainWrapper">
<div className="Results_Main">
<h2>Subscriptions</h2>
{Array.from(classes)
.sort((a, b) => (a > b ? 1 : -1)) // Sort to ensure the sub order doesn't change
.map(([courseCode, course]) => {
return (
<ClassCard
key={courseCode}
course={course}
sections={course.sections}
userInfo={userInfo}
fetchUserInfo={fetchUserInfo}
onSignIn={onSignIn}
/>
);
})}
</div>
</>
) : (
<EmptyCard />
)}
</div>
</div>
</>
) : (
<EmptyCard />
)}
</>
);
Expand Down

0 comments on commit 7ebacba

Please sign in to comment.