Skip to content

Commit c93dc9a

Browse files
authored
Merge pull request #134 from enBloc-org/133/favourtie-button-state
133/favourtie button state
2 parents 8042d18 + 724db10 commit c93dc9a

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/components/popup-components/ManageFavouritesButton/ManageFavouritesButton.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
1-
import { useState } from "react"
1+
import { useEffect, useState } from "react"
22

33
import { sendToBackground } from "@plasmohq/messaging"
44

5+
import { UserFavourites, UserSession } from "~types/userTypes"
6+
import newStorage from "~utils/newStorage"
7+
58
const ManageFavouritesButton = ({ popupId }) => {
9+
const storage = newStorage()
610
const [status, setStatus] = useState<
711
"idle" | "loading" | "rejected"
812
>("idle")
913
const [isFavourite, setIsFavourite] = useState<boolean>(false)
1014

15+
useEffect(() => {
16+
const fetchFavourites = async () => {
17+
const userSession: UserSession = await storage.get(
18+
"arebyte-audience-session"
19+
)
20+
const {
21+
data,
22+
error
23+
}: { data: UserFavourites; error: string | null } =
24+
await sendToBackground({
25+
name: "fetchUserFavourites",
26+
body: {
27+
id: userSession.id,
28+
jwt: userSession.jwt
29+
}
30+
})
31+
32+
if (error) {
33+
console.error(error)
34+
return setStatus("rejected")
35+
}
36+
setIsFavourite(data.favourites.some(fav => fav.id === popupId))
37+
}
38+
39+
fetchFavourites()
40+
}, [])
41+
1142
const clickHandler = async () => {
1243
setStatus("loading")
1344
const { error } = await sendToBackground({
@@ -21,7 +52,7 @@ const ManageFavouritesButton = ({ popupId }) => {
2152

2253
if (error) {
2354
console.error(error)
24-
setStatus("rejected")
55+
return setStatus("rejected")
2556
}
2657

2758
setIsFavourite(!isFavourite)

src/utils/popup-utils/displayPopup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default async function displayPopup(popup: Popup) {
3232
type: "text",
3333
index: 0,
3434
popupInfo: {
35+
id: popup.id,
3536
artist_name: popup.artist_name,
3637
medium: popup.medium,
3738
work_title: popup.work_title,
@@ -99,6 +100,7 @@ export default async function displayPopup(popup: Popup) {
99100
type: "video",
100101
index: 0,
101102
popupInfo: {
103+
id: popup.id,
102104
artist_name: popup.artist_name,
103105
medium: popup.medium,
104106
work_title: popup.work_title,

0 commit comments

Comments
 (0)