Skip to content

Commit

Permalink
feat: /pets/:id fav
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanatpakornS committed Feb 25, 2024
1 parent 7939788 commit 5f338ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/components/Admin/Pets/Add/EditInfoAndSubmit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface EditInfoAndSubmitProps {
enableSubmit?: boolean;
isAdmin: boolean;
isFav?: boolean;
handleFavPressed?: () => void;
handleFavPressed?: (event: React.MouseEvent<HTMLButtonElement>) => void;
id?: string;
}

Expand Down Expand Up @@ -119,15 +119,18 @@ const EditInfoAndSubmit = (props: EditInfoAndSubmitProps) => {
</div>
) : (
<div className="ml-2">
<button type="button" id="like-button">
<button
type="button"
id="like-button"
onClick={props.handleFavPressed}
>
<Icon
icon={
props.isFav
? "ph:heart-straight-fill"
: "ph:heart-straight-bold"
}
className="h-6 w-6 cursor-pointer text-accent-red"
onClick={props.handleFavPressed}
/>
</button>
</div>
Expand Down
22 changes: 18 additions & 4 deletions src/components/Pets/Details/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import EditInfoAndSubmit, {
} from "@/components/Admin/Pets/Add/EditInfoAndSubmit";
import BigPetCard from "@/components/Pets/Details/BigPetCard";
import MainLayout from "@/layouts/MainLayout";
import useFavoriteStore from "@/store/favStore";
import { Pet } from "@/types/pets";
import { useState } from "react";

const Details = ({ isAdmin, data }: { isAdmin: boolean; data: Pet }) => {
const { favorites } = useFavoriteStore();
const [petInfo, setPetInfo] = useState<info>({
gender: data.gender as "male" | "female",
type: data.type as "dog" | "cat" | "-",
Expand All @@ -19,13 +21,23 @@ const Details = ({ isAdmin, data }: { isAdmin: boolean; data: Pet }) => {
});

const [isFav, setIsFav] = useState(false);
const addToFavorites = useFavoriteStore((state) => state.addToFavorites);
const removeFromFavorites = useFavoriteStore(
(state) => state.removeFromFavorites
);

const handleSubmit = () => {
console.log(petInfo);
};

const handleFavPressed = () => {
setIsFav(!isFav);
const handleFavPressed = (event: React.MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
if (isFav) {
removeFromFavorites(data.id);
} else {
addToFavorites(data.id);
}
setIsFav((prev) => !prev);
};

return (
Expand All @@ -37,8 +49,10 @@ const Details = ({ isAdmin, data }: { isAdmin: boolean; data: Pet }) => {
setValue={setPetInfo}
onSubmit={handleSubmit}
isAdmin={isAdmin}
isFav={isFav}
handleFavPressed={handleFavPressed}
isFav={favorites.find((fav) => fav === data.id) ? true : false}
handleFavPressed={(event: React.MouseEvent<HTMLButtonElement>) =>
handleFavPressed(event)
}
id={data.id}
/>
<img src={logo} alt="logo" className="hidden h-64 w-64 xl:block" />
Expand Down

0 comments on commit 5f338ab

Please sign in to comment.