diff --git a/src/app/vol/[journalId]/page.tsx b/src/app/vol/[journalId]/page.tsx index cda0df6..056c553 100644 --- a/src/app/vol/[journalId]/page.tsx +++ b/src/app/vol/[journalId]/page.tsx @@ -3,7 +3,7 @@ import Link from 'next/link'; import { notFound } from 'next/navigation'; -import { JournalCard, SongCardList, HotJournalList, Comment, Coolect } from '@/components'; +import { JournalCard, SongCardList, HotJournalList, Comment, Collect } from '@/components'; import { apiGetJournalInfoById, apiGetSongsByJournalNo } from '@/services'; const getData = async (journalId: string) => { @@ -51,10 +51,9 @@ export default async function JournalDetail({ params: { journalId } }: { params: {/* 收藏 */} - @@ -68,11 +67,7 @@ export default async function JournalDetail({ params: { journalId } }: { params: {/* 卡片 */} - +
diff --git a/src/components/Collect.tsx b/src/components/Collect.tsx index 42905a8..3a845a0 100644 --- a/src/components/Collect.tsx +++ b/src/components/Collect.tsx @@ -1,3 +1,5 @@ +// 期刊收藏 + 'use client'; import { useEffect, useState } from 'react'; @@ -5,38 +7,31 @@ import { useEffect, useState } from 'react'; import { apiCollect } from '@/services'; interface Props { - haveCollect: boolean; + active: boolean; id: string; type: string; size?: number; - count?: string; text?: string; } -export default function Collect({ haveCollect, id, type, size = 24, count = '0', text = '' }: Props) { +export default function Collect({ active, id, type, size = 24, text = '' }: Props) { const [state, setState] = useState(false); // 收藏状态 useEffect(() => { - setState(haveCollect); - }, [haveCollect]); + setState(active); + }, [active]); // 收藏/取消收藏 const handleCollect = async () => { - setState(!state); + setState(!state); // 更新收藏状态 const res = await apiCollect({ isAdd: !state, objectId: id, collectType: type }); - if (res.code !== 200) { - setState(!state); - } + if (res.code !== 200) setState(!state); // 如果请求失败,回退状态 }; return (
{/* 文案 */} - {!!text && ( -

- {`${count}${text}`} -

- )} + {!!text &&

{text}

} {/* 图标 */}
(false); // 点赞状态 + + useEffect(() => { + setState(active); + }, [active]); + + // 收藏/取消收藏 + const handleCollect = async () => { + setState(!state); // 更新点赞状态 + const res = await apiCollect({ isAdd: !state, objectId: id, collectType: type }); + if (res.code !== 200) setState(!state); // 如果请求失败,回退状态 + }; + + return ( +
+ {/* 文案 */} + {!!text &&

{text}

} + {/* 图标 */} + +
+
+ ); +}