(e: React.MouseEvent)
| 31 | }, [added, type]); |
| 32 | |
| 33 | const handleClick = async (e: React.MouseEvent) => { |
| 34 | e.preventDefault(); // Prevent link navigation if inside a link |
| 35 | |
| 36 | if (!userId && !onWatchlistChange) { |
| 37 | console.error("WatchlistButton: userId or onWatchlistChange is required"); |
| 38 | toast.error("Please sign in to modify watchlist"); |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | const next = !added; |
| 43 | setAdded(next); // Optimistic update |
| 44 | setLoading(true); |
| 45 | |
| 46 | try { |
| 47 | if (userId) { |
| 48 | if (next) { |
| 49 | await addToWatchlist(userId, symbol, company); |
| 50 | toast.success(`${symbol} added to watchlist`); |
| 51 | } else { |
| 52 | await removeFromWatchlist(userId, symbol); |
| 53 | toast.success(`${symbol} removed from watchlist`); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Call external handler if provided (e.g. for UI refresh) |
| 58 | onWatchlistChange?.(symbol, next); |
| 59 | } catch (error) { |
| 60 | console.error("Watchlist action failed:", error); |
| 61 | setAdded(!next); // Revert on error |
| 62 | toast.error("Failed to update watchlist"); |
| 63 | } finally { |
| 64 | setLoading(false); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | if (type === "icon") { |
| 69 | return ( |
nothing calls this directly
no test coverage detected