Skip to content

Commit

Permalink
뷰어페이지 스크랩 버그 수정 (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
parkhyeonki authored Dec 9, 2022
2 parents d572357 + 0ebc808 commit 6920f5b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
2 changes: 1 addition & 1 deletion backend/src/apis/scraps/scraps.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const createScrap = async (req: Request, res: Response) => {
}
});

return res.status(201).send();
return res.status(201).send({ book_id, article_id });
};

const deleteScrap = async (req: Request, res: Response) => {
Expand Down
56 changes: 38 additions & 18 deletions frontend/components/viewer/ScrapModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { useRouter } from 'next/router';

import { useEffect, useState } from 'react';

import { useRecoilState } from 'recoil';
import { useRecoilState, useRecoilValue } from 'recoil';

import { getUserKnottedBooksApi } from '@apis/bookApi';
import { createScrapApi } from '@apis/scrapApi';
import scrapState from '@atoms/scrap';
import signInStatusState from '@atoms/signInStatus';
import DragArticle from '@components/common/DragDrop';
import Dropdown from '@components/common/Dropdown';
import ModalButton from '@components/common/Modal/ModalButton';
Expand All @@ -13,20 +17,26 @@ import { IBook, IArticle, IScrap, IBookScraps } from '@interfaces';
import { ArticleWrapper, Label, ScrapModalWrapper, WarningLabel } from './styled';

interface ScrapModalProps {
books: IBookScraps[];
bookId: number;
handleModalClose: () => void;
article: IArticle;
}

export default function ScrapModal({ books, handleModalClose, article }: ScrapModalProps) {
export default function ScrapModal({ bookId, handleModalClose, article }: ScrapModalProps) {
const [selectedBookIndex, setSelectedBookIndex] = useState(-1);
const [filteredScraps, setFilteredScraps] = useState<IScrap[]>([]);
const { execute: createScrap } = useFetch(createScrapApi);
const { data: createScrapData, execute: createScrap } = useFetch(createScrapApi);
const { data: books, execute: getUserKnottedBooks } =
useFetch<IBookScraps[]>(getUserKnottedBooksApi);

const user = useRecoilValue(signInStatusState);

const [scrapList, setScrapList] = useRecoilState(scrapState);

const [isSelectedBookUnavailable, setSelectedBookUnavailable] = useState(false);

const router = useRouter();

const createBookDropdownItems = (items: IBook[]) =>
items.map((item) => {
return {
Expand All @@ -51,9 +61,20 @@ export default function ScrapModal({ books, handleModalClose, article }: ScrapMo
return items.some((item) => item.article.id === articleId);
};

useEffect(() => {
const handleScrapBtnClick = () => {
if (selectedBookIndex === -1) return;

const scraps = scrapList.map((v, i) => ({ ...v, order: i + 1 }));

createScrap({ book_id: selectedBookIndex, article_id: article.id, scraps });
};
useEffect(() => {
getUserKnottedBooks(user.nickname);
}, [user.nickname]);

useEffect(() => {
if (selectedBookIndex === -1 || !books) return;

const selectedBook = books.find((book) => book.id === selectedBookIndex);

if (!selectedBook || checkArticleExistsInBook(article.id, selectedBook.scraps)) {
Expand All @@ -73,24 +94,23 @@ export default function ScrapModal({ books, handleModalClose, article }: ScrapMo
setScrapList(createScrapDropdownItems(filteredScraps));
}, [filteredScraps]);

const handleScrapBtnClick = () => {
if (selectedBookIndex === -1) return;

const scraps = scrapList.map((v, i) => ({ ...v, order: i + 1 }));

createScrap({ book_id: selectedBookIndex, article_id: article.id, scraps });
useEffect(() => {
if (createScrapData === undefined) return;
router.push(`/viewer/${bookId}/${article.id}`);
handleModalClose();
};
}, [createScrapData]);

return (
<ScrapModalWrapper>
<Label>책 선택</Label>
<Dropdown
label="글이 담길 책을 선택해주세요."
items={createBookDropdownItems(books)}
selectedId={selectedBookIndex}
handleItemSelect={(id) => setSelectedBookIndex(id)}
/>
{books && (
<Dropdown
label="글이 담길 책을 선택해주세요."
items={createBookDropdownItems(books)}
selectedId={selectedBookIndex}
handleItemSelect={(id) => setSelectedBookIndex(id)}
/>
)}
{isSelectedBookUnavailable && (
<WarningLabel>선택하신 책에 이미 동일한 글이 존재합니다.</WarningLabel>
)}
Expand Down
16 changes: 3 additions & 13 deletions frontend/pages/viewer/[...data].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import { useRouter } from 'next/router';

import { useEffect, useState } from 'react';

import { useRecoilValue } from 'recoil';

import { getArticleApi } from '@apis/articleApi';
import { getBookApi, getUserKnottedBooksApi } from '@apis/bookApi';
import signInStatusState from '@atoms/signInStatus';
import GNB from '@components/common/GNB';
import Modal from '@components/common/Modal';
import ArticleContainer from '@components/viewer/ArticleContent';
Expand All @@ -24,27 +21,20 @@ interface ViewerProps {
}

export default function Viewer({ article }: ViewerProps) {
const { data: userBooks, execute: getUserKnottedBooks } = useFetch(getUserKnottedBooksApi);
const { data: book, execute: getBook } = useFetch<IBookScraps>(getBookApi);

const user = useRecoilValue(signInStatusState);
const router = useRouter();

const [isOpened, setIsOpened] = useState(false);

const [isModalShown, setModalShown] = useState(false);

const handleModalOpen = () => setModalShown(true);
const handleModalClose = () => setModalShown(false);
const router = useRouter();

const handleSideBarToggle = () => {
setIsOpened((prev) => !prev);
};

useEffect(() => {
getUserKnottedBooks(user.nickname);
}, [user.nickname]);

const checkArticleAuthority = (targetBook: IBookScraps, id: number) => {
if (targetBook.scraps.find((scrap) => scrap.article.id === id)) {
return true;
Expand Down Expand Up @@ -101,9 +91,9 @@ export default function Viewer({ article }: ViewerProps) {
) : (
<div>loading</div>
)}
{isModalShown && (
{isModalShown && book && (
<Modal title="글 스크랩하기" handleModalClose={handleModalClose}>
<ScrapModal books={userBooks} handleModalClose={handleModalClose} article={article} />
<ScrapModal bookId={book.id} handleModalClose={handleModalClose} article={article} />
</Modal>
)}
</PageNoScrollWrapper>
Expand Down

0 comments on commit 6920f5b

Please sign in to comment.