import { useQuery } from "@tanstack/react-query"; import { createAlias, deleteAlias, getAliases, setPrimaryAlias, type Alias } from "api/api"; import { Modal } from "../Modal"; import { AsyncButton } from "../../AsyncButton"; import { useEffect, useState } from "react"; import { Trash } from "lucide-react"; import SetVariousArtists from "./SetVariousArtist"; import SetPrimaryArtist from "./SetPrimaryArtist"; interface Props { type: string id: number open: boolean setOpen: Function } export default function EditModal({ open, setOpen, type, id }: Props) { const [input, setInput] = useState('') const [loading, setLoading ] = useState(false) const [err, setError ] = useState() const [displayData, setDisplayData] = useState([]) const { isPending, isError, data, error } = useQuery({ queryKey: [ 'aliases', { type: type, id: id }, ], queryFn: ({ queryKey }) => { const params = queryKey[1] as { type: string; id: number }; return getAliases(params.type, params.id); }, }); useEffect(() => { if (data) { setDisplayData(data) } }, [data]) if (isError) { return (

Error: {error.message}

) } if (isPending) { return (

Loading...

) } const handleSetPrimary = (alias: string) => { setError(undefined) setLoading(true) setPrimaryAlias(type, id, alias) .then(r => { if (r.ok) { window.location.reload() } else { r.json().then((r) => setError(r.error)) } }) setLoading(false) } const handleNewAlias = () => { setError(undefined) if (input === "") { setError("alias must be provided") return } setLoading(true) createAlias(type, id, input) .then(r => { if (r.ok) { setDisplayData([...displayData, {alias: input, source: "Manual", is_primary: false, id: id}]) } else { r.json().then((r) => setError(r.error)) } }) setLoading(false) } const handleDeleteAlias = (alias: string) => { setError(undefined) setLoading(true) deleteAlias(type, id, alias) .then(r => { if (r.ok) { setDisplayData(displayData.filter((v) => v.alias != alias)) } else { r.json().then((r) => setError(r.error)) } }) setLoading(false) } const handleClose = () => { setOpen(false) setInput('') } return (

Alias Manager

{displayData.map((v) => (
{v.alias} (source: {v.source})
handleSetPrimary(v.alias)} disabled={v.is_primary}>Set Primary handleDeleteAlias(v.alias)} confirm disabled={v.is_primary}>
))}
setInput(e.target.value)} /> Submit
{err &&

{err}

}
{ type.toLowerCase() === "album" && <> }
) }