Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/app/dashboard/utils/order.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ScrapCardData } from "@/types/scrapCard";
import { sortByKorean, sortByDateDesc, type SortType } from "@/utils/sorting";
import { sortByKorean, type SortType } from "@/utils/sorting";

export type { SortType };

Expand All @@ -11,7 +11,7 @@ export function sortCards(
sortType: SortType
): ScrapCardData[] {
if (sortType === "latest") {
return sortByDateDesc(cards, (card) => card.date);
return [...cards].reverse();
}

return sortByKorean(cards, (card) => card.term);
Expand Down
11 changes: 3 additions & 8 deletions src/app/quiz/components/QuizResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function QuizResult({
onRetry,
}: QuizResultProps) {
const { user } = useAuthCore();
const { toggleScrap, isScraped } = useScrap();
const { addMultipleToScrap } = useScrap();
const { showToast } = useToast();
const [isRetrying, setIsRetrying] = useState(false);

Expand All @@ -42,13 +42,8 @@ export default function QuizResult({
}

try {
let scrapCount = 0;
for (const question of wrongQuestions) {
if (!isScraped(question.term.id)) {
await toggleScrap(question.term.id);
scrapCount++;
}
}
const termIds = wrongQuestions.map((q) => q.term.id);
const scrapCount = await addMultipleToScrap(termIds);
showToast(
scrapCount > 0
? `틀린 문제 ${scrapCount}개를 스크랩했습니다!`
Expand Down
34 changes: 34 additions & 0 deletions src/contexts/auth/ScrapContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface ScrapContextType {
toggleScrap: (
termId: number
) => Promise<{ success: boolean; isScraped: boolean }>;
addMultipleToScrap: (termIds: number[]) => Promise<number>;
}

const ScrapContext = createContext<ScrapContextType | null>(null);
Expand Down Expand Up @@ -78,11 +79,44 @@ export function ScrapProvider({ children }: { children: ReactNode }) {
}
};

/**
* 여러 항목을 한 번에 스크랩에 추가
*/
const addMultipleToScrap = async (termIds: number[]): Promise<number> => {
if (!user || !userData) {
return 0;
}

// 이미 스크랩된 항목 제외
const newTermIds = termIds.filter((id) => !userData.scrapList.includes(id));

if (newTermIds.length === 0) {
return 0;
}

const newScrapList = [...userData.scrapList, ...newTermIds];

try {
const userRef = doc(db, "users", user.uid);
await updateDoc(userRef, {
scrapList: newScrapList,
});

updateScrapList(newScrapList);

return newTermIds.length;
} catch (error) {
console.error("다중 스크랩 추가 실패:", error);
throw error;
}
};

return (
<ScrapContext.Provider
value={{
isScraped,
toggleScrap,
addMultipleToScrap,
}}
>
{children}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/terms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,8 @@ export async function getRelatedTerms(
relatedIds: number[]
): Promise<TermIndexItem[]> {
const index = await getTermsIndex();
return index.filter((t) => relatedIds.includes(t.id));
const indexMap = new Map(index.map((t) => [t.id, t]));
return relatedIds
.map((id) => indexMap.get(id))
.filter(Boolean) as TermIndexItem[];
}