-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Still WIP. Closes #1
- Loading branch information
Showing
49 changed files
with
274 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
prisma/migrations/20240902121412_favourite_resources/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
-- CreateTable | ||
CREATE TABLE "UserFavourites" ( | ||
"id" TEXT NOT NULL, | ||
"userId" TEXT NOT NULL, | ||
"resourceId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "UserFavourites_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "UserFavourites_userId_idx" ON "UserFavourites"("userId"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "UserFavourites_resourceId_idx" ON "UserFavourites"("resourceId"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { HeartFilledIcon, HeartIcon } from "@radix-ui/react-icons"; | ||
import { useMemo } from "react"; | ||
|
||
import { Button, type ButtonProps } from "@/components/ui/button"; | ||
import { useToast } from "@/components/ui/use-toast"; | ||
import { cn } from "@/lib/utils"; | ||
import { api } from "@/utils/api"; | ||
|
||
export const ResourceFavouriteButton = ({ | ||
resourceId, | ||
favouriteCount, | ||
showLabel, | ||
...buttonProps | ||
}: { | ||
resourceId: string; | ||
favouriteCount?: number; | ||
showLabel?: boolean; | ||
} & ButtonProps) => { | ||
const { toast } = useToast(); | ||
const { data: user, isLoading } = api.user.getUser.useQuery(); | ||
const utils = api.useUtils(); | ||
|
||
const isFavourite = useMemo(() => { | ||
return user?.favourites.some( | ||
(favourite) => favourite.resourceId === resourceId, | ||
); | ||
}, [user, resourceId]); | ||
|
||
const { mutate: setFavourite, isLoading: isSaving } = | ||
api.user.setFavourite.useMutation({ | ||
onSuccess: async () => { | ||
await utils.user.getUser.invalidate(); | ||
void utils.resource.invalidate(); | ||
}, | ||
onError: (e) => { | ||
const errorMessage = | ||
e.message ?? e.data?.zodError?.fieldErrors.content?.[0]; | ||
|
||
toast({ | ||
title: "Uh oh! Something went wrong.", | ||
variant: "destructive", | ||
description: | ||
errorMessage ?? | ||
"Failed to update favourite! Please try again later.", | ||
}); | ||
}, | ||
}); | ||
|
||
const label = useMemo( | ||
() => (isFavourite ? "Remove from favourites" : "Add to favourites"), | ||
[isFavourite], | ||
); | ||
|
||
return ( | ||
<Button | ||
{...buttonProps} | ||
title={label} | ||
onClick={() => { | ||
if (!user) { | ||
return toast({ | ||
title: "You must be logged in to favourite a resource.", | ||
variant: "destructive", | ||
}); | ||
} | ||
|
||
setFavourite({ | ||
resourceId, | ||
favourite: !isFavourite, | ||
}); | ||
}} | ||
disabled={isSaving || isLoading} | ||
variant={buttonProps.variant ?? "outline"} | ||
className={cn(buttonProps.className, "group")} | ||
data-testid="resource-favourite-button" | ||
> | ||
{isFavourite ? ( | ||
<HeartFilledIcon className="h-4 w-4 text-red-500 group-hover:text-black dark:group-hover:text-white" /> | ||
) : ( | ||
<HeartIcon className="h-4 w-4 text-muted-foreground group-hover:text-red-500" /> | ||
)} | ||
{favouriteCount !== undefined && ( | ||
<span className="ml-0.5 text-xs text-muted-foreground"> | ||
({favouriteCount}) | ||
</span> | ||
)} | ||
{showLabel && <span className="ml-2">{label}</span>} | ||
</Button> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Head from "next/head"; | ||
|
||
import { PageLayout } from "@/components/page-layout"; | ||
import { ResourceList } from "@/components/resource-list"; | ||
import { api } from "@/utils/api"; | ||
|
||
export default function MyFavouriteResources() { | ||
const queryResult = api.resource.getMyFavouriteResources.useQuery(); | ||
|
||
return ( | ||
<> | ||
<Head> | ||
<title>My Favourite Resources - ImprovDB</title> | ||
</Head> | ||
<PageLayout title="My Favourite Resources" authenticatedOnly> | ||
<ResourceList queryResult={queryResult} useFilters showFavourites /> | ||
</PageLayout> | ||
</> | ||
); | ||
} |
Oops, something went wrong.