Skip to content

Commit

Permalink
Added nearby places to map on OfferScreen; Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fed1v committed Nov 11, 2024
1 parent 5c671eb commit ef5fd7c
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 293 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Offer} from '../../types/offer.ts';
import {PlaceCardFavorite} from '../place-card/place-card-favorite.tsx';
import {FavoritePlaceCard} from './favorite-place-card.tsx';

type FavoritesListProps = {
offers: Offer[];
}

export function FavoritesList({offers}: FavoritesListProps) {
export function FavoritePlaceCardList({offers}: FavoritesListProps) {
const cities = Array.from(new Set(offers.map((offer) => offer.city.name))).toSorted();

return (
Expand All @@ -28,7 +28,7 @@ export function FavoritesList({offers}: FavoritesListProps) {
offers
.filter((offer) => offer.city.name === city)
.map((offer) => (
<PlaceCardFavorite
<FavoritePlaceCard
key={offer.id}
{...offer}
imageSrc={offer.previewImage}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type PlaceCardFavoriteProps = {
isPremium: boolean;
}

export function PlaceCardFavorite({id, title, type, imageSrc, price, isPremium}: PlaceCardFavoriteProps) {
export function FavoritePlaceCard({id, title, type, imageSrc, price, isPremium}: PlaceCardFavoriteProps) {
const offerUrl = AppRoute.Offer.replace(':id', id);

return (
Expand All @@ -21,14 +21,14 @@ export function PlaceCardFavorite({id, title, type, imageSrc, price, isPremium}:
</div>}

<div className="favorites__image-wrapper place-card__image-wrapper">
<a href="#">
<Link to={offerUrl}>
<img
className="place-card__image"
src={imageSrc}
width="150" height="110"
alt="Place image"
/>
</a>
</Link>
</div>

<div className="favorites__card-info place-card__info">
Expand Down
45 changes: 45 additions & 0 deletions src/components/header/header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {Link} from 'react-router-dom';
import {AppRoute} from '../../consts.ts';

export function Header() {
return (
<header className="header">
<div className="container">
<div className="header__wrapper">
<div className="header__left">
<Link to={AppRoute.Main} className="header__logo-link">
<img
className="header__logo"
src="img/logo.svg"
alt="6 cities logo"
width={81}
height={41}
/>
</Link>
</div>
<nav className="header__nav">
<ul className="header__nav-list">
<li className="header__nav-item user">
<a
className="header__nav-link header__nav-link--profile"
href="#"
>
<div className="header__avatar-wrapper user__avatar-wrapper"></div>
<span className="header__user-name user__name">
[email protected]
</span>
<span className="header__favorite-count">3</span>
</a>
</li>
<li className="header__nav-item">
<a className="header__nav-link" href="#">
<span className="header__signout">Sign out</span>
</a>
</li>
</ul>
</nav>
</div>
</div>
</header>
);
}
39 changes: 0 additions & 39 deletions src/components/offers-list/offers-list.tsx

This file was deleted.

69 changes: 69 additions & 0 deletions src/components/place-card/place-card-list.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {Offer} from '../../types/offer.ts';
import {PlaceCard} from './place-card.tsx';
import {Nullable} from 'vitest';
import {PlaceCardType} from '../../consts.ts';

type GeneralPlaceCardListCommonProps = {
offers: Offer[];
onActiveItemChange?: (id: Nullable<string>) => void;
imageWidth: number;
imageHeight: number;
className: string;
placeCardType: PlaceCardType;
};

function GeneralPlaceCardList({
offers,
onActiveItemChange,
imageWidth,
imageHeight,
className,
placeCardType,
}: GeneralPlaceCardListCommonProps) {
return (
<div className={className}>
{
offers.map((offer) => (
<PlaceCard
key={offer.id}
{...offer}
imageSrc={offer.previewImage}
width={imageWidth}
height={imageHeight}
placeCardType={placeCardType}
onActiveItemChange={onActiveItemChange}
/>
))
}
</div>
);
}

type PlaceCardListProps = {
offers: Offer[];
onActiveItemChange?: (id: Nullable<string>) => void;
}

export function CityPlaceCardList(props: PlaceCardListProps) {
return (
<GeneralPlaceCardList
{...props}
imageWidth={260}
imageHeight={200}
className="cities__places-list places__list tabs__content"
placeCardType={PlaceCardType.City}
/>
);
}

export function NearPlaceCardList(props: PlaceCardListProps) {
return (
<GeneralPlaceCardList
{...props}
imageWidth={260}
imageHeight={200}
className="near-places__list places__list"
placeCardType={PlaceCardType.Near}
/>
);
}
40 changes: 31 additions & 9 deletions src/components/place-card/place-card.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Link} from 'react-router-dom';
import {AppRoute} from '../../consts.ts';
import {AppRoute, PlaceCardType} from '../../consts.ts';
import {Nullable} from 'vitest';

type PlaceCardProps = {
id: string;
Expand All @@ -8,30 +9,51 @@ type PlaceCardProps = {
imageSrc: string;
price: number;
isPremium: boolean;
onMouseEnter: (id: string) => void;
onMouseLeave: (id: string) => void;
width: number;
height: number;
onActiveItemChange?: (id: Nullable<string>) => void;
placeCardType: PlaceCardType;
}

export function PlaceCard({id, title, type, imageSrc, price, isPremium, onMouseEnter, onMouseLeave}: PlaceCardProps) {
export function PlaceCard({
id,
title,
type,
imageSrc,
price,
isPremium,
width,
height,
onActiveItemChange,
placeCardType,
}: PlaceCardProps) {
const offerUrl = AppRoute.Offer.replace(':id', id);

let classNamePrefix: string = '';

if (placeCardType === PlaceCardType.City) {
classNamePrefix = 'cities';
} else if (placeCardType === PlaceCardType.Near) {
classNamePrefix = 'near-places';
}

return (
<article
className="cities__card place-card"
onMouseEnter={() => onMouseEnter(id)}
onMouseLeave={() => onMouseLeave(id)}
className={`${classNamePrefix}__card place-card`}
onMouseEnter={() => onActiveItemChange?.(id)}
onMouseLeave={() => onActiveItemChange?.(null)}
>
{isPremium &&
<div className="place-card__mark">
<span>Premium</span>
</div>}

<Link to={offerUrl}>
<div className="cities__image-wrapper place-card__image-wrapper">
<div className={`${classNamePrefix}__image-wrapper place-card__image-wrapper`}>
<a href="#">
<img
className="place-card__image"
src={imageSrc} width="260" height="200"
src={imageSrc} width={width} height={height}
alt="Place image"
/>
</a>
Expand Down
5 changes: 5 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ export enum AuthorizationStatus {
NoAuth = 'NO_AUTH',
Unknown = 'UNKNOWN'
}

export enum PlaceCardType{
City,
Near
}
35 changes: 5 additions & 30 deletions src/pages/favorites-screen/favorites-screen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Offer} from '../../types/offer.ts';
import {FavoritesList} from '../../components/favorites-list/favorites-list.tsx';
import {FavoritePlaceCardList} from '../../components/favorites-list/favorite-place-card-list.tsx';
import {Header} from '../../components/header/header.tsx';

type FavoritesScreenProps = {
offers: Offer[];
Expand All @@ -8,40 +9,14 @@ type FavoritesScreenProps = {
export function FavoritesScreen({offers}: FavoritesScreenProps) {
return (
<div className="page">
<header className="header">
<div className="container">
<div className="header__wrapper">
<div className="header__left">
<a className="header__logo-link" href="main.html">
<img className="header__logo" src="img/logo.svg" alt="6 cities logo" width="81" height="41"/>
</a>
</div>
<nav className="header__nav">
<ul className="header__nav-list">
<li className="header__nav-item user">
<a className="header__nav-link header__nav-link--profile" href="#">
<div className="header__avatar-wrapper user__avatar-wrapper">
</div>
<span className="header__user-name user__name">[email protected]</span>
<span className="header__favorite-count">3</span>
</a>
</li>
<li className="header__nav-item">
<a className="header__nav-link" href="#">
<span className="header__signout">Sign out</span>
</a>
</li>
</ul>
</nav>
</div>
</div>
</header>
<Header/>

<main className="page__main page__main--favorites">
<div className="page__favorites-container container">
<section className="favorites">
<h1 className="favorites__title">Saved listing</h1>
<FavoritesList

<FavoritePlaceCardList
offers={offers}
/>
</section>
Expand Down
37 changes: 6 additions & 31 deletions src/pages/main-screen/main-screen.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {Offer} from '../../types/offer.ts';
import {OffersList} from '../../components/offers-list/offers-list.tsx';
import {Map} from '../../components/map/map.tsx';
import {useState} from 'react';
import {Nullable} from 'vitest';
import {Header} from '../../components/header/header.tsx';
import {CityPlaceCardList} from '../../components/place-card/place-card-list.tsx';

type MainScreenProps = {
offers: Offer[];
Expand All @@ -18,34 +19,8 @@ export function MainScreen({offers}: MainScreenProps) {

return (
<div className="page page--gray page--main">
<header className="header">
<div className="container">
<div className="header__wrapper">
<div className="header__left">
<a className="header__logo-link header__logo-link--active">
<img className="header__logo" src="img/logo.svg" alt="6 cities logo" width="81" height="41"/>
</a>
</div>
<nav className="header__nav">
<ul className="header__nav-list">
<li className="header__nav-item user">
<a className="header__nav-link header__nav-link--profile" href="#">
<div className="header__avatar-wrapper user__avatar-wrapper">
</div>
<span className="header__user-name user__name">[email protected]</span>
<span className="header__favorite-count">3</span>
</a>
</li>
<li className="header__nav-item">
<a className="header__nav-link" href="#">
<span className="header__signout">Sign out</span>
</a>
</li>
</ul>
</nav>
</div>
</div>
</header>
<Header/>

<main className="page__main page__main--index">
<h1 className="visually-hidden">Cities</h1>
<div className="tabs">
Expand Down Expand Up @@ -104,9 +79,9 @@ export function MainScreen({offers}: MainScreenProps) {
<li className="places__option" tabIndex={0}>Top rated first</li>
</ul>
</form>
<OffersList
<CityPlaceCardList
offers={offers}
onActiveOfferChange={handleActiveOfferChange}
onActiveItemChange={handleActiveOfferChange}
/>
</section>

Expand Down
Loading

0 comments on commit ef5fd7c

Please sign in to comment.