From a3814b8adf36645198fad76b0654f18492c42bf7 Mon Sep 17 00:00:00 2001 From: sealday Date: Sat, 6 Apr 2024 23:32:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/songs/actions.tsx | 69 +++++++++++++++++++------------- app/songs/delete-song-button.tsx | 21 ++++++++++ app/songs/page.tsx | 10 +++-- 3 files changed, 69 insertions(+), 31 deletions(-) create mode 100644 app/songs/delete-song-button.tsx diff --git a/app/songs/actions.tsx b/app/songs/actions.tsx index aa54888..6bb22ce 100644 --- a/app/songs/actions.tsx +++ b/app/songs/actions.tsx @@ -4,6 +4,7 @@ import { InternetProviderLyricSearchResponse, getSearchResults, } from "@/lib/actions/lyrics"; +import { revalidateTag } from "next/cache"; import { LyricSource } from "@/lib/types"; import { cookies } from "next/headers"; @@ -23,7 +24,7 @@ export async function searchSongs( export async function addSong( _: unknown, item: InternetProviderLyricSearchResponse -): Promise { +) { const token = cookies().get("token"); await fetch("https://tachy.daoyoucloud.com/api/songs:create", { method: "POST", @@ -40,37 +41,51 @@ export async function addSong( source_id: item.id, }), }); - const params = new URLSearchParams(); - params.append('pageSize', '99999'); - params.append('filter', JSON.stringify({"$and":[{"createdBy":{"id":{"$eq":"{{$user.id}}"}}}]})) - const songs = await fetch("https://tachy.daoyoucloud.com/api/songs:list?" + params.toString(), { - method: "GET", - headers: { - "X-App": "danmu-sim", - "X-Authenticator": "basic", - authorization: "Bearer " + token?.value, - "content-type": "application/json", - }, - }); - const result = await songs.json(); - console.log(result.data); - return result?.data; + revalidateTag("songs"); } export async function listSongs(): Promise { const token = cookies().get("token"); const params = new URLSearchParams(); - params.append('pageSize', '99999'); - params.append('filter', JSON.stringify({"$and":[{"createdBy":{"id":{"$eq":"{{$user.id}}"}}}]})) - const songs = await fetch("https://tachy.daoyoucloud.com/api/songs:list?" + params.toString(), { - method: "GET", - headers: { - "X-App": "danmu-sim", - "X-Authenticator": "basic", - authorization: "Bearer " + token?.value, - "content-type": "application/json", - }, - }); + params.append("pageSize", "99999"); + params.append( + "filter", + JSON.stringify({ $and: [{ createdBy: { id: { $eq: "{{$user.id}}" } } }] }) + ); + const songs = await fetch( + "https://tachy.daoyoucloud.com/api/songs:list?" + params.toString(), + { + method: "GET", + headers: { + "X-App": "danmu-sim", + "X-Authenticator": "basic", + authorization: "Bearer " + token?.value, + "content-type": "application/json", + }, + next: { + tags: ["songs"], + }, + } + ); const result = await songs.json(); return result?.data ?? []; } + +export async function deleteSong(id: number): Promise { + const token = cookies().get("token"); + const params = new URLSearchParams(); + params.append('filterByTk', id.toString()); + await fetch( + "https://tachy.daoyoucloud.com/api/songs:destroy?" + params.toString(), + { + method: "GET", + headers: { + "X-App": "danmu-sim", + "X-Authenticator": "basic", + authorization: "Bearer " + token?.value, + "content-type": "application/json", + }, + } + ); + revalidateTag("songs"); +} diff --git a/app/songs/delete-song-button.tsx b/app/songs/delete-song-button.tsx new file mode 100644 index 0000000..86282f1 --- /dev/null +++ b/app/songs/delete-song-button.tsx @@ -0,0 +1,21 @@ +"use client"; +import { deleteSong } from "./actions"; + +export default function DeleteSongButton({ + className, + id, +}: { + className?: string | undefined; + id: number; +}) { + return ( + + ); +} diff --git a/app/songs/page.tsx b/app/songs/page.tsx index e8e8463..7f2f7cc 100644 --- a/app/songs/page.tsx +++ b/app/songs/page.tsx @@ -1,4 +1,5 @@ -import { listSongs } from "./actions"; +import { deleteSong, listSongs } from "./actions"; +import DeleteSongButton from "./delete-song-button"; import SearchForm from "./search-form"; export default async function Page() { @@ -18,9 +19,10 @@ export default async function Page() { {song.name} - {song.artist} - + ))}