-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from bounswe/frontend/feature/151-recipe-page
Frontend/feature/151 recipe page
- Loading branch information
Showing
19 changed files
with
597 additions
and
99 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,17 @@ | ||
import { AlertCircle } from "lucide-react"; | ||
import { Alert, AlertDescription, AlertTitle } from "./ui/alert"; | ||
import { renderError } from "@/services/api/semanticBrowseFetcher"; | ||
|
||
export default function ErrorAlert< | ||
T extends Parameters<typeof renderError>[0], | ||
>({ error }: { error: T }) { | ||
return ( | ||
<div className="container flex flex-col gap-2 py-8"> | ||
<Alert variant="destructive"> | ||
<AlertCircle className="h-4 w-4" /> | ||
<AlertTitle>Error</AlertTitle> | ||
<AlertDescription>{renderError(error)}</AlertDescription> | ||
</Alert> | ||
</div> | ||
); | ||
} |
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,53 @@ | ||
import { render, fireEvent, screen } from "@testing-library/react"; | ||
import RatingInput from "./RatingInput"; | ||
import { beforeEach, describe, test, expect, vi } from "vitest"; | ||
|
||
describe("RatingInput", () => { | ||
let currentRating: number, setRating: (s: number) => void; | ||
|
||
beforeEach(() => { | ||
// Arrange | ||
currentRating = 3; | ||
setRating = vi.fn(); | ||
}); | ||
|
||
test("renders correct number of stars based on currentRating", () => { | ||
render(<RatingInput currentRating={currentRating} setRating={setRating} />); | ||
|
||
// Assert | ||
const filledStars = screen.getAllByLabelText(/filled star/i); | ||
const emptyStars = screen.getAllByLabelText(/empty star/i); | ||
expect(filledStars.length).toBe(currentRating); | ||
expect(emptyStars.length).toBe(5 - currentRating); | ||
}); | ||
|
||
test("updates tempRating on mouse enter and resets on mouse leave", () => { | ||
render(<RatingInput currentRating={currentRating} setRating={setRating} />); | ||
|
||
// Act | ||
const fourthStar = screen.getAllByLabelText(/empty star/i)[0]; | ||
fireEvent.mouseEnter(fourthStar); | ||
|
||
// Assert | ||
const filledStars = screen.getAllByLabelText(/filled star/i); | ||
expect(filledStars.length).toBe(4); | ||
|
||
// Act | ||
fireEvent.mouseLeave(fourthStar); | ||
|
||
// Assert | ||
const filledStarsAfterLeave = screen.getAllByLabelText(/filled star/i); | ||
expect(filledStarsAfterLeave.length).toBe(currentRating); | ||
}); | ||
|
||
test("calls setRating with correct value on click", () => { | ||
render(<RatingInput currentRating={currentRating} setRating={setRating} />); | ||
|
||
// Act | ||
const secondStar = screen.getAllByLabelText(/filled star/i)[1]; | ||
fireEvent.click(secondStar); | ||
|
||
// Assert | ||
expect(setRating).toHaveBeenCalledWith(2); | ||
}); | ||
}); |
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,48 @@ | ||
import { cn } from "@/lib/utils"; | ||
import { StarIcon } from "lucide-react"; | ||
import { useEffect, useState } from "react"; | ||
|
||
const ratingIcons = [1, 2, 3, 4, 5]; | ||
|
||
export default function RatingInput({ | ||
currentRating, | ||
setRating, | ||
}: { | ||
currentRating: number; | ||
setRating: (rating: number) => void; | ||
}) { | ||
// input with 5 stars that when hovered show the new rating | ||
const [tempRating, setTempRating] = useState(currentRating); | ||
const [hovering, setHovering] = useState(false); | ||
|
||
useEffect(() => { | ||
if (!hovering) setTempRating(currentRating); | ||
}, [hovering, currentRating]); | ||
|
||
return ( | ||
<div | ||
className="flex cursor-pointer" | ||
onMouseEnter={() => setHovering(true)} | ||
onMouseLeave={() => setHovering(false)} | ||
> | ||
{ratingIcons.map((rating) => ( | ||
<span | ||
className="px-1 py-2" | ||
onClick={() => setRating(rating)} | ||
key={rating} | ||
onMouseEnter={() => setTempRating(rating)} | ||
> | ||
<StarIcon | ||
aria-label={tempRating >= rating ? "filled star" : "empty star"} | ||
className={cn( | ||
"h-4 w-4", | ||
tempRating >= rating | ||
? "fill-yellow-500 text-yellow-500" | ||
: "fill-gray-400 text-gray-400", | ||
)} | ||
/> | ||
</span> | ||
))} | ||
</div> | ||
); | ||
} |
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
Oops, something went wrong.