135 lines
5.9 KiB
TypeScript
135 lines
5.9 KiB
TypeScript
import { useForm } from "@inertiajs/react";
|
|
import { useState } from "react";
|
|
import { newSubmissionKey } from "@/lib/portal-api";
|
|
import { Field, card, input, button, api } from "./shared";
|
|
export type Message = { id: string; sender_role: string; body: string; created_at: string };
|
|
export function Conversation({
|
|
id,
|
|
messages,
|
|
submitted,
|
|
revisions,
|
|
}: {
|
|
id: string;
|
|
messages: Message[];
|
|
submitted: boolean;
|
|
revisions: { version: number; created_at: string }[];
|
|
}) {
|
|
const form = useForm({ client_key: newSubmissionKey(), body: "" });
|
|
const [revision, setRevision] = useState<{
|
|
version: number;
|
|
snapshot: {
|
|
payload: {
|
|
full_name?: string;
|
|
organisation?: string;
|
|
objective?: string;
|
|
horizon?: string;
|
|
};
|
|
needs: { details: { description: string } }[];
|
|
attachment_ids: string[];
|
|
};
|
|
} | null>(null);
|
|
const [error, setError] = useState("");
|
|
return (
|
|
<>
|
|
<section className={card + " space-y-4"}>
|
|
<h2 className="text-xl font-bold">Versões guardadas</h2>
|
|
<Field label="Consultar versão">
|
|
<select
|
|
className={input}
|
|
defaultValue=""
|
|
onChange={(e) => {
|
|
setError("");
|
|
if (!e.target.value) {
|
|
setRevision(null);
|
|
return;
|
|
}
|
|
api(`/api/v1/investment/submissions/${id}/revisions/${e.target.value}`)
|
|
.then(setRevision)
|
|
.catch(() => setError("Não foi possível consultar esta versão."));
|
|
}}
|
|
>
|
|
<option value="">Seleccionar versão</option>
|
|
{revisions.map((r) => (
|
|
<option value={r.version} key={r.version}>
|
|
Versão {r.version} —{" "}
|
|
{new Date(r.created_at).toLocaleString("pt-MZ")}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
{error && <p role="alert">{error}</p>}
|
|
{revision && (
|
|
<div className="space-y-2">
|
|
<h3 className="font-bold">Versão {revision.version}</h3>
|
|
<p>Investidor: {revision.snapshot.payload.full_name}</p>
|
|
<p>Empresa: {revision.snapshot.payload.organisation || "Não indicada"}</p>
|
|
<p className="whitespace-pre-line">
|
|
Objectivo: {revision.snapshot.payload.objective || "Não indicado"}
|
|
</p>
|
|
<p>Prazo: {revision.snapshot.payload.horizon || "Não indicado"}</p>
|
|
<ul>
|
|
{revision.snapshot.needs.map((n, i) => (
|
|
<li key={i}>{n.details.description}</li>
|
|
))}
|
|
</ul>
|
|
<p>
|
|
{revision.snapshot.attachment_ids.length} anexos associados nessa
|
|
versão.
|
|
</p>
|
|
</div>
|
|
)}
|
|
</section>
|
|
{submitted && (
|
|
<section className={card + " space-y-4"}>
|
|
<h2 className="text-xl font-bold">Conversa com o apoio</h2>
|
|
<p>
|
|
As mensagens ficam registadas neste pedido e são visíveis ao investidor e à
|
|
equipa autorizada.
|
|
</p>
|
|
{messages.map((m) => (
|
|
<article key={m.id} className="rounded-lg border p-4">
|
|
<p className="font-semibold">
|
|
{m.sender_role === "support" ? "Apoio ao Investidor" : "Investidor"}{" "}
|
|
· {new Date(m.created_at).toLocaleString("pt-MZ")}
|
|
</p>
|
|
<p className="mt-2 whitespace-pre-line">{m.body}</p>
|
|
</article>
|
|
))}
|
|
{!messages.length && <p>Ainda não existem mensagens neste pedido.</p>}
|
|
<form
|
|
className="space-y-3"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
form.post(`/investidor/manifestacoes/${id}/messages`, {
|
|
preserveScroll: true,
|
|
onSuccess: () =>
|
|
form.setData({ client_key: newSubmissionKey(), body: "" }),
|
|
});
|
|
}}
|
|
>
|
|
<Field label="Mensagem para acompanhamento">
|
|
<textarea
|
|
className={input}
|
|
rows={4}
|
|
required
|
|
minLength={3}
|
|
maxLength={5000}
|
|
value={form.data.body}
|
|
onChange={(e) => form.setData("body", e.target.value)}
|
|
/>
|
|
</Field>
|
|
{Object.entries(form.errors).map(([key, value]) => (
|
|
<p role="alert" key={key}>
|
|
{value}
|
|
</p>
|
|
))}
|
|
<button className={button} disabled={form.processing}>
|
|
Enviar mensagem
|
|
</button>
|
|
</form>
|
|
</section>
|
|
)}
|
|
</>
|
|
);
|
|
}
|