100 lines
4.3 KiB
TypeScript
100 lines
4.3 KiB
TypeScript
import { Link, router } from "@inertiajs/react";
|
|
import { useState } from "react";
|
|
import { Frame, Field, input, button, card, api } from "./shared";
|
|
import { businessModalities, businessStates } from "./business-types";
|
|
import type { Business } from "./business-types";
|
|
|
|
export default function Businesses({
|
|
records,
|
|
management,
|
|
modalities,
|
|
}: {
|
|
records: { data: Business[]; prev_page_url: string | null; next_page_url: string | null };
|
|
management: boolean;
|
|
modalities: string[];
|
|
}) {
|
|
const [title, setTitle] = useState("");
|
|
const [modality, setModality] = useState(modalities[0]);
|
|
const [error, setError] = useState("");
|
|
const [busy, setBusy] = useState(false);
|
|
return (
|
|
<Frame title={management ? "Revisão de negócios" : "Os meus negócios"}>
|
|
<p>
|
|
Área privada de preparação e revisão. A submissão não confirma titularidade,
|
|
validade ou transmissibilidade do negócio, activo ou direito.
|
|
</p>
|
|
{!management && (
|
|
<form
|
|
className={`${card} space-y-4`}
|
|
onSubmit={async (e) => {
|
|
e.preventDefault();
|
|
setBusy(true);
|
|
setError("");
|
|
try {
|
|
const result = await api("/api/v1/existing-business/listings", {
|
|
modality,
|
|
classification: "confidential",
|
|
reason: "Criação de rascunho pelo proprietário",
|
|
payload: { title, wizard_version: "1", wizard_step: "1" },
|
|
});
|
|
router.visit(`/investidor/negocios/${result.id}`);
|
|
} catch (err) {
|
|
setError(
|
|
err instanceof Error
|
|
? err.message
|
|
: "Não foi possível criar o rascunho.",
|
|
);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}}
|
|
>
|
|
<h2 className="text-xl font-bold">Preparar negócio</h2>
|
|
<Field label="Título privado">
|
|
<input
|
|
className={input}
|
|
value={title}
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
required
|
|
maxLength={200}
|
|
/>
|
|
</Field>
|
|
<Field label="Modalidade">
|
|
<select
|
|
className={input}
|
|
value={modality}
|
|
onChange={(e) => setModality(e.target.value)}
|
|
>
|
|
{modalities.map((m) => (
|
|
<option key={m} value={m}>
|
|
{businessModalities[m] ?? m}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
{error && <p role="alert">{error}</p>}
|
|
<button className={button} disabled={busy}>
|
|
{busy ? "A criar…" : "Criar rascunho privado"}
|
|
</button>
|
|
</form>
|
|
)}
|
|
{!records.data.length && <p role="status">Não existem negócios nesta lista.</p>}
|
|
{records.data.map((r) => (
|
|
<article className={card} key={r.id}>
|
|
<Link className="font-bold underline" href={`/investidor/negocios/${r.id}`}>
|
|
{r.payload.title}
|
|
</Link>
|
|
<p>
|
|
{businessModalities[r.modality]} · {businessStates[r.status]} · versão{" "}
|
|
{r.version_no}
|
|
</p>
|
|
</article>
|
|
))}
|
|
<nav aria-label="Paginação dos negócios" className="flex gap-4">
|
|
{records.prev_page_url && <Link href={records.prev_page_url}>Anterior</Link>}
|
|
{records.next_page_url && <Link href={records.next_page_url}>Seguinte</Link>}
|
|
</nav>
|
|
</Frame>
|
|
);
|
|
}
|