-
Notifications
You must be signed in to change notification settings - Fork 1
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 #672 from bounswe/frontend/feature/596/tagfollowbu…
…tton [Frontend] Folow/Unfollow Tags
- Loading branch information
Showing
3 changed files
with
111 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { | ||
useFollowTag, | ||
useGetTagDetails, | ||
useUnfollowTag, | ||
} from "@/services/api/programmingForumComponents"; | ||
import { useState } from "react"; | ||
import { Button } from "./ui/button"; | ||
|
||
export default function TagFollowButton({ | ||
tag, | ||
}: { | ||
tag: { tagId?: string; following?: boolean }; | ||
}) { | ||
const { isLoading, data, error, refetch } = useGetTagDetails( | ||
{ | ||
pathParams: { | ||
tagId: tag.tagId!, | ||
}, | ||
}, | ||
{ | ||
enabled: typeof tag.following !== "boolean", | ||
}, | ||
); | ||
|
||
const [optimisticFollowing, setOptimisticFollowing] = useState( | ||
null as boolean | null, | ||
); | ||
|
||
const { mutateAsync: follow } = useFollowTag({ | ||
onSuccess: () => { | ||
refetch().then(() => { | ||
setOptimisticFollowing(null); | ||
}); | ||
}, | ||
onError: () => { | ||
setOptimisticFollowing(null); | ||
}, | ||
}); | ||
const { mutateAsync: unfollow } = useUnfollowTag({ | ||
onSuccess: () => { | ||
refetch().then(() => { | ||
setOptimisticFollowing(null); | ||
}); | ||
}, | ||
onError: () => { | ||
setOptimisticFollowing(null); | ||
}, | ||
}); | ||
|
||
const following = optimisticFollowing ?? data?.data?.following; | ||
|
||
return ( | ||
<Button | ||
disabled={!!error || isLoading} | ||
variant={following && !isLoading ? "primary-outline" : "default"} | ||
onClick={() => { | ||
if (following) { | ||
unfollow({ | ||
pathParams: { | ||
tagId: tag.tagId!, | ||
}, | ||
}); | ||
setOptimisticFollowing(false); | ||
} else { | ||
follow({ | ||
pathParams: { | ||
tagId: tag.tagId!, | ||
}, | ||
}); | ||
setOptimisticFollowing(true); | ||
} | ||
}} | ||
> | ||
{isLoading | ||
? "Loading..." | ||
: error | ||
? "Error" | ||
: following | ||
? "Following" | ||
: "Follow"} | ||
</Button> | ||
); | ||
} |
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