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: 도서 상태 정보를 청구기호 순으로 정렬 #549

Merged
Merged
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
26 changes: 23 additions & 3 deletions src/component/book/BookDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ import Image from "~/component/utils/Image";
import Like from "~/component/book/like/Like";
import TagWrapper from "~/component/book/tag/TagWrapper";
import "~/asset/css/BookDetail.css";
import { Book } from "~/type";

const callsignToNumbers = (callSign: string) =>
callSign
.replace(/[^0-9\.]/g, "")
.split(".")
.map(Number);

const compareCallsign = (a: Book, b: Book) => {
const xs = callsignToNumbers(a.callSign);
const ys = callsignToNumbers(b.callSign);

return xs.reduce((sum, x, i) => sum + (x - ys[i]), 0);
};

const BookDetail = () => {
const id = useParams().id || "";
Expand Down Expand Up @@ -104,9 +118,15 @@ const BookDetail = () => {
</span>
<div className="book-state__list">
<div>
{bookDetailInfo.books?.map((book, index) => (
<BookStatus key={book.id} book={book} index={index} />
))}
{bookDetailInfo.books
?.toSorted((a, b) => compareCallsign(a, b))
Copy link
Collaborator

Choose a reason for hiding this comment

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

낯선 메서드라 찾아보니 ECMA 2023이네요!
최신 문법 활용 역시 👍
덕분에 새로 배웠습니다!

.map((book, index) => (
<BookStatus
key={book.callSign}
book={book}
index={index}
/>
))}
</div>
</div>
</div>
Expand Down
Loading