53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { Link } from '@inertiajs/react';
|
|
import { useEffect, useState } from 'react';
|
|
export default function PartnershipSourceLinks({
|
|
type,
|
|
id,
|
|
}: {
|
|
type: string;
|
|
id: string | number;
|
|
}) {
|
|
const [data, setData] = useState<{
|
|
available: boolean;
|
|
items: { id: number; title: string }[];
|
|
} | null>(null);
|
|
useEffect(() => {
|
|
const controller = new AbortController();
|
|
fetch(
|
|
`/api/v1/partnerships/source/${encodeURIComponent(type)}/${encodeURIComponent(String(id))}`,
|
|
{
|
|
signal: controller.signal,
|
|
headers: { Accept: 'application/json' },
|
|
},
|
|
)
|
|
.then((r) => (r.ok ? r.json() : null))
|
|
.then(setData)
|
|
.catch(() => {});
|
|
return () => controller.abort();
|
|
}, [type, id]);
|
|
if (!data?.available) return null;
|
|
return (
|
|
<section className="my-5 space-y-3 rounded border p-4">
|
|
<h2 className="text-xl font-bold">
|
|
Parcerias ligadas a este registo
|
|
</h2>
|
|
{data.items.map((item) => (
|
|
<p key={item.id}>
|
|
<Link className="underline" href={'/parcerias/' + item.id}>
|
|
{item.title}
|
|
</Link>
|
|
</p>
|
|
))}
|
|
{!data.items.length && (
|
|
<p>Ainda não há parcerias publicadas com esta origem.</p>
|
|
)}
|
|
<Link
|
|
className="underline"
|
|
href={`/parcerias/criar?origin_type=${encodeURIComponent(type)}&origin_id=${encodeURIComponent(String(id))}`}
|
|
>
|
|
Propor parceria a partir deste registo
|
|
</Link>
|
|
</section>
|
|
);
|
|
}
|