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";
import BusinessObjects from "./business-objects";
import type { BusinessObject } from "./business-objects";
import BusinessVerifications from "./business-verifications";
import type { BusinessVerification } from "./business-verifications";
import BusinessWizard from "./business-wizard";
import type { FormSchema } from "./business-wizard";
type Props = {
formSchema: FormSchema;
sectors: { id: number; name: string }[];
record: Business;
canEdit: boolean;
owner: boolean;
canReview: boolean;
modalities: string[];
objects: BusinessObject[];
objectKinds: string[];
verificationChecks: string[];
verifications: BusinessVerification[];
history: { version_no: number; action: string; created_at: string; note: string }[];
};
export default function BusinessPage(props: Props) {
if (props.canEdit)
return ;
return ;
}
function Editor({
record,
canEdit,
owner,
canReview,
modalities,
history,
objects,
objectKinds,
verificationChecks,
verifications,
formSchema,
sectors,
}: Props) {
const [payload, setPayload] = useState(record.payload);
const [modality, setModality] = useState(record.modality);
const [note, setNote] = useState("");
const [consent, setConsent] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState("");
const dirty =
JSON.stringify(payload) !== JSON.stringify(record.payload) || modality !== record.modality;
const run = async (action?: string) => {
setBusy(true);
setError("");
try {
await api(
`/api/v1/existing-business/listings/${record.id}${action ? "/transitions" : ""}`,
action
? { action, version_no: record.version_no, note, review_consent: consent }
: {
_method: "PUT",
modality,
classification: record.classification,
payload,
reason: note,
version_no: record.version_no,
},
);
router.reload();
} catch (err) {
setError(
err instanceof Error
? err.message
: "Não foi possível guardar. Recarregue se a versão mudou.",
);
} finally {
setBusy(false);
}
};
return (
Voltar à lista
{businessStates[record.status]} · versão {record.version_no} · informação reservada
Informação declarada, pendente de validação. Não constitui anúncio público,
certificação, avaliação ou autorização de transmissão.
{!canEdit && (
- Sector
-
{sectors.find((sector) => sector.id === record.sector_id)?.name ?? "—"}
{Object.entries(formSchema)
.filter(
([key]) =>
![
"title",
"description",
"declared_reason",
"source_reference",
"valid_until",
].includes(key),
)
.map(([key, field]) => (
- {field.pt}
-
{record.payload[key] || "—"}
))}
)}
{owner && canEdit && (
)}
{owner && canEdit && (
)}
{canReview && record.status === "submitted" && (
)}
{canReview && record.status === "in_review" && (
)}
{owner && record.status !== "withdrawn" && (
)}
Histórico
{history.map((h) => (
-
Versão {h.version_no} ·{" "}
{businessStates[h.action] ??
(
{
created: "Criado",
updated: "Alterado",
archived: "Arquivado",
object_added: "Elemento declarado acrescentado",
object_updated: "Elemento declarado alterado",
verification: "Verificação registada",
} as Record
)[h.action] ??
h.action}{" "}
· {new Date(h.created_at).toLocaleString("pt-MZ")}
{h.note}
))}
);
}