Skip to content

Commit

Permalink
Merge pull request #65 from RyuDongHo/main
Browse files Browse the repository at this point in the history
[5, 6주차/다일] 워크북 제출합니다.
  • Loading branch information
RyuDongHo authored Nov 16, 2024
2 parents 581ac3b + 40b7e78 commit db4996e
Show file tree
Hide file tree
Showing 143 changed files with 18,514 additions and 41 deletions.
615 changes: 615 additions & 0 deletions keyword/chapter05/keyword.md

Large diffs are not rendered by default.

337 changes: 337 additions & 0 deletions keyword/chapter06/keyword.md

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions mission/chapter04/mission/package-lock.json

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

1 change: 1 addition & 0 deletions mission/chapter04/mission/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0",
"react-intersection-observer": "^9.13.1",
"react-router-dom": "^6.26.2",
"styled-components": "^6.1.13",
"styled-reset": "^4.5.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ const useMovieProvider = (id) => {
}, [id]);
return [movieProvider, loading];
};
export default useMovieProvider;
export default useMovieProvider;
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ const useMovieList = (category, page) => {
"GET",
`${BASE_URL}/movie/${category}?language=ko-KR&page=${page}`
);
result.results.forEach(e => {
result.results.forEach((e) => {
e.poster_path = `${BASE_IMG_URL}${BASE_IMG_SIZE}${e.poster_path}`;
});

setMovieList((prevList) => [
...prevList,
...result.results
]);

// if(page % 5 == 0){
// window.scrollTo({ top: 0});
// setMovieList(result.results);
// } else{
// setMovieList((prevList) => [...prevList, ...result.results]);
// }
setMovieList((prevList) => [...prevList, ...result.results]);
setLoading(false);
console.log(page, "page")
};

fetchMovie();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,30 @@ import STYLE from "./style";
import React from "react";
import Movie from "./ui/Movie";
import { useParams } from "react-router-dom";
const MovieList = React.memo(() => {
import { useInView } from "react-intersection-observer";
const MovieList = () => {
const { category } = useParams();
const [page, setPage] = React.useState(1);
const [movieList, loading] = useMovieList(category, page);

const observer = React.useRef();
// const lastMovieElementRef = React.useCallback(
// (node) => {
// if (loading) return;
// if (observer.current) observer.current.disconnect();
// observer.current = new IntersectionObserver((entries) => {
// if (entries[0].isIntersecting) {
// setPage((prevPage) => prevPage + 1); // 페이지 증가
// }
// });
// if (node) observer.current.observe(node);
// },
// [loading]
// );
const lastMovieElementRef = (node) => {
if (loading) return;
if (observer.current) observer.current.disconnect();
observer.current = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting) {
setPage((prevPage) => prevPage + 1); // 페이지 증가
}
});
if (node) observer.current.observe(node);
}
const { ref, inView } = useInView({
threshold: 0,
});

React.useEffect(() =>{
if(inView && !loading){
setPage((prev) => prev+1);
}
}, [inView, loading])

if (loading) return <div>still loading...</div>; // API loading

if (loading) return <div>still loading...</div>;
return (
<STYLE.MovieList>
{movieList.map((elem, index) => {
// 마지막 아이템에 ref 추가하여 관찰
console.log(index + "렌더링");
return (
<Movie
lastMovieElementRef={index === movieList.length - 1 ? lastMovieElementRef : null}
observe={index === movieList.length - 1 ? ref : null}
key={index}
data={elem}
/>
Expand All @@ -50,7 +35,7 @@ const MovieList = React.memo(() => {
{loading && <div>Loading more movies...</div>}
</STYLE.MovieList>
);
});
MovieList.displayName= "MovieList"
};
MovieList.displayName = "MovieList";

export default MovieList;
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import STYLE from "./style";
import { useNavigate } from "react-router-dom";
import { handleNavigation } from "../../../../../../3_Shared/model/handleNavigate";
const Movie = (props) => {
const { lastMovieElementRef, data} = props;
const { observe, data} = props;
const { poster_path, title, release_date, id } = data;
const navigate = useNavigate();
return (
<STYLE.Movie ref={lastMovieElementRef} onClick={()=>{handleNavigation(navigate, `/movie-detail/${id}`);}}>
<STYLE.Movie ref={observe} onClick={()=>{handleNavigation(navigate, `/movie-detail/${id}`);}}>
<STYLE.Thumbnail src={`${poster_path}`} alt="movie poster" />
<STYLE.ThumbnailHoverEffectDiv />
<STYLE.MovieInfo>
Expand Down
14 changes: 14 additions & 0 deletions mission/chapter04/mission/src/3_Shared/lib/useThrottle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from "react";
const useThrottle = (callback, delay = 1000) => {
let lastCallTime = React.useRef(Date.now());

return () => {
const timeElapsed = Date.now() - lastCallTime.current;
if(timeElapsed >= delay) {
callback();
lastCallTime.current = Date.now();
}
}
};

export default useThrottle;
Loading

0 comments on commit db4996e

Please sign in to comment.