forked from realworld-apps/angular-realworld-example-app
-
Notifications
You must be signed in to change notification settings - Fork 0
chore(angular-react): Migrate FavoriteButtonComponent to React #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
3
commits into
main
Choose a base branch
from
devin/1775661995-angular-to-react-favorite-button
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,105 @@ | ||
| import React, { useState, useCallback } from 'react'; | ||
|
|
||
| interface Article { | ||
| slug: string; | ||
| title: string; | ||
| description: string; | ||
| body: string; | ||
| tagList: string[]; | ||
| createdAt: string; | ||
| updatedAt: string; | ||
| favorited: boolean; | ||
| favoritesCount: number; | ||
| author: { | ||
| username: string; | ||
| bio: string; | ||
| image: string; | ||
| following: boolean; | ||
| }; | ||
| } | ||
|
|
||
| interface FavoriteButtonProps { | ||
| article: Article; | ||
| children?: React.ReactNode; | ||
| /** Called after a successful toggle. Receives the new `favorited` state. */ | ||
| onToggle?: (favorited: boolean) => void; | ||
| /** Whether the current user is authenticated. */ | ||
| isAuthenticated?: boolean; | ||
| /** Callback to navigate when user is not authenticated. */ | ||
| onNavigateToRegister?: () => void; | ||
| /** | ||
| * Service function to favorite an article by slug. | ||
| * TODO: Wire up to actual ArticlesService when integrating. | ||
| */ | ||
| onFavorite?: (slug: string) => Promise<void>; | ||
| /** | ||
| * Service function to unfavorite an article by slug. | ||
| * TODO: Wire up to actual ArticlesService when integrating. | ||
| */ | ||
| onUnfavorite?: (slug: string) => Promise<void>; | ||
| } | ||
|
|
||
| /** | ||
| * React equivalent of the Angular `FavoriteButtonComponent`. | ||
| * | ||
| * Renders a favorite/unfavorite button for an article. | ||
| * Uses `<ng-content>` equivalent via `props.children`. | ||
| * Mirrors the Angular signal-based `isSubmitting` state with `useState`. | ||
| */ | ||
| export const FavoriteButton: React.FC<FavoriteButtonProps> = ({ | ||
| article, | ||
| children, | ||
| onToggle, | ||
| isAuthenticated = false, | ||
| onNavigateToRegister, | ||
| onFavorite, | ||
| onUnfavorite, | ||
| }) => { | ||
| const [isSubmitting, setIsSubmitting] = useState(false); | ||
|
|
||
| const toggleFavorite = useCallback(async () => { | ||
| setIsSubmitting(true); | ||
|
|
||
| if (!isAuthenticated) { | ||
| onNavigateToRegister?.(); | ||
| setIsSubmitting(false); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| if (!article.favorited) { | ||
| if (onFavorite) { | ||
| await onFavorite(article.slug); | ||
| } else { | ||
| return; | ||
| } | ||
| } else { | ||
| if (onUnfavorite) { | ||
| await onUnfavorite(article.slug); | ||
| } else { | ||
| return; | ||
| } | ||
| } | ||
| onToggle?.(!article.favorited); | ||
| } catch { | ||
| // Error handling — matches Angular's error callback (no-op) | ||
| } finally { | ||
| setIsSubmitting(false); | ||
| } | ||
| }, [article, isAuthenticated, onFavorite, onUnfavorite, onToggle, onNavigateToRegister]); | ||
|
|
||
| const buttonClass = [ | ||
| 'btn', | ||
| 'btn-sm', | ||
| isSubmitting ? 'disabled' : '', | ||
| article.favorited ? 'btn-primary' : 'btn-outline-primary', | ||
| ] | ||
| .filter(Boolean) | ||
| .join(' '); | ||
|
|
||
| return ( | ||
| <button className={buttonClass} onClick={toggleFavorite} disabled={isSubmitting}> | ||
| <i className="ion-heart"></i> {children} | ||
| </button> | ||
| ); | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.