-
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 #13 from Mayanzev/module8-task2
- Loading branch information
Showing
23 changed files
with
345 additions
and
185 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
60 changes: 60 additions & 0 deletions
60
src/components/change-favorite-button/change-favorite-button.tsx
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,60 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useAppDispatch, useAppSelector } from '../../hooks'; | ||
import { postFavoriteAction } from '../../store/api-actions'; | ||
import { getFavoriteOffersId } from '../../store/favorite-process/selectors'; | ||
import { changeFavoritesId } from '../../store/favorite-process/favorite-process'; | ||
import { AppRoute, AuthorizationStatus } from '../../const'; | ||
import { redirectToRoute } from '../../store/action'; | ||
import { getAuthorizationStatus } from '../../store/user-process/selectors'; | ||
|
||
type ChangeFavoriteButtonProps = { | ||
offerId: string; | ||
typeButton: string; | ||
width: string; | ||
height: string; | ||
}; | ||
|
||
function ChangeFavoriteButton({ offerId, typeButton, width, height }: ChangeFavoriteButtonProps): JSX.Element { | ||
const dispatch = useAppDispatch(); | ||
const favoritesOffersId = useAppSelector(getFavoriteOffersId); | ||
const status = useAppSelector(getAuthorizationStatus); | ||
const [isFavorite, setIsFavorite] = useState(favoritesOffersId.includes(offerId)); | ||
const [isSubmitting, setIsSubmitting] = useState(false); | ||
|
||
useEffect(() => { | ||
if (!isSubmitting) { | ||
setIsFavorite(favoritesOffersId.includes(offerId)); | ||
} | ||
}, [favoritesOffersId, offerId, isSubmitting, dispatch]); | ||
|
||
const handleFavorite = () => { | ||
if (status === AuthorizationStatus.NoAuth) { | ||
dispatch(redirectToRoute(AppRoute.Login)); | ||
} else { | ||
setIsSubmitting(true); | ||
const updatedFavorites = isFavorite ? favoritesOffersId.filter((id) => id !== offerId) : [...favoritesOffersId, offerId]; | ||
dispatch(changeFavoritesId(updatedFavorites)); | ||
setIsFavorite(!isFavorite); | ||
dispatch(postFavoriteAction({ id: offerId, status: isFavorite ? 0 : 1 })) | ||
.then(() => setIsSubmitting(false)) | ||
.catch(() => { | ||
setIsSubmitting(false); | ||
}); | ||
} | ||
}; | ||
return ( | ||
<button | ||
className={isFavorite ? `${typeButton}__bookmark-button ${typeButton}__bookmark-button--active button` : `${typeButton}__bookmark-button button`} | ||
type="button" | ||
onClick={handleFavorite} | ||
disabled={isSubmitting} | ||
> | ||
<svg className={`${typeButton}__bookmark-icon`} width={width} height={height}> | ||
<use xlinkHref="#icon-bookmark"></use> | ||
</svg> | ||
<span className="visually-hidden">To bookmarks</span> | ||
</button> | ||
); | ||
} | ||
|
||
export default ChangeFavoriteButton; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
type EmptyOffersProps = { | ||
city: string; | ||
} | ||
|
||
function EmptyOffers({city}: EmptyOffersProps): JSX.Element { | ||
return ( | ||
<div className="cities"> | ||
<div className="cities__places-container cities__places-container--empty container"> | ||
<section className="cities__no-places"> | ||
<div className="cities__status-wrapper tabs__content"> | ||
<b className="cities__status">No places to stay available</b> | ||
<p className="cities__status-description">We could not find any property available at the moment in {city}</p> | ||
</div> | ||
</section> | ||
<div className="cities__right-section" /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
export default EmptyOffers; |
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
Oops, something went wrong.