Skip to content

Commit

Permalink
[wip] pass favorites into user favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccahongsf committed May 8, 2024
1 parent 768c181 commit 0bd742d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {IndexUiState} from "instantsearch.js/es/types/ui-state";
import useAccordion from "@lib/hooks/useAccordion";
import {RefinementListItem} from "instantsearch.js/es/connectors/refinement-list/connectRefinementList";
import {clsx} from "clsx";
import DefaultResult, {AlgoliaHit} from "@components/algolia-results/default";
import {AlgoliaHit} from "@components/algolia-results/default";
import SummerCourse from "@components/algolia-results/summer-course/summer-course";

type Props = {
appId: string
Expand Down Expand Up @@ -262,8 +263,8 @@ const HitList = () => {

<ul className="list-unstyled">
{hits.map(hit =>
<li key={hit.objectID} className="border-b border-gray-300 last:border-0">
<DefaultResult hit={hit}/>
<li key={hit.objectID}>
<SummerCourse hit={hit}/>
</li>
)}
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,14 @@ import {
ParagraphSumUserFavorite
} from "@lib/gql/__generated__/drupal.d";
import AlgoliaCourseList from "@components/patterns/algolia-course-list";


const favoriteCourseList = [
{
"url": "https://example.com/g76H32uJqR",
"title": "Race Against the Stars",
"summary": undefined,
"photo": "http://summer.lagunita.loc/sites/summer/files/media/image/temp-img_1.jpg",
"updated": 0,
"html": "This class introduces the core concepts and methods of Cultural and Social Anthropology. Through the ethnographic study of human societies, anthropology has emerged as a dynamic discipline that inquiries into the complexity of humanity. It has produced new kinds of inquiry into race, class, gender, history, power, language, economy, culture, and local, transnational, and global phenomena. This course will introduce students to anthropology’s unique approach to studying human culture and society and teach them core anthropological concepts. It will also present students with cross-cultural case studies on contemporary issues, including environmental problems and climate change, capitalism, gender and sexuality, race, immigration, and colonialism.",
"type": "",
"sum_course_availability": "Available",
"sum_course_catalog_number": "ASTRO-101",
"sum_course_class_number": "492",
"sum_course_course_cost": 7800,
"sum_course_cross_listing": "ASTRO-100",
"sum_course_description": "In turpis. Phasellus volutpat, metus eget egestas mollis, lacus lacus blandit dui, id egestas quam mauris ut lacus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas egestas arcu quis ligula mattis placerat. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"sum_course_end_date": "2024-05-17T11:09:28.440Z",
"sum_course_format": "Hybrid",
"sum_course_interest": ["Mathematics"],
"sum_course_length": "Short",
"sum_course_notes": "Vivamus consectetuer hendrerit lacus. Proin faucibus arcu quis ante. Nunc nonummy metus. Sed hendrerit. Maecenas vestibulum mollis diam.",
"sum_course_population": ["Graduate", "Part-time"],
"sum_course_prerequisites": "ASTRO-001",
"sum_course_schedule": "9:00 - 11:30am Monday, Wednesday, Friday",
"sum_course_start_date": "2023-08-14T04:33:14.440Z",
"sum_course_units": 3,
"sum_students_also_studied": 72
},
{
"url": "https://example.com/f94Z29cVqS",
"title": "Microbiology: Human Health & Society",
"summary": undefined,
"photo": "http://summer.lagunita.loc/sites/summer/files/media/image/temp-img_1.jpg",
"updated": 0,
"html": "This class introduces the core concepts and methods of Cultural and Social Anthropology. Through the ethnographic study of human societies, anthropology has emerged as a dynamic discipline that inquiries into the complexity of humanity. It has produced new kinds of inquiry into race, class, gender, history, power, language, economy, culture, and local, transnational, and global phenomena. This course will introduce students to anthropology’s unique approach to studying human culture and society and teach them core anthropological concepts. It will also present students with cross-cultural case studies on contemporary issues, including environmental problems and climate change, capitalism, gender and sexuality, race, immigration, and colonialism.",
"type": "",
"sum_course_availability": "Not Available",
"sum_course_catalog_number": "ASTRO-123",
"sum_course_class_number": "815",
"sum_course_course_cost": 4750,
"sum_course_cross_listing": "ASTRO-101",
"sum_course_description": "Quisque rutrum. Etiam iaculis nunc ac metus. Etiam sollicitudin, ipsum eu pulvinar rutrum, tellus ipsum laoreet sapien, quis venenatis ante odio sit amet eros. Quisque libero metus, condimentum nec, tempor a, commodo mollis, magna. Phasellus consectetuer vestibulum elit.",
"sum_course_end_date": "2023-11-30T05:07:42.015Z",
"sum_course_format": "Online",
"sum_course_interest": ["Science"],
"sum_course_length": "Long",
"sum_course_notes": "Etiam feugiat lorem non metus. Nulla neque dolor, sagittis eget, iaculis quis, molestie non, velit. Sed in libero ut nibh placerat accumsan. Phasellus volutpat, metus eget egestas mollis, lacus lacus blandit dui, id egestas quam mauris ut lacus. Donec orci lectus, aliquam ut, faucibus non, euismod id, nulla.",
"sum_course_population": ["Undergraduate", "Full-time"],
"sum_course_prerequisites": "ASTRO-001",
"sum_course_schedule": "9:00 - 11:30am Monday, Wednesday, Friday",
"sum_course_start_date": "2023-03-12T17:38:42.015Z",
"sum_course_units": 8,
"sum_students_also_studied": 58
}
]

import useFavorites from "@lib/hooks/useFavorites";

type Props = HtmlHTMLAttributes<HTMLDivElement> & {
paragraph: ParagraphSumUserFavorite
}

const UserFavoriteParagraph = async ({paragraph, ...props}: Props) => {
const UserFavoriteParagraph = async ({ paragraph, ...props }: Props) => {
const { favs } = useFavorites();
const siteSettingsConfig = await getConfigPage<StanfordBasicSiteSetting>("StanfordBasicSiteSetting")

if (!siteSettingsConfig?.suSiteAlgoliaId || !siteSettingsConfig.suSiteAlgoliaSearch || !siteSettingsConfig.suSiteAlgoliaIndex) {
Expand All @@ -79,12 +24,12 @@ const UserFavoriteParagraph = async ({paragraph, ...props}: Props) => {
<div>
{/* Favorites List */}
</div>
{/* {favoriteCourseList.map((course, i) => <SummerCourse hit={course} key={i} />)} */}
<AlgoliaCourseList
appId={siteSettingsConfig.suSiteAlgoliaId}
searchIndex={siteSettingsConfig.suSiteAlgoliaIndex}
searchApiKey={siteSettingsConfig.suSiteAlgoliaSearch}
itemUuids={["eb0fe1c4-e98d-4fda-8962-4faa627340e0", "e66bd9be-a47b-4df7-804f-b173839e12aa", "e2979287-eef5-46a6-bb1e-e15e4c0e8280"]}
itemUuids={favs}
// itemUuids={["eb0fe1c4-e98d-4fda-8962-4faa627340e0", "e66bd9be-a47b-4df7-804f-b173839e12aa", "e2979287-eef5-46a6-bb1e-e15e4c0e8280"]}
/>
</div>
)
Expand Down
8 changes: 6 additions & 2 deletions src/components/patterns/algolia-course-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const AlgoliaCourseList = ({appId, searchIndex, searchApiKey, itemUuids}: Props)
}

const HitList = () => {
const {hits} = useHits<HitType>({});
const { hits } = useHits<HitType>({});
const {currentRefinement: currentPage, pages, nbPages, refine: goToPage} = usePagination({padding: 2})

if (hits.length === 0) {
Expand All @@ -45,7 +45,11 @@ const HitList = () => {
return (
<div>
<ul className="list-unstyled">
{hits.map((hit, i) => <SummerCourse hit={hit} key={i} />)}
{hits.map(hit =>
<li key={hit.objectID}>
<SummerCourse hit={hit} />
</li>
)}
</ul>

{pages.length > 1 &&
Expand Down
46 changes: 23 additions & 23 deletions src/lib/hooks/useFavorites.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
"use client";

import {useCallback} from "react";
import {useLocalStorage} from "usehooks-ts";

type Favorite = {
uuid: string
title: string
}
import { useCallback } from "react";
import { useLocalStorage, useIsClient } from "usehooks-ts";

const useFavorites = (): {
favs: Favorite[],
addFav: (_uuid: string, _title: string) => void,
removeFav: (_uuid: string) => void
favs: string[];
addFav: (_uuid: string) => void;
removeFav: (_uuid: string) => void;
} => {
const [favs, setFavs] = useLocalStorage<Favorite[]>("favorites", [], {initializeWithValue: false})
const isClient = useIsClient();
const [favs, setFavs] = useLocalStorage<string[]>("favorites", [], { initializeWithValue: false });

const addFav = useCallback((uuid: string, title: string) => {
setFavs([...favs, {uuid, title}])
}, [favs, setFavs])
const addFav = useCallback(
(uuid: string) => {
setFavs([...favs, uuid ]);
},
[favs, setFavs]
);

const removeFav = useCallback((uuid: string) => {
const itemIndex = favs.findIndex(fav => fav.uuid === uuid);
const oldFavs = [...favs];
oldFavs.splice(itemIndex, 1);
setFavs(oldFavs);
}, [favs, setFavs])
const removeFav = useCallback(
(uuid: string) => {
const updatedFavs = favs.filter((fav) => fav !== uuid);
setFavs(updatedFavs);
},
[favs, setFavs]
);

return {favs, addFav, removeFav}
}
return { favs: isClient ? favs : [], addFav, removeFav };
};

export default useFavorites;
export default useFavorites;

0 comments on commit 0bd742d

Please sign in to comment.