-
Notifications
You must be signed in to change notification settings - Fork 0
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 #3 from thejoltjoker/client/favorites
Client/favorites
- Loading branch information
Showing
26 changed files
with
599 additions
and
192 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
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,50 @@ | ||
import { useFavoritesContext } from "../contexts/FavoritesContext"; | ||
import { ImageItem } from "../models/ImageItem"; | ||
import { FavoritesActionType } from "../reducers/FavoritesReducer"; | ||
import { createFavorite } from "../services/user.service"; | ||
|
||
type Props = { image: ImageItem }; | ||
|
||
const SaveImage = ({ image }: Props) => { | ||
const { favorites, dispatch } = useFavoritesContext(); | ||
|
||
// TODO Dynamically get userId | ||
const userId = "1"; | ||
const handleSave = async () => { | ||
try { | ||
await createFavorite(userId, image); | ||
dispatch({ | ||
type: FavoritesActionType.Add, | ||
payload: [image], | ||
}); | ||
} catch (error) { | ||
console.error("Error when saving image to favorites"); | ||
} | ||
}; | ||
|
||
return ( | ||
<button | ||
className={ | ||
favorites.find((img) => img.imageId === image.imageId) | ||
? "rounded-full bg-blue-500 p-1 text-white transition" | ||
: "rounded-full p-1 text-white transition group-hover:bg-blue-500" | ||
} | ||
onClick={handleSave} | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="0 0 24 24" | ||
fill="currentColor" | ||
className={`stroke h-6 w-6 translate-y-[1px] fill-none stroke-white stroke-2 group-hover:fill-white ${favorites.find((img) => img.imageId === image.imageId) && "fill-white"}`} | ||
> | ||
<path | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z" | ||
/> | ||
</svg> | ||
</button> | ||
); | ||
}; | ||
|
||
export default SaveImage; |
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,15 @@ | ||
import { Dispatch, createContext, useContext } from "react"; | ||
import { ImageItem } from "../models/ImageItem"; | ||
import { FavoritesAction } from "../reducers/FavoritesReducer"; | ||
|
||
interface FavoritesContextModel { | ||
favorites: ImageItem[]; | ||
dispatch: Dispatch<FavoritesAction>; | ||
} | ||
|
||
export const FavoritesContext = createContext<FavoritesContextModel>({ | ||
favorites: [], | ||
dispatch: () => {}, | ||
}); | ||
|
||
export const useFavoritesContext = () => useContext(FavoritesContext); |
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
import { ImageItem } from "./ImageItem"; | ||
|
||
export interface GetFavoritesResponse { | ||
favorites: ImageItem[]; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export interface ImageItem { | ||
imageId: string; | ||
title: string; | ||
snippet: string; | ||
contextLink: string; | ||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { UserFavorite } from "./UserFavorite"; | ||
import { ImageItem } from "./ImageItem"; | ||
|
||
export interface User { | ||
userId?: string; | ||
email: string; | ||
favorites: UserFavorite[]; | ||
favorites: ImageItem[]; | ||
} |
This file was deleted.
Oops, something went wrong.
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,26 @@ | ||
import { ImageItem } from "../models/ImageItem"; | ||
|
||
export enum FavoritesActionType { | ||
Set, | ||
Add, | ||
} | ||
|
||
export interface FavoritesAction { | ||
type: FavoritesActionType; | ||
payload: ImageItem[]; | ||
} | ||
|
||
export const FavoritesReducer = ( | ||
favorites: ImageItem[], | ||
action: FavoritesAction, | ||
) => { | ||
switch (action.type) { | ||
case FavoritesActionType.Set: { | ||
return [...action.payload]; | ||
} | ||
case FavoritesActionType.Add: { | ||
return [...favorites, ...action.payload]; | ||
} | ||
} | ||
throw new Error("Unknown action: " + action.type); | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.