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

Feature/56 switch to refactored endpoints #57

Merged
merged 3 commits into from
Sep 21, 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
9 changes: 4 additions & 5 deletions app/(main)/courses/[id]/@reviews/paginated-reviews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const ReviewsPage: React.FC<ReviewsPageProps> = ({
loadMore,
}) => {
const { data, error, isLoading } = useSWR<CourseReviewsRouteResponse>([
`/courses/${department}-${courseNumber}/reviews` +
`/courses/${department}/${courseNumber}/reviews` +
formatSearchParams({ page, limit, tags }),
{ headers: { 'ngrok-skip-browser-warning': '***' } },
]);
Expand Down Expand Up @@ -134,7 +134,7 @@ const PaginatedReviews: React.FC<{
// initial data used for populating tags;
// because of caching, shouldn't incur much overhead
const { data, error, isLoading } = useSWR<CourseReviewsRouteResponse>([
`/courses/${department}-${courseNumber}/reviews` +
`/courses/${department}/${courseNumber}/reviews` +
formatSearchParams({ page: 1, limit: 3, tags }),
{ headers: { 'ngrok-skip-browser-warning': '***' } },
]);
Expand Down Expand Up @@ -163,9 +163,8 @@ const PaginatedReviews: React.FC<{
>
{
data?.filters.tags.map((tag) => (
// TODO - put real value, not just -1
<TagCheckbox key={tag} value={tag} count={-1}>
{tag}
<TagCheckbox key={tag.tag} value={tag.tag} count={tag.count}>
{tag.tag}
</TagCheckbox>
)) as React.ReactNode[]
}
Expand Down
4 changes: 3 additions & 1 deletion app/(main)/courses/[id]/@schedules/paginated-schedules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
CourseSchedulesRouteParams,
CourseSchedulesRouteResponse,
} from '@/types/api/course/schedules';
import { formatSearchParams } from '@/utils/fetches';

interface SchedulePageProps
extends CourseSchedulesRouteParams,
Expand All @@ -27,7 +28,8 @@ const SchedulePage: React.FC<SchedulePageProps> = ({
limit,
}) => {
const { data, error } = useSWR<CourseSchedulesRouteResponse>([
`/courses/${department}-${courseNumber}/schedules`,
`/courses/${department}/${courseNumber}/schedules` +
formatSearchParams({ page, limit }),
{ headers: { 'ngrok-skip-browser-warning': '***' } },
]);

Expand Down
22 changes: 10 additions & 12 deletions app/(main)/courses/[id]/@summary/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Button from '@/components/button';
import InfoCard from '@/components/info-card';
import LineChart from '@/components/line-chart';
import SectionLabel from '@/components/section-label';
import Tag from '@/components/tag';
import { CourseReviewsStatsRouteResponse } from '@/types/api/course/reviews-stats';
import { CourseSummaryRouteResponse } from '@/types/api/course/summary';
import { formatResponse } from '@/utils/fetches';
import getEvaluation from '@/utils/get-evaluation';
Expand All @@ -28,11 +28,18 @@ export default async function Page({
const [department, courseNumber] = params.id.split('-');
const courseSummary: CourseSummaryRouteResponse = await fetch(
process.env.BACKEND_URL +
`/courses/${department.toUpperCase()}-${courseNumber}/summary`,
`/courses/${department.toUpperCase()}/${courseNumber}/summary`,
)
.then((res) => res.json())
.then(formatResponse);
if (!courseSummary) notFound();
const reviewsStats: CourseReviewsStatsRouteResponse = await fetch(
process.env.BACKEND_URL +
`/courses/${department.toUpperCase()}/${courseNumber}/reviews-stats`,
)
.then((res) => res.json())
.then(formatResponse);
if (!reviewsStats) notFound();

const {
satisfiesArea,
Expand All @@ -48,10 +55,9 @@ export default async function Page({
qualityDistribution,
gradeDistribution,
easeDistribution,
tags,
takeAgainPercent,
ratingDistribution,
} = courseSummary;
} = { ...courseSummary, ...reviewsStats };
// TODO - figure out what to do besides hardcode
const openSections = 0;
const totalSections = 0;
Expand Down Expand Up @@ -104,14 +110,6 @@ export default async function Page({
</div>
</div>

<div className="flex min-w-min flex-wrap justify-center gap-[10px]">
{(tags || []).map((tag) => (
<Tag key={tag} size="lg">
{tag}
</Tag>
))}
</div>

<div className="flex min-w-min flex-wrap justify-center gap-[10px] pt-[10px] text-button">
<Button variant="secondary" postfix={<ArrowTopRightOnSquareIcon />}>
Compare Course
Expand Down
4 changes: 2 additions & 2 deletions app/(main)/courses/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export default async function Page({
`/courses/search` +
formatSearchParams({
page: Number(searchParams?.page) || 1,
search: searchParams?.query,
department: searchParams?.departments,
query: searchParams?.query,
department: JSON.parse(searchParams?.departments || '[]'),
limit: 3,
/*
filters: {
Expand Down
8 changes: 4 additions & 4 deletions app/(main)/courses/search/results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ const Results: React.FC<{
{
searchResults?.filters.departments.map((value) => (
<TagCheckbox
key={value}
value={value}
count={-1} // TODO - add this data
key={value.department}
value={value.department}
count={value.count}
>
{value}
{value.department}
</TagCheckbox>
)) as React.ReactNode[]
}
Expand Down
8 changes: 3 additions & 5 deletions app/(main)/professors/[id]/@reviews/paginated-reviews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ const ReviewsPage: React.FC<ReviewsPageProps> = ({
title={`${department}${courseNumber}`}
href={`courses/${department}${courseNumber}`}
{...rest}
// TODO - get a real value here
overall={-1}
overall={rest.quality}
upvotes={rest.votes.upvotes}
userName={rest.username}
/>
Expand Down Expand Up @@ -165,9 +164,8 @@ const PaginatedReviews: React.FC<{
>
{
data?.filters.tags.map((tag) => (
// TODO - get an actual value here
<TagCheckbox key={tag} value={tag} count={-1}>
{tag}
<TagCheckbox key={tag.tag} value={tag.tag} count={tag.count}>
{tag.tag}
</TagCheckbox>
)) as React.ReactNode[]
}
Expand Down
13 changes: 10 additions & 3 deletions app/(main)/professors/[id]/@summary/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@/components/rating-summary';
import SectionLabel from '@/components/section-label';
import Tag from '@/components/tag';
import { ProfessorReviewsStatsRouteResponse } from '@/types/api/professor/reviews-stats';
import { ProfessorSummaryRouteResponse } from '@/types/api/professor/summary';
import { formatResponse } from '@/utils/fetches';
import getEvaluation from '@/utils/get-evaluation';
Expand All @@ -35,6 +36,12 @@ export default async function Page({
.then((res) => res.json())
.then(formatResponse);
if (!professorSummary) notFound();
const reviewsStats: ProfessorReviewsStatsRouteResponse = await fetch(
process.env.BACKEND_URL + `/professors/${params.id}/reviews-stats`,
)
.then((res) => res.json())
.then(formatResponse);
if (!reviewsStats) notFound();

const {
avgQuality,
Expand All @@ -51,7 +58,7 @@ export default async function Page({
gradeDistribution,
totalReviews,
takeAgainPercent,
} = professorSummary;
} = { ...professorSummary, ...reviewsStats };

const type = searchParams.sort;

Expand Down Expand Up @@ -93,8 +100,8 @@ export default async function Page({

<div className="flex min-w-min flex-wrap justify-center gap-[10px]">
{tags.map((tag) => (
<Tag key={tag} size="lg">
{tag}
<Tag key={tag.tag} size="lg">
{tag.tag}
</Tag>
))}
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/(main)/professors/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default async function Page({
formatSearchParams({
page: Number(searchParams?.page) || 1, // 1-indexed
limit: 3,
search: searchParams?.query,
query: searchParams?.query,
/*filters: {
search: searchParams?.query,
sort: searchParams?.sort as any,
Expand Down
2 changes: 1 addition & 1 deletion app/(main)/professors/search/results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const Results: React.FC<{
<PaginationButtons
totalPages={
searchResults?.totalResults
? Math.ceil(searchResults?.totalResults / 4)
? Math.ceil(searchResults?.totalResults / 3)
: 0
}
/>
Expand Down
24 changes: 24 additions & 0 deletions app/mock-api/courses/reviews-stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
CourseReviewsStatsRouteParams,
CourseReviewsStatsRouteResponse,
} from '@/types/api/course/reviews-stats';

export const response = ({
courseNumber,
department,
}: CourseReviewsStatsRouteParams): CourseReviewsStatsRouteResponse | null => {
return {
department: department.toUpperCase(),
courseNumber,
avgEase: 4.5,
avgQuality: 4.5,
avgGrade: 'A',
avgRating: 4.5,
totalReviews: 2,
takeAgainPercent: 100,
gradeDistribution: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
ratingDistribution: [1, 1, 1, 1, 1],
qualityDistribution: [1, 1, 1, 1, 1],
easeDistribution: [1, 1, 1, 1, 1],
};
};
4 changes: 3 additions & 1 deletion app/mock-api/courses/reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ export const response: FakeResponseFunctionType<
filters: {
//search: filters?.search ?? '',
//sort: filters?.sort ?? 'relevant',
tags: Array.from(tagFilters.entries()).map(([tag, count]) => tag),
tags: Array.from(tagFilters.entries()).map(([tag, count]) => {
return { tag, count };
}),
//professors: Array.from(professorFilters.entries()).map(([id, count]) => ({
// id,
// name: professorNames.get(id) || '',
Expand Down
6 changes: 4 additions & 2 deletions app/mock-api/courses/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const response: FakeResponseFunctionType<
CourseSearchRouteBody
> = (
{},
{ limit, page, search, department },
{ limit, page, query, department },
): CourseSearchRouteResponse | null => {
const result = courses.filter((course) => {
/*if (filters?.search) {
Expand Down Expand Up @@ -269,7 +269,9 @@ export const response: FakeResponseFunctionType<
},*/
filters: {
departments: Array.from(departmentFilters.entries()).map(
([department, count]) => department,
([department, count]) => {
return { department, count };
},
),
},
...getPaginatedItems<CourseSearch>({
Expand Down
11 changes: 0 additions & 11 deletions app/mock-api/courses/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ const courses: CourseSummaryRouteResponse[] = [
satisfiesArea: 'Area G',
//openSections: 3,
//totalSections: 5,
avgQuality: 4,
avgEase: 1,
avgRating: 3,
avgGrade: 'A-',
ratingDistribution: [11, 5, 1, 7, 23],
qualityDistribution: [4, 3, 2, 9, 29],
easeDistribution: [2, 15, 11, 20, 9],
gradeDistribution: [2, 15, 11, 20, 9, 0, 0, 0, 0, 0, 0, 0, 0],
totalReviews: 47,
takeAgainPercent: 52,
tags: ['Tough Grader', 'Test Heavy'],
},
];

Expand Down
26 changes: 26 additions & 0 deletions app/mock-api/professors/reviews-stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
ProfessorReviewsStatsRouteParams,
ProfessorReviewsStatsRouteResponse,
} from '@/types/api/professor/reviews-stats';
import { FakeResponseFunctionType } from '@/utils/fake-fetch';

export const response: FakeResponseFunctionType<
ProfessorReviewsStatsRouteParams
> = ({}): ProfessorReviewsStatsRouteResponse | null => {
return {
avgEase: 4.5,
avgGrade: 'A',
avgQuality: 4.5,
avgRating: 4.5,
totalReviews: 2,
takeAgainPercent: 100,
tags: [
{ tag: 'Accessible Outside Class', count: 1 },
{ tag: 'Amazing Lectures', count: 1 },
],
ratingDistribution: [1, 1, 1, 1, 1],
qualityDistribution: [1, 1, 1, 1, 1],
easeDistribution: [1, 1, 1, 1, 1],
gradeDistribution: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
};
};
4 changes: 3 additions & 1 deletion app/mock-api/professors/reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ export const response: FakeResponseFunctionType<
return {
totalResults: result.length,
filters: {
tags: Array.from(tagFilters.entries()).map(([tag, count]) => tag),
tags: Array.from(tagFilters.entries()).map(([tag, count]) => {
return { tag, count };
}),
},
/*filters: {
search: filters?.search ?? '',
Expand Down
6 changes: 3 additions & 3 deletions app/mock-api/professors/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ const professors: ProfessorSearch[] = [
export const response: FakeResponseFunctionType<
ProfessorSearchRouteParams,
ProfessorSearchRouteBody
> = ({}, { search, page, limit }): ProfessorSearchRouteResponse | null => {
> = ({}, { query, page, limit }): ProfessorSearchRouteResponse | null => {
const result = professors.filter((professor) => {
if (search) {
if (query) {
const wordsArray = professor.name
.toLowerCase()
.replace(/[^a-zA-Z0-9 ]/g, ' ')
.trim()
.split(/\s+/);
const searchQuery = search.toLowerCase();
const searchQuery = query.toLowerCase();
if (fuzzySearch(searchQuery, wordsArray, 2).length === 0) {
return false;
}
Expand Down
22 changes: 0 additions & 22 deletions app/mock-api/professors/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,11 @@ const professors: ProfessorSummaryRouteResponse[] = [
id: '2',
name: 'Jahan Ghofraniha',
email: '[email protected]',
avgQuality: 2.6,
avgEase: 2.8,
avgRating: 2.4,
avgGrade: 'A-',
ratingDistribution: [11, 5, 1, 7, 23],
qualityDistribution: [4, 3, 2, 9, 29],
easeDistribution: [2, 15, 11, 20, 9],
gradeDistribution: [2, 15, 11, 20, 9, 0, 0, 0, 0, 0, 0, 0, 0],
totalReviews: 47,
takeAgainPercent: 52,
tags: ['Tough Grader', 'So Many Papers'],
},
{
id: '3',
name: 'Kurt Mammen',
email: '[email protected]',
avgQuality: 1.6,
avgEase: 5,
avgRating: 5,
avgGrade: 'B-',
ratingDistribution: [11, 5, 1, 7, 23],
qualityDistribution: [4, 3, 2, 9, 29],
easeDistribution: [2, 15, 11, 20, 9],
gradeDistribution: [2, 15, 11, 20, 9, 0, 0, 0, 0, 0, 0, 0, 0],
totalReviews: 60,
takeAgainPercent: 90,
tags: ['Tough Grader'],
},
];

Expand Down
Loading
Loading