import { useQuery } from "@tanstack/react-query"; import { getAlbum, type Artist } from "api/api"; import { useEffect, useState } from "react" interface Props { id: number type: string } export default function SetPrimaryArtist({ id, type }: Props) { const [err, setErr] = useState('') const [primary, setPrimary] = useState() const [success, setSuccess] = useState('') const { isPending, isError, data, error } = useQuery({ queryKey: [ 'get-artists-'+type.toLowerCase(), { id: id }, ], queryFn: () => { return fetch('/apis/web/v1/artists?'+type.toLowerCase()+'_id='+id).then(r => r.json()) as Promise; }, }); useEffect(() => { if (data) { for (let a of data) { if (a.is_primary) { setPrimary(a) break } } } }, [data]) if (isError) { return (

Error: {error.message}

) } if (isPending) { return (

Loading...

) } const updatePrimary = (artist: number, val: boolean) => { setErr(''); setSuccess(''); fetch(`/apis/web/v1/artists/primary?artist_id=${artist}&${type.toLowerCase()}_id=${id}&is_primary=${val}`, { method: 'POST', headers: { "Content-Type": "application/x-www-form-urlencoded" } }) .then(r => { if (r.ok) { setSuccess('successfully updated primary artists'); } else { r.json().then(r => setErr(r.error)); } }); } return (

Set Primary Artist

{err &&

{err}

} {success &&

{success}

}
); }