FINISH: User Category Page
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { CircleMinus, CirclePlus } from "lucide-react";
|
import { CircleMinus, CirclePlus, Pencil } from "lucide-react";
|
||||||
import { useCategoryFollow } from "../../hooks/useCategoryFollow";
|
import { useCategoryFollow } from "../../hooks/useCategoryFollow";
|
||||||
|
|
||||||
|
|
||||||
interface FollowButtonProps {
|
interface FollowButtonProps {
|
||||||
category: {
|
category: {
|
||||||
category_id: number;
|
category_id: number;
|
||||||
@@ -11,6 +12,7 @@ interface FollowButtonProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const FollowButton: React.FC<FollowButtonProps> = ({ category }) => {
|
const FollowButton: React.FC<FollowButtonProps> = ({ category }) => {
|
||||||
|
const [mode, setMode] = useState<"pencil" | "minus" | "plus">("pencil");
|
||||||
const [isFollowing, setIsFollowing] = useState<boolean | null>(category.isFollowing);
|
const [isFollowing, setIsFollowing] = useState<boolean | null>(category.isFollowing);
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const { followCategory, unfollowCategory } = useCategoryFollow();
|
const { followCategory, unfollowCategory } = useCategoryFollow();
|
||||||
@@ -23,6 +25,17 @@ const FollowButton: React.FC<FollowButtonProps> = ({ category }) => {
|
|||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Cycle between modes
|
||||||
|
setMode((prevMode) => {
|
||||||
|
if (prevMode === "pencil") return "minus";
|
||||||
|
if (prevMode === "minus") return "plus";
|
||||||
|
return "minus";
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error toggling state:", error);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (isFollowing) {
|
if (isFollowing) {
|
||||||
await unfollowCategory(category.category_name);
|
await unfollowCategory(category.category_name);
|
||||||
@@ -40,13 +53,15 @@ const FollowButton: React.FC<FollowButtonProps> = ({ category }) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
className="absolute top-2 right-2 bg-black bg-opacity-50 p-2 rounded-full hover:bg-opacity-75 transition z-[9999]"
|
className="absolute top-2 right-2 bg-black bg-opacity-50 p-2 rounded-full hover:bg-opacity-75 transition z-[1]"
|
||||||
onClick={handleClick}
|
onClick={handleClick}
|
||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
>
|
>
|
||||||
{isLoading ? (
|
{isLoading ? (
|
||||||
<span className="text-white w-5 h-5">⏳</span>
|
<span className="text-white w-5 h-5">⏳</span>
|
||||||
) : isFollowing ? (
|
) : mode === "pencil" ? (
|
||||||
|
<Pencil className="text-white w-5 h-5" />
|
||||||
|
) : mode === "minus" ? (
|
||||||
<CircleMinus className="text-white w-5 h-5" />
|
<CircleMinus className="text-white w-5 h-5" />
|
||||||
) : (
|
) : (
|
||||||
<CirclePlus className="text-white w-5 h-5" />
|
<CirclePlus className="text-white w-5 h-5" />
|
||||||
@@ -54,5 +69,4 @@ const FollowButton: React.FC<FollowButtonProps> = ({ category }) => {
|
|||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default FollowButton;
|
export default FollowButton;
|
||||||
|
|||||||
@@ -1,14 +1,11 @@
|
|||||||
import React, { useState, useEffect, useRef } from "react";
|
import React, { useState, useEffect, useRef } from "react";
|
||||||
import { useAuth } from "../context/AuthContext";
|
import { useAuth } from "../context/AuthContext";
|
||||||
import { useNavigate, useParams } from "react-router-dom";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
import { CircleMinus, CirclePlus } from "lucide-react";
|
|
||||||
import DynamicPageContent from "../components/Layout/DynamicPageContent";
|
import DynamicPageContent from "../components/Layout/DynamicPageContent";
|
||||||
import { fetchContentOnScroll } from "../hooks/fetchContentOnScroll";
|
|
||||||
import { useCategoryFollow } from "../hooks/useCategoryFollow";
|
import { useCategoryFollow } from "../hooks/useCategoryFollow";
|
||||||
import { ListItemProps as StreamData } from "../components/Layout/ListItem";
|
|
||||||
import LoadingScreen from "../components/Layout/LoadingScreen";
|
|
||||||
import FollowButton from "../components/Input/FollowButton";
|
import FollowButton from "../components/Input/FollowButton";
|
||||||
|
|
||||||
|
|
||||||
interface Category {
|
interface Category {
|
||||||
isFollowing: boolean;
|
isFollowing: boolean;
|
||||||
category_id: number;
|
category_id: number;
|
||||||
@@ -30,7 +27,6 @@ const FollowedCategories: React.FC<FollowedCategoryProps> = ({ extraClasses = ""
|
|||||||
if (categoryName) checkCategoryFollowStatus(categoryName);
|
if (categoryName) checkCategoryFollowStatus(categoryName);
|
||||||
}, [categoryName]);
|
}, [categoryName]);
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isLoggedIn) return;
|
if (!isLoggedIn) return;
|
||||||
|
|
||||||
@@ -48,50 +44,6 @@ const FollowedCategories: React.FC<FollowedCategoryProps> = ({ extraClasses = ""
|
|||||||
fetchFollowedCategories();
|
fetchFollowedCategories();
|
||||||
}, [isLoggedIn]);
|
}, [isLoggedIn]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
toggleFollow;
|
|
||||||
})
|
|
||||||
|
|
||||||
const toggleFollow = async (categoryId: number, categoryName: string) => {
|
|
||||||
// Find the current category state from followedCategories
|
|
||||||
const category = followedCategories.find(cat => cat.category_id === categoryId);
|
|
||||||
if (!category) return;
|
|
||||||
|
|
||||||
const currentFollowState = category.isFollowing; // Use the actual state instead of isCategoryFollowing
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Set local state per category (independent button states)
|
|
||||||
setFollowedCategories(prevCategories =>
|
|
||||||
prevCategories.map(cat =>
|
|
||||||
cat.category_id === categoryId ? { ...cat, isLoading: true } : cat
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (currentFollowState) {
|
|
||||||
await unfollowCategory(categoryName);
|
|
||||||
} else {
|
|
||||||
await followCategory(categoryName);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update only the clicked category
|
|
||||||
setFollowedCategories(prevCategories =>
|
|
||||||
prevCategories.map(cat =>
|
|
||||||
cat.category_id === categoryId
|
|
||||||
? { ...cat, isFollowing: !currentFollowState, isLoading: false }
|
|
||||||
: cat
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error toggling follow state:", error);
|
|
||||||
|
|
||||||
// Reset loading state in case of error
|
|
||||||
setFollowedCategories(prevCategories =>
|
|
||||||
prevCategories.map(cat =>
|
|
||||||
cat.category_id === categoryId ? { ...cat, isLoading: false } : cat
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DynamicPageContent>
|
<DynamicPageContent>
|
||||||
|
|||||||
Reference in New Issue
Block a user