Skip to content

Commit

Permalink
refactor: extract types to .types.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinWu098 committed Dec 5, 2024
1 parent 5029213 commit b84b215
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/app/search/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Suspense } from "react";
import { Search } from "@/components/search/Search";
import { Search } from "@/components/search/search";
import { UNIVERSITY_GE } from "@/lib/constants";
import { queryDatabase } from "@/lib/utils/query-db";

Expand Down
40 changes: 4 additions & 36 deletions src/components/search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,17 @@ import { SearchFilterDialog } from "@/components/search/filter/search-filter-dia
import { SearchFilterSortDropdown } from "@/components/search/filter/search-filter-sort-dropdown";
import { SearchBlurb } from "@/components/search/search-blurb";
import { SearchResults } from "@/components/search/search-results";
import type {
CourseObject,
FilterValues,
} from "@/components/search/search.types";
import { UNIVERSITY_GE } from "@/lib/constants";
import { useQueryState } from "nuqs";

import { filterData } from "../../lib/utils/filter";
import { SearchFilter } from "./filter/search-filter";
import { SearchSelect } from "./SearchSelect";

export interface CourseObject {
sendingInstitution: string;
courseCode: string;
courseName: string;
cvcId: string;
assistPath: string;
niceToHaves: string[];
units: number;
tuition: number;
startDate: number;
endDate: number;
async: boolean;
hasOpenSeats: boolean;
hasPrereqs: boolean;
instantEnrollment: boolean;
fulfillsGEs: FullFillsGE[];
articulatesTo: string[];
}

type FullFillsGE = {
category: string;
count: number;
};

export type FilterValues = {
format: boolean[];
enrollment: boolean[];
available: boolean[];
start: Date | undefined;
end: Date | undefined;
institution: string;
min: number;
max: number;
sort: string;
};

export function Search({
university: _university,
ge: _ge,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CourseObject } from "@/components/search/Search";
import type { CourseObject } from "@/components/search/search.types";
import {
Select,
SelectContent,
Expand Down
5 changes: 2 additions & 3 deletions src/components/search/filter/search-filter.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Dispatch, SetStateAction } from "react";
import { SearchFilterCheckbox } from "@/components/search/filter/search-filter-checkbox";
import { SearchFilterDateSelect } from "@/components/search/filter/search-filter-date-select";
import { SearchFilterInstitutionDropdown } from "@/components/search/filter/search-filter-institution-dropdown";

import { CourseObject, FilterValues } from "../Search";
import { SearchFilterCheckbox } from "./search-filter-checkbox";
import { CourseObject, FilterValues } from "@/components/search/search.types";

interface SearchFilterProps {
setFormat: Dispatch<SetStateAction<boolean[]>>;
Expand Down
8 changes: 5 additions & 3 deletions src/components/search/search-blurb.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { useCallback, useEffect, useState } from "react";

import { CourseObject, FilterValues } from "./Search";
import { useCallback, useEffect, useState } from "react";
import type {
CourseObject,
FilterValues,
} from "@/components/search/search.types";

interface SearchBlurbProps {
filterData: (
Expand Down
2 changes: 1 addition & 1 deletion src/components/search/search-results.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { CourseObject } from "@/components/search/search.types";
import { format } from "date-fns";
import { ExternalLinkIcon } from "lucide-react";
import LazyLoad from "react-lazy-load";

import { CourseObject } from "./Search";
import Tags from "./Tags";

const formatTime = (date: number) => {
Expand Down
35 changes: 35 additions & 0 deletions src/components/search/search.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export interface CourseObject {
sendingInstitution: string;
courseCode: string;
courseName: string;
cvcId: string;
assistPath: string;
niceToHaves: string[];
units: number;
tuition: number;
startDate: number;
endDate: number;
async: boolean;
hasOpenSeats: boolean;
hasPrereqs: boolean;
instantEnrollment: boolean;
fulfillsGEs: FullFillsGE[];
articulatesTo: string[];
}

type FullFillsGE = {
category: string;
count: number;
};

export type FilterValues = {
format: boolean[];
enrollment: boolean[];
available: boolean[];
start: Date | undefined;
end: Date | undefined;
institution: string;
min: number;
max: number;
sort: string;
};
2 changes: 1 addition & 1 deletion src/lib/utils/filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CourseObject, FilterValues } from "../../components/search/Search";
import { CourseObject, FilterValues } from "@/components/search/search.types";

export const startsAfter = (start: Date | undefined, result: CourseObject) => {
if (start === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/query-db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CourseObject } from "../../components/search/Search";
import { CourseObject } from "@/components/search/search.types";

export type DatabaseReturn = {
data: CourseObject[];
Expand Down
19 changes: 10 additions & 9 deletions tests/search-filters.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CourseObject, FilterValues } from "@/components/search/Search";
import { CourseObject, FilterValues } from "@/components/search/search.types";
import { endsBefore, filterData, startsAfter } from "@/lib/utils/filter";

import "@testing-library/jest-dom";

interface CourseData {
Expand Down Expand Up @@ -88,7 +89,7 @@ describe("Search Filters", () => {

test("default filter values do not throw error", async () => {
expect(() => filterData(data.data, defaultFilterValues)).not.toThrow(
Error,
Error
);
});

Expand Down Expand Up @@ -154,7 +155,7 @@ describe("Search Filters", () => {
institution: "placeholder sending institution 1",
});
expect(result[0].sendingInstitution).toEqual(
"placeholder sending institution 1",
"placeholder sending institution 1"
);
});
});
Expand Down Expand Up @@ -191,7 +192,7 @@ describe("Filter Utils' Time Utilities", () => {
sort: "Tuition",
});
expect(result[0].sendingInstitution).toEqual(
"placeholder sending institution 2",
"placeholder sending institution 2"
);
});
});
Expand All @@ -203,7 +204,7 @@ describe("Search Sorting", () => {
sort: "Alphabetical",
});
expect(result[0].sendingInstitution).toEqual(
"placeholder sending institution 1",
"placeholder sending institution 1"
);
});

Expand All @@ -213,7 +214,7 @@ describe("Search Sorting", () => {
sort: "Alphabetical",
});
expect(result[0].sendingInstitution).toEqual(
"placeholder sending institution 1",
"placeholder sending institution 1"
);
});

Expand All @@ -223,7 +224,7 @@ describe("Search Sorting", () => {
sort: "Tuition",
});
expect(result[0].sendingInstitution).toEqual(
"placeholder sending institution 2",
"placeholder sending institution 2"
);
});

Expand All @@ -233,7 +234,7 @@ describe("Search Sorting", () => {
sort: "Shortest Term",
});
expect(result[0].sendingInstitution).toEqual(
"placeholder sending institution 1",
"placeholder sending institution 1"
);
});

Expand All @@ -246,7 +247,7 @@ describe("Search Sorting", () => {
console.log(result);

expect(result[0].sendingInstitution).toEqual(
"placeholder sending institution 3",
"placeholder sending institution 3"
);
});
});

0 comments on commit b84b215

Please sign in to comment.