diff --git a/app/api/settings/tts/filter/users/route.ts b/app/api/settings/tts/filter/users/route.ts new file mode 100644 index 0000000..81cdf11 --- /dev/null +++ b/app/api/settings/tts/filter/users/route.ts @@ -0,0 +1,82 @@ +import { db } from "@/lib/db" +import { NextResponse } from "next/server"; +import fetchUserUsingAPI from "@/lib/validate-api"; + +export async function GET(req: Request) { + try { + const user = await fetchUserUsingAPI(req) + if (!user) { + return new NextResponse("Unauthorized", { status: 401 }); + } + + const filters = await db.ttsUsernameFilter.findMany({ + where: { + userId: user.id + } + }); + + return NextResponse.json(filters); + } catch (error) { + console.log("[TTS/FILTER/USER]", error); + return new NextResponse("Internal Error", { status: 500 }); + } +} + +export async function POST(req: Request) { + try { + const user = await fetchUserUsingAPI(req) + if (!user) { + return new NextResponse("Unauthorized", { status: 401 }); + } + + const { username, tag } = await req.json(); + + const filter = await db.ttsUsernameFilter.upsert({ + where: { + userId_username: { + userId: user.id as string, + username + } + }, + update: { + tag + }, + create: { + userId: user.id as string, + username, + tag + } + }); + + return NextResponse.json(filter); + } catch (error) { + console.log("[TTS/FILTER/USER]", error); + return new NextResponse("Internal Error", { status: 500 }); + } +} + +export async function DELETE(req: Request) { + try { + const user = await fetchUserUsingAPI(req) + if (!user) { + return new NextResponse("Unauthorized", { status: 401 }); + } + + const { searchParams } = new URL(req.url) + const username = searchParams.get('username') as string + + const filter = await db.ttsUsernameFilter.delete({ + where: { + userId_username: { + userId: user.id as string, + username + } + } + }); + + return NextResponse.json(filter) + } catch (error) { + console.log("[TTS/FILTER/USER]", error); + return new NextResponse("Internal Error", { status: 500 }); + } +} \ No newline at end of file diff --git a/app/settings/tts/filters/page.tsx b/app/settings/tts/filters/page.tsx index a74a319..2a11534 100644 --- a/app/settings/tts/filters/page.tsx +++ b/app/settings/tts/filters/page.tsx @@ -3,7 +3,7 @@ import axios from "axios"; import * as React from 'react'; import { Calendar, Check, ChevronsUpDown, MoreHorizontal, Plus, Tags, Trash, User } from "lucide-react" -import { ApiKey, TwitchConnection } from "@prisma/client"; +import { ApiKey, TtsUsernameFilter, TwitchConnection } from "@prisma/client"; import { useEffect, useState } from "react"; import { useSession } from "next-auth/react"; import Link from "next/link"; @@ -23,6 +23,7 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { DropdownMenu, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger } from "@/components/ui/dropdown-menu"; import { DropdownMenuContent, DropdownMenuTrigger } from "@radix-ui/react-dropdown-menu"; import { Label } from "@/components/ui/label"; +import { db } from "@/lib/db"; const TTSFiltersPage = () => { const { data: session, status } = useSession(); @@ -30,33 +31,36 @@ const TTSFiltersPage = () => { const [moreOpen, setMoreOpen] = useState(0) const [tag, setTag] = useState("blacklisted") const [open, setOpen] = useState(false) - const [userTags, setUserTag] = useState<{ username: string, tag: string }[]>([{ username: "test", tag:"blacklisted" }, { username: "hello world", tag:"moderator" }]) + const [userTags, setUserTag] = useState<{ username: string, tag: string }[]>([]) const router = useRouter(); - const labels = [ + const tags = [ "blacklisted", "priority" ] // Username blacklist const usernameFilteredFormSchema = z.object({ - username: z.string().trim().min(4).max(25).regex(new RegExp("[a-zA-Z0-9][a-zA-Z0-9_]{3, 24}"), "Must be a valid twitch username.") + //userId: z.string().trim().min(1), + username: z.string().trim().min(4).max(25), //.regex(new RegExp("[a-zA-Z0-9][a-zA-Z0-9_]{3, 24}"), "Must be a valid twitch username.") + tag: z.string().trim() }); const usernameFilteredForm = useForm({ resolver: zodResolver(usernameFilteredFormSchema), defaultValues: { - username: "" + //userId: session?.user?.id ?? "", + username: "", + tag: "" } }); useEffect(() => { const fetchData = async () => { try { - // const userFiltersData = (await axios.get("/api/settings/tts/filters/users")).data ?? {}; - // setApiKeys(userFiltersData) - // console.log(userFiltersData); + const userFiltersData = await axios.get("/api/settings/tts/filter/users"); + setUserTag(userFiltersData.data ?? []) } catch (error) { console.log("ERROR", error) } @@ -65,47 +69,31 @@ const TTSFiltersPage = () => { fetchData().catch(console.error); }, []); - const onApiKeyAdd = async () => { - ///let usernames = blacklistedUsers.split("\n"); - //console.log(usernames) - // try { - // await axios.post("/api/settings/tts/filters/users", { - // usernames: [] - // }); - // setUserFilters(userFilters.concat(usernames)) - // } catch (error) { - // console.log("ERROR", error) - // } - } - - const onApiKeyDelete = async (username: string) => { - try { - await axios.delete("/api/settings/tts/filters/users/" + username); - //setUserFilters(userFilters.filter((u) => u != username)) - } catch (error) { - console.log("ERROR", error) - } + const onDelete = () => { + const username = userTags[Math.log2(moreOpen)].username + axios.delete("/api/settings/tts/filter/users?username=" + username) + .then(() => { + setUserTag(userTags.filter((u) => u.username != username)) + }).catch((e) => console.error(e)) } const isLoading = usernameFilteredForm.formState.isSubmitting; - const addTwitchUser = (values: z.infer) => { - let response = null; - console.log("TEST") - console.log(values) + const onAdd = (values: z.infer) => { + try { + values.tag = tag + axios.post("/api/settings/tts/filter/users", values) + .then((d) => { + userTags.push({ username: values.username, tag: tag }) + setUserTag(userTags) - // try { - // response = await axios.post("/api/settings/tts/filter/badges", values); - // } catch (error) { - // console.log("[CONNECTIONS/TWITCH/POST]", error); - // return; - // } - - userTags.push({ username: values.username, tag: tag }) - setUserTag(userTags) - usernameFilteredForm.reset(); - router.refresh(); - //window.location.reload(); + usernameFilteredForm.reset(); + router.refresh(); + }) + } catch (error) { + console.log("[TTS/FILTERS/USER]", error); + return; + } } return ( @@ -114,12 +102,12 @@ const TTSFiltersPage = () => {
{userTags.map((user, index) => ( -
-

+

+

{user.tag} - {user.username} + {user.username}

0} onOpenChange={() => setMoreOpen((v) => v ^ (1 << index))}> @@ -144,17 +132,17 @@ const TTSFiltersPage = () => { No label found. - {labels.map((label) => ( + {tags.map((tag) => ( { userTags[index].tag = value setUserTag(userTags) setMoreOpen(0) }} > - {label} + {tag} ))} @@ -163,10 +151,7 @@ const TTSFiltersPage = () => { - { - userTags.splice(index, 1) - setUserTag(userTags) - }} className="text-red-600"> + Delete @@ -176,9 +161,9 @@ const TTSFiltersPage = () => {
))}
- -
-