Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: task6 (Redux) #5

Open
wants to merge 1 commit into
base: task5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 89 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
"keywords": [],
"author": "Viktar Hushchyn",
"dependencies": {
"@reduxjs/toolkit": "^1.9.0",
"normalize.css": "^8.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.39.1"
"react-hook-form": "^7.39.1",
"react-redux": "^8.0.5"
},
"devDependencies": {
"@types/node": "18.11.4",
Expand Down
16 changes: 1 addition & 15 deletions src/Components/MovieCard/movieCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,7 @@ import styles from './movieCard.module.scss';
import MovieCardMenuButton from './MovieCardMenuButton';
import { AppContext } from '../../Contexts/appContext';
import getReleaseYear from '../../Utils/getReleaseYear';

export type MovieCardArgs = {
id: number;
title?: string;
tagline?: string;
vote_average?: number;
vote_count?: number;
poster_path?: string;
overview?: string;
release_date?: string;
budget?: number;
revenue?: number;
runtime?: number;
genres?: string[];
};
import { MovieCardArgs } from '../../Models';

const MovieCard: React.FunctionComponent<MovieCardArgs> = ({
id,
Expand Down
2 changes: 1 addition & 1 deletion src/Components/MovieDetail/movieDetail.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { useContext } from 'react';

import { MovieCardArgs } from '../MovieCard';
import Brand from '../Brand';
import SearchButton from '../SearchButton';

import styles from './movieDetail.module.scss';
import { AppContext } from '../../Contexts/appContext';
import getReleaseYear from '../../Utils/getReleaseYear';
import getMovieLength from '../../Utils/getMovieLength';
import { MovieCardArgs } from '../../Models';

type MovieDetailType = {
activeMovie: MovieCardArgs;
Expand Down
22 changes: 19 additions & 3 deletions src/Components/Navigation/navigation.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import React from 'react';
import React, { useEffect, useMemo, useState } from 'react';

import styles from './navigation.module.scss';
import useAppDispatch from '../../Hooks/useAppDispatch';
import { filterMoviesByGenre, loadMovies } from '../../Containers/MovieList/movieListSlice';

const Navigation: React.FunctionComponent = () => {
const navItems: string[] = ['all', 'documentary', 'comedy', 'horror', 'crime'];
const [filter, setFilter] = useState('all');
const dispatch = useAppDispatch();

const navItems: string[] = useMemo(() => ['all', 'documentary', 'comedy', 'horror', 'crime'], []);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

если этот набор параметров не будет меняться, то можно его не держать в реактовом компоненте а вынести например за пределы (между 5 и 7 строкой) и будет просто const NAV_ITMES = ['all', 'documentary', 'comedy', 'horror', 'crime']. тогда и useMemo не нужен и в зависимостях эффекта можно не держать

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

еще для простых конструкций можно JSON.stringify(navItems)
вот тут подробно написано facebook/react#14476 (comment)


const handleOnFilter = item => setFilter(item);

useEffect(() => {
if (filter === navItems[0]) {
dispatch(loadMovies());
} else {
dispatch(filterMoviesByGenre(filter));
}
}, [filter, dispatch, navItems]);

return (
<nav>
<ul className={styles.nav}>
{navItems.map(item => {
return (
<li key={item} className={styles.navItem} value={item}>
<li key={item} className={styles.navItem} value={item} onClick={() => handleOnFilter(item)}>
<div>{item}</div>
</li>
);
Expand Down
22 changes: 20 additions & 2 deletions src/Components/Search/search.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import React from 'react';
import React, { useEffect, useState } from 'react';

import styles from './search.module.scss';
import useAppDispatch from '../../Hooks/useAppDispatch';
import { searchMoviesByTitle } from '../../Containers/MovieList/movieListSlice';

const Search: React.FunctionComponent = () => {
const [searchText, setSearchText] = useState('');
const dispatch = useAppDispatch();

const handleOnChange = e => setSearchText(e.target.value);

useEffect(() => {
if (searchText?.length > 2) {
dispatch(searchMoviesByTitle(searchText));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

в учебке не так важно, но в проде лучше использовать debounce если поиск происходит на сервере, чтобы на каждый введенный символ (а пользователи обычно вводят быстро) не происходил ненужный запрос

}
}, [searchText, dispatch]);
return (
<>
<h2 className={styles.searchText}>find your movie</h2>
<div className={styles.searchInputWrapper}>
<input className={styles.searchInput} type="search" placeholder="What do you want to watch?" />
<input
className={styles.searchInput}
type="search"
value={searchText}
onChange={handleOnChange}
placeholder="What do you want to watch?"
/>
<button className={styles.searchButton} type="button">
search
</button>
Expand Down
17 changes: 15 additions & 2 deletions src/Components/Sorting/sorting.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import React from 'react';
import React, { useEffect, useState } from 'react';

import styles from './sorting.module.scss';
import useAppDispatch from '../../Hooks/useAppDispatch';
import { sortMovies } from '../../Containers/MovieList/movieListSlice';

const Sorting: React.FunctionComponent = () => {
const [sortBy, setSortBy] = useState('all');
const dispatch = useAppDispatch();

const handleOnChange = e => setSortBy(e.target.value);

useEffect(() => {
dispatch(sortMovies(sortBy));
}, [sortBy, dispatch]);

return (
<div className={styles.wrapper}>
<span className={styles.label}>sort by</span>
<select className={styles.select}>
<select className={styles.select} onChange={handleOnChange}>
<option value="genre">genre</option>
<option value="rating">rating</option>
<option value="releaseDate">release date</option>
</select>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/Containers/App/app.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
align-items: center;
margin: auto;
width: 100%;
height: 100%;
height: 100vh;
background: #424242;
}
Loading