From a225c15ccaaccecaa94b5ed361193efefe8c3f39 Mon Sep 17 00:00:00 2001 From: Mathys Date: Sun, 5 May 2024 13:20:41 -0400 Subject: [PATCH 1/4] feat: add functions to handle templates with localstorage --- frontend/app/select-template/page.tsx | 64 +++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/frontend/app/select-template/page.tsx b/frontend/app/select-template/page.tsx index d16e455..83d4d66 100644 --- a/frontend/app/select-template/page.tsx +++ b/frontend/app/select-template/page.tsx @@ -1,18 +1,56 @@ "use client" import { useState } from "react" +import Link from "next/link" import { IPageType, ITemplate } from "@/api/generated" +import { FilePlus2 } from "lucide-react" import { Button } from "@/components/ui/button" import Card from "@/components/SelectTemplate/Card" import SelectTemplateSideBar from "@/components/SelectTemplate/Sidebar/Sidebar" -import { FilePlus2 } from "lucide-react" -import Link from "next/link" export default function IndexPage() { const [template, setTemplates] = useState([]) const [pageType, setPageType] = useState(IPageType.NONE) + function getTemplateId(template: ITemplate): string { + return template.title + template.dateCreated + } + + function getFavoriteTemplates(): string[] { + const favoriteTemplatesJson = localStorage.getItem("favoriteTemplates") + if (favoriteTemplatesJson) { + return JSON.parse(favoriteTemplatesJson) as string[] + } else { + return [] + } + } + + function isFavoriteTemplate(templateId: string): boolean { + const favoriteTemplates = getFavoriteTemplates() + return favoriteTemplates.includes(templateId) + } + + function toggleFavoriteTemplate(templateId: string): void { + const favoriteTemplates = getFavoriteTemplates() + if (!favoriteTemplates.includes(templateId)) { + console.log(template) + favoriteTemplates.push(templateId) + localStorage.setItem( + "favoriteTemplates", + JSON.stringify(favoriteTemplates) + ) + } else { + const newFavoriteTemplates = favoriteTemplates.filter( + (id) => id !== templateId + ) + localStorage.setItem( + "favoriteTemplates", + JSON.stringify(newFavoriteTemplates) + ) + } + } + return (
-

Page Type

{Object.keys(IPageType).map((page: string, index: number) => (
- {template.length === 0 && (
@@ -63,12 +102,19 @@ export default function IndexPage() { {template.length !== 0 && (
{template.map((template: ITemplate, index: number) => ( - + ))}
)} -
-
+ + ) } From d479e7ce80dd81ab2389e7945c419242dcb36570 Mon Sep 17 00:00:00 2001 From: Mathys Date: Sun, 5 May 2024 13:21:10 -0400 Subject: [PATCH 2/4] feat: add handle favorite to the card --- .../components/SelectTemplate/Card/Card.tsx | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/frontend/components/SelectTemplate/Card/Card.tsx b/frontend/components/SelectTemplate/Card/Card.tsx index ee982e6..a749ed7 100644 --- a/frontend/components/SelectTemplate/Card/Card.tsx +++ b/frontend/components/SelectTemplate/Card/Card.tsx @@ -13,7 +13,12 @@ import CardDescriptions from "./CardDescriptions" import CardHeader from "./CardHeader" import CardTags from "./CardTags" -export const Card: React.FC> = ({ +export const Card: React.FC< + Omit & { + isFavoriteTemplate: boolean + toggleFavoriteTemplate: (template: string) => void + } +> = ({ title, author, contributors, @@ -23,10 +28,22 @@ export const Card: React.FC> = ({ folder, lastUpdated, tags, + isFavoriteTemplate, + toggleFavoriteTemplate, }) => { + function handleFavoriteTemplate() { + toggleFavoriteTemplate(title + dateCreated) + } + return ( - + From bda50ccb25738b7532929608e3a5e7a9c7bb812b Mon Sep 17 00:00:00 2001 From: Mathys Date: Sun, 5 May 2024 13:21:44 -0400 Subject: [PATCH 3/4] feat: add favorite icon to toggle in card header --- .../SelectTemplate/Card/CardHeader.tsx | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/frontend/components/SelectTemplate/Card/CardHeader.tsx b/frontend/components/SelectTemplate/Card/CardHeader.tsx index b04d4d2..57ef283 100644 --- a/frontend/components/SelectTemplate/Card/CardHeader.tsx +++ b/frontend/components/SelectTemplate/Card/CardHeader.tsx @@ -1,5 +1,6 @@ +import { useState } from "react" import { ITemplate } from "@/api/generated" -import { StarIcon } from "lucide-react" +import { Heart, StarIcon } from "lucide-react" import { Badge } from "@/components/ui/badge" import { @@ -10,14 +11,35 @@ import { import SocialButton from "@/components/common/SocialButton" const CardHeader: React.FC< - Pick -> = ({ featured, title, author }) => { + Pick & { + isFavoriteTemplate: boolean + handleFavoriteTemplate: () => void + } +> = ({ + featured, + title, + author, + isFavoriteTemplate, + handleFavoriteTemplate, +}) => { + const [isFavorite, setIsFavorite] = useState(isFavoriteTemplate) return ( -
- - {title} - +
+
+ + {title} + + { + handleFavoriteTemplate() + setIsFavorite(!isFavorite) + }} + /> +
{featured && (
From 9b4577b962ce0243ae614ed7eacea8b0125e3a33 Mon Sep 17 00:00:00 2001 From: Mathys Date: Sun, 5 May 2024 13:30:18 -0400 Subject: [PATCH 4/4] feat: sort templates by favorite --- frontend/app/select-template/page.tsx | 33 +++++++++++++++++++-------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/frontend/app/select-template/page.tsx b/frontend/app/select-template/page.tsx index 83d4d66..a92aadf 100644 --- a/frontend/app/select-template/page.tsx +++ b/frontend/app/select-template/page.tsx @@ -51,6 +51,17 @@ export default function IndexPage() { } } + function getSortedTemplatesByFavorite() { + return template.sort((a, b) => + isFavoriteTemplate(getTemplateId(a)) === + isFavoriteTemplate(getTemplateId(b)) + ? 0 + : isFavoriteTemplate(getTemplateId(a)) + ? -1 + : 1 + ) + } + return (
- {template.map((template: ITemplate, index: number) => ( - - ))} + {getSortedTemplatesByFavorite().map( + (template: ITemplate, index: number) => ( + + ) + )}
)}