Skip to content

Commit

Permalink
добавление рандомного города на странице логина
Browse files Browse the repository at this point in the history
  • Loading branch information
denispan committed Apr 9, 2024
1 parent 0a88e30 commit ad4dd6e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
19 changes: 19 additions & 0 deletions src/components/login-city-link/login-city-link.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {getRandomElement} from '../../utils/common.ts';
import {CITIES} from '../../const.ts';
import {Link} from 'react-router-dom';

function LoginCityLink() {
const randomCity = getRandomElement(CITIES);

return (
<section className="locations locations--login locations--current">
<div className="locations__item">
<Link to={randomCity.slug} className="locations__item-link">
<span>{randomCity.name}</span>
</Link>
</div>
</section>
);
}

export default LoginCityLink;
9 changes: 2 additions & 7 deletions src/pages/login/login.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Logo from '../../components/logo/logo.tsx';
import {useDocumentTitle} from '../../hooks/document-title.ts';
import LoginForm from '../../components/form-login/login-form.tsx';
import LoginCityLink from '../../components/login-city-link/login-city-link.tsx';

interface LoginProps {
title?: string;
Expand All @@ -27,13 +28,7 @@ function Login({title = 'Login'}: LoginProps) {
<h1 className="login__title">Sign in</h1>
<LoginForm />
</section>
<section className="locations locations--login locations--current">
<div className="locations__item">
<a className="locations__item-link" href="#">
<span>Amsterdam</span>
</a>
</div>
</section>
<LoginCityLink />
</div>
</main>
</div>
Expand Down
10 changes: 4 additions & 6 deletions src/pages/main/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ function Main({title = 'Main', citySlug}: MainProps) {
const allOffers = useAppSelector(offersSelectors.offers);

useEffect(() => {
if (status === RequestStatus.Idle) {
fetchOffers().unwrap().catch((err: AxiosError) => {
toast.warning(err.message);
}) ;
}
}, [status, fetchOffers]);
fetchOffers().unwrap().catch((err: AxiosError) => {
toast.warning(err.message);
}) ;
}, []);


const activeCity = CITIES.find((city) => city.slug === citySlug);
Expand Down
7 changes: 6 additions & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ const capitalizeFirstLetter = (string: string) => string.charAt(0).toUpperCase()

const getCitySlug = (cityName: CityName) => CITIES.find((city) => city.name === cityName)?.slug;

export {getRatingValue, capitalizeFirstLetter, getCitySlug};
const getRandomElement = <T>(arr:readonly T[]): T => {
const randomIndex = Math.floor(Math.random() * arr.length);
return arr[randomIndex];
};

export {getRatingValue, capitalizeFirstLetter, getCitySlug, getRandomElement};

0 comments on commit ad4dd6e

Please sign in to comment.