= useSelector;
\ No newline at end of file
diff --git a/src/index.tsx b/src/index.tsx
index 135d8c4..ddf385a 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -1,7 +1,8 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './components/app/app';
-import { Settings } from './const';
+import { Provider } from 'react-redux';
+import { store } from './store';
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
@@ -9,6 +10,8 @@ const root = ReactDOM.createRoot(
root.render(
-
+
+
+
);
diff --git a/src/mocks/cities.ts b/src/mocks/cities.ts
index b6a938c..3fd1a29 100644
--- a/src/mocks/cities.ts
+++ b/src/mocks/cities.ts
@@ -1,7 +1,34 @@
import { City } from '../types/city';
-export const AMSTERDAM: City = {
- title: 'Amsterdam',
- lat: 52.3740300,
- lng: 4.8896900
-};
+export const CITIES: City[] = [
+ {
+ title: 'Paris',
+ lat: 52.3740300,
+ lng: 4.8896900
+ },
+ {
+ title: 'Brussels',
+ lat: 52.3740300,
+ lng: 4.8896900
+ },
+ {
+ title: 'Cologne',
+ lat: 52.3740300,
+ lng: 4.8896900
+ },
+ {
+ title: 'Amsterdam',
+ lat: 52.3740300,
+ lng: 4.8896900
+ },
+ {
+ title: 'Hamburg',
+ lat: 52.3740300,
+ lng: 4.8896900
+ },
+ {
+ title: 'Dusseldorf',
+ lat: 52.3740300,
+ lng: 4.8896900
+ },
+];
diff --git a/src/mocks/offers.ts b/src/mocks/offers.ts
index 6cf9555..17b7543 100644
--- a/src/mocks/offers.ts
+++ b/src/mocks/offers.ts
@@ -1,4 +1,6 @@
import { Offer } from '../types/offer';
+import { CITIES } from './cities';
+import { POINTS } from './points';
import { REVIEWS } from './reviews';
export const OFFERS: Offer[] = [
@@ -11,7 +13,9 @@ export const OFFERS: Offer[] = [
type: 'Apartment',
isFavorite: false,
rating: 4,
- reviews: [REVIEWS[0]]
+ reviews: [REVIEWS[0], REVIEWS[1]],
+ city: CITIES[0],
+ point: POINTS[0]
},
{
id: '2',
@@ -22,7 +26,9 @@ export const OFFERS: Offer[] = [
type: 'Room',
isFavorite: true,
rating: 4,
- reviews: [REVIEWS[1]]
+ reviews: [REVIEWS[1]],
+ city: CITIES[1],
+ point: POINTS[1]
},
{
id: '3',
@@ -33,7 +39,9 @@ export const OFFERS: Offer[] = [
type: 'Apartment',
isFavorite: false,
rating: 4.7,
- reviews: [REVIEWS[2]]
+ reviews: [REVIEWS[2]],
+ city: CITIES[0],
+ point: POINTS[2]
},
{
id: '4',
@@ -44,6 +52,8 @@ export const OFFERS: Offer[] = [
type: 'Apartment',
isFavorite: true,
rating: 5,
- reviews: [REVIEWS[3]]
+ reviews: [REVIEWS[3]],
+ city: CITIES[3],
+ point: POINTS[3]
},
];
diff --git a/src/pages/main-screen/main-screen.tsx b/src/pages/main-screen/main-screen.tsx
index 22e0a69..4b066f2 100644
--- a/src/pages/main-screen/main-screen.tsx
+++ b/src/pages/main-screen/main-screen.tsx
@@ -1,17 +1,15 @@
-import { Offer } from '../../types/offer';
import OfferList from '../../components/offer-list/offer-list';
import { Link } from 'react-router-dom';
import Map from '../../components/map/map';
-import { POINTS } from '../../mocks/points';
-import { AMSTERDAM } from '../../mocks/cities';
import { typeOfCardList } from '../../const';
+import { useAppSelector } from '../../hooks';
+import CityList from '../../components/city-list/city-list';
-type MainScreenProps = {
- placesCount: number;
- offers: Offer[];
- }
-
-function MainScreen({placesCount, offers}: MainScreenProps): JSX.Element {
+function MainScreen(): JSX.Element {
+ const [city, offers] = useAppSelector((state) => [state.city, state.offers]);
+ const chosenOffers = offers.filter((offer) => offer.city === city);
+ const points = chosenOffers.map((offer) => offer.point);
+ const favoriteOffers = offers.filter((offer) => offer.isFavorite);
return (
@@ -30,7 +28,7 @@ function MainScreen({placesCount, offers}: MainScreenProps): JSX.Element {
Oliver.conner@gmail.com
- 3
+ {favoriteOffers.length}
@@ -49,45 +47,14 @@ function MainScreen({placesCount, offers}: MainScreenProps): JSX.Element {
Cities
Places
- {placesCount} places to stay in Amsterdam
+ {chosenOffers.length} places to stay in {city.title}
-
+
diff --git a/src/pages/offer-screen/offer-screen.tsx b/src/pages/offer-screen/offer-screen.tsx
index ab72538..06af9b0 100644
--- a/src/pages/offer-screen/offer-screen.tsx
+++ b/src/pages/offer-screen/offer-screen.tsx
@@ -2,7 +2,7 @@ import { Link } from 'react-router-dom';
import ReviewsList from '../../components/reviews-list/reviews-list';
import { OFFERS } from '../../mocks/offers';
import { Offer } from '../../types/offer';
-import { AMSTERDAM } from '../../mocks/cities';
+import { CITIES } from '../../mocks/cities';
import { POINTS } from '../../mocks/points';
import Map from '../../components/map/map';
import OfferList from '../../components/offer-list/offer-list';
@@ -168,7 +168,7 @@ function OfferScreen({offer}: OfferScreenProps): JSX.Element {
diff --git a/src/store/action.ts b/src/store/action.ts
new file mode 100644
index 0000000..dc00bbf
--- /dev/null
+++ b/src/store/action.ts
@@ -0,0 +1,8 @@
+import { createAction } from "@reduxjs/toolkit";
+import { City } from "../types/city";
+
+export const getOffers = createAction('OFFERS_GET')
+
+export const changeCity = createAction('CITY_CHANGE', (value: City) => ({
+ payload: value
+}));
\ No newline at end of file
diff --git a/src/store/index.ts b/src/store/index.ts
new file mode 100644
index 0000000..faeda0c
--- /dev/null
+++ b/src/store/index.ts
@@ -0,0 +1,4 @@
+import { configureStore } from '@reduxjs/toolkit';
+import { reducer } from './reducer';
+
+export const store = configureStore({reducer});
\ No newline at end of file
diff --git a/src/store/reducer.ts b/src/store/reducer.ts
new file mode 100644
index 0000000..d64484d
--- /dev/null
+++ b/src/store/reducer.ts
@@ -0,0 +1,28 @@
+import { createReducer } from '@reduxjs/toolkit';
+import { CITIES } from '../mocks/cities';
+import { OFFERS } from '../mocks/offers';
+import { City } from '../types/city';
+import { Offer } from '../types/offer';
+import { changeCity, getOffers } from './action';
+
+type StateType = {
+ city: City;
+ offers: Offer[]
+ }
+
+const initialState: StateType = {
+ city: CITIES[0],
+ offers: OFFERS
+ };
+
+const reducer = createReducer(initialState, (builder) => {
+ builder
+ .addCase(getOffers, (state) => {
+ state.offers = OFFERS;
+ })
+ .addCase(changeCity, (state, action) => {
+ state.city = action.payload;
+ });
+});
+
+export {reducer};
\ No newline at end of file
diff --git a/src/types/offer.ts b/src/types/offer.ts
index cbe3352..9811de6 100644
--- a/src/types/offer.ts
+++ b/src/types/offer.ts
@@ -1,4 +1,6 @@
+import { Point } from './point';
import { Review } from '../types/review';
+import { City } from './city';
export type Offer = {
id: string;
@@ -10,4 +12,6 @@ export type Offer = {
isFavorite: boolean;
rating: number;
reviews: Review[];
+ city: City;
+ point: Point;
};
diff --git a/src/types/state.ts b/src/types/state.ts
new file mode 100644
index 0000000..c45a265
--- /dev/null
+++ b/src/types/state.ts
@@ -0,0 +1,5 @@
+import { store } from '../store/index.js';
+
+export type State = ReturnType;
+
+export type AppDispatch = typeof store.dispatch;