Skip to content

Commit

Permalink
Оптимизируй это (часть 2) (#13)
Browse files Browse the repository at this point in the history
* фиксит ссылку

* disabled favorite button
  • Loading branch information
denispan authored Apr 9, 2024
1 parent e86fdd5 commit 2379004
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
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), []);
};

Expand Down

0 comments on commit 2379004

Please sign in to comment.