Skip to content
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

Оптимизируй это (часть 2) #13

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/components/favorite-button/favorite-button.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {classNames} from '../../utils/class-names/class-names.ts';
import {OfferShortInfo} from '../../types/offer.ts';
import {useActionCreators, useAppSelector} from '../../hooks/store.ts';
import {favoritesActions, favoritesSelectors} from '../../store/slices/favorites.ts';
import {favoritesActions} from '../../store/slices/favorites.ts';
import {useState} from 'react';
import {AppRoute, AuthStatus, RequestStatus} from '../../const.ts';
import {AppRoute, AuthStatus} from '../../const.ts';
import {toast} from 'react-toastify';
import {useNavigate} from 'react-router-dom';
import {userSelectors} from '../../store/slices/user.ts';
Expand All @@ -27,8 +27,8 @@ function FavoriteButton({componentType, isFavorite, offerId}: FavoriteButtonProp
} as const;

const [isFavoriteCurrent, setIsFavoriteCurrent] = useState(isFavorite);
const [isDisabled, setIsDisabled] = useState(false);
const {toggleFavorite} = useActionCreators(favoritesActions);
const statusToggleFavorite = useAppSelector(favoritesSelectors.statusToggleFavorite);
const authStatus = useAppSelector(userSelectors.authStatus);
const navigate = useNavigate();

Expand All @@ -38,12 +38,13 @@ function FavoriteButton({componentType, isFavorite, offerId}: FavoriteButtonProp
if (!isAuth) {
navigate(AppRoute.Login);
}

setIsDisabled(true);
toast.promise(toggleFavorite({status: Number(!isFavoriteCurrent) as 0 | 1, offerId}).unwrap(), {
pending: 'Sending request',
success: {
render() {
setIsFavoriteCurrent(!isFavoriteCurrent);
setIsDisabled(false);
return 'Success';
}
},
Expand All @@ -59,7 +60,7 @@ function FavoriteButton({componentType, isFavorite, offerId}: FavoriteButtonProp
}
type="button"
onClick={onClickHandler}
disabled={statusToggleFavorite === RequestStatus.Loading}
disabled={isDisabled}
>
<svg className={`${componentType}__bookmark-icon`} {...sizes[componentType]}>
<use xlinkHref="#icon-bookmark"></use>
Expand Down
2 changes: 1 addition & 1 deletion src/components/login-city-link/login-city-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function LoginCityLink() {
return (
<section className="locations locations--login locations--current">
<div className="locations__item">
<Link to={randomCity.slug} className="locations__item-link">
<Link to={`/${randomCity.slug}`} className="locations__item-link">
<span>{randomCity.name}</span>
</Link>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/hooks/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const useAppStore: () => typeof store = useStore;
const useActionCreators = <Actions extends ActionCreatorsMapObject>(actions: Actions): BoundActions<Actions> => {
const dispatch = useAppDispatch();

// eslint-disable-next-line react-hooks/exhaustive-deps
return useMemo(() => bindActionCreators(actions, dispatch), []);
Comment on lines +15 to 16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В принеципе забей

Suggested change
// eslint-disable-next-line react-hooks/exhaustive-deps
return useMemo(() => bindActionCreators(actions, dispatch), []);
return useMemo(() => bindActionCreators(actions, dispatch), [dispatch, actions]);

};

Expand Down
Loading