Skip to content

Commit

Permalink
feature-065: 하위 카테고리 태그 선택에 따라 비디오 보이도록 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
gs0428 committed Feb 14, 2024
1 parent 572926c commit 3acf9cb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 36 deletions.
1 change: 0 additions & 1 deletion src/hooks/useMoveCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const useMoveCategory = () => {
grabedCategory.current!.categoryId,
topId,
);
console.log(res);
if (res.isSuccess) {
await updateCategories();
navigate(`/category/${grabedCategory.current?.topCategoryId}`);
Expand Down
67 changes: 32 additions & 35 deletions src/pages/CategoryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { useRecoilValue } from 'recoil';
import { categoryState } from '@/stores/category';
import { ISubFolderProps, ITagProps } from 'types/category';
import EmptyCard from '@/components/category/EmptyCard';
import { deleteVideos, getRecentVideos, getVideoById } from '@/apis/videos';
import { deleteVideos, getRecentVideos } from '@/apis/videos';
import { IVideoProps } from 'types/videos';
import { sortVideos } from '@/utils/sortVideos';
import { CardContainer } from '@/styles/category/Card.style';
import { CategorySelectBox } from '@/components/SummaryPage/SummaryDetailBox/CategorySelectBox';
import Chip from '@/components/common/chip/Chip';
import { getCategoryTags } from '@/apis/category';
import handleVideo from '@/utils/handleVideo';

const CategoryPage = () => {
const params = useParams();
Expand Down Expand Up @@ -59,30 +59,15 @@ const CategoryPage = () => {
setMenus([]);
})
.catch((err) => console.log(err));
} else if (!params.sub_folder) {
getVideoById(params.top_folder).then((res) => {
const index = categories.findIndex(
(category) => category.categoryId === Number(params.top_folder),
);
setName(categories[index].name);
setMenus(categories[index].subFolders);
setVideos(res.isSuccess ? res.result.videos : []);
});
} else {
getVideoById(params.sub_folder).then((res) => {
const index = categories.findIndex(
(category) => category.categoryId === Number(params.top_folder),
);
const subIndex = categories[index].subFolders.findIndex(
(subFolder) => subFolder.categoryId === Number(params.sub_folder),
);
getCategoryTags(params.sub_folder!).then((res) =>
setMenus(res.result.tags),
);
setName(categories[index].subFolders[subIndex].name);

setVideos(res.isSuccess ? res.result.videos : []);
});
handleVideo(
categories,
params.top_folder,
params.sub_folder!,
setMenus,
setName,
setVideos,
);
}
setCheckedVideos([]);
}, [categories, params.sub_folder, params.top_folder]);
Expand All @@ -104,7 +89,6 @@ const CategoryPage = () => {
if (checkedVideos.length === videos.length) {
handleDeleteVideos();
} else {
console.log('모두 선택');
setCheckedVideos(videos.map((video) => video.video_id));
}
};
Expand Down Expand Up @@ -198,15 +182,28 @@ const CategoryPage = () => {
)}
{sortedVideos.length > 0 && (
<CardContainer>
{sortedVideos.map((video) => (
<Card
mode="category"
video={video}
checkedVideos={checkedVideos}
setCheckedVideos={setCheckedVideos}
key={video.category_id}
/>
))}
{sortedVideos.map((video) => {
const matchedTagCount = video.tag.reduce((acc, cur) => {
if (selectedTags.includes(cur.name)) return (acc += 1);
return acc;
}, 0);
if (
params.sub_folder &&
selectedTags.length &&
matchedTagCount !== selectedTags.length
)
return;

return (
<Card
mode="category"
video={video}
checkedVideos={checkedVideos}
setCheckedVideos={setCheckedVideos}
key={video.category_id}
/>
);
})}
</CardContainer>
)}
</CategoryPageStyles.Container>
Expand Down
1 change: 1 addition & 0 deletions src/styles/category/index.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const Container = styled.div`

export const MenuWrap = styled.div`
display: flex;
min-width: 'fit-content';
justify-content: space-between;
align-items: center;
margin-bottom: 40px;
Expand Down
36 changes: 36 additions & 0 deletions src/utils/handleVideo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getCategoryTags } from '@/apis/category';
import { getVideoById } from '@/apis/videos';
import { IFolderProps, ISubFolderProps, ITagProps } from 'types/category';
import { IVideoProps } from 'types/videos';

const handleVideo = async (
categories: IFolderProps[],
topCategoryId: string,
subCategoryId: string,
setMenus: React.Dispatch<
React.SetStateAction<ISubFolderProps[] | ITagProps[]>
>,
setName: React.Dispatch<React.SetStateAction<string>>,
setVideos: React.Dispatch<React.SetStateAction<IVideoProps[]>>,
) => {
await getVideoById(topCategoryId).then((res) => {
const topCategory = categories.find(
(category) => category.categoryId === Number(topCategoryId),
);
if (subCategoryId) {
const subName = topCategory?.subFolders.find(
(subFolder) => subFolder.categoryId === Number(subCategoryId),
);
setName(subName!.name);
getCategoryTags(subCategoryId!).then((res) => {
if (res.isSuccess) setMenus(res.result.tags);
else setMenus([]);
});
} else {
setName(topCategory!.name);
setMenus(topCategory!.subFolders);
}
setVideos(res.isSuccess ? res.result.videos : []);
});
};
export default handleVideo;

0 comments on commit 3acf9cb

Please sign in to comment.