@@ -0,0 +1,335 @@
|
||||
import { router } from '@inertiajs/react';
|
||||
import { useState } from 'react';
|
||||
import { profileJson, ProfileValidationError } from '@/lib/profile-json';
|
||||
import { button, control } from '@/pages/management/shared';
|
||||
|
||||
export default function OpportunityProfileActions({
|
||||
id,
|
||||
etag,
|
||||
actions,
|
||||
gates,
|
||||
documents,
|
||||
approvalHash,
|
||||
onErrors,
|
||||
participants = [],
|
||||
publishers = [],
|
||||
opinions = [],
|
||||
publicationVisibility,
|
||||
}: {
|
||||
id: number;
|
||||
etag: string;
|
||||
actions: string[];
|
||||
gates: string[];
|
||||
documents: { id: string; title: string }[];
|
||||
approvalHash?: string;
|
||||
publicationVisibility?: string;
|
||||
onErrors?: (errors: Record<string, string>) => void;
|
||||
publishers?: { id: number; name: string }[];
|
||||
participants?: {
|
||||
id: number;
|
||||
name: string;
|
||||
technical: boolean;
|
||||
legal: boolean;
|
||||
}[];
|
||||
opinions?: {
|
||||
id: string;
|
||||
gate: string;
|
||||
recipient_id: number;
|
||||
status: string;
|
||||
}[];
|
||||
}) {
|
||||
const [reason, setReason] = useState('');
|
||||
const [items, setItems] = useState('');
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [message, setMessage] = useState('');
|
||||
const [gate, setGate] = useState(gates[0] ?? '');
|
||||
const [decision, setDecision] = useState('approved');
|
||||
const [evidence, setEvidence] = useState<string[]>([]);
|
||||
const [assignee, setAssignee] = useState('');
|
||||
const [confirmed, setConfirmed] = useState(false);
|
||||
const [recipient, setRecipient] = useState('');
|
||||
const [opinion, setOpinion] = useState('');
|
||||
const [actionErrors, setActionErrors] = useState<Record<string, string>>(
|
||||
{},
|
||||
);
|
||||
const labels: Record<string, string> = {
|
||||
submit: 'Submeter para validação',
|
||||
approve: 'Aprovar versão',
|
||||
publish:
|
||||
publicationVisibility === 'public'
|
||||
? 'Publicar no catálogo público'
|
||||
: 'Disponibilizar com acesso restrito',
|
||||
request_revision: 'Preparar alteração material',
|
||||
review_gate: 'Guardar revisão especializada',
|
||||
assign_review: 'Atribuir validação',
|
||||
assign_publication: 'Atribuir publicação',
|
||||
appeal_uphold: 'Manter rejeição após recurso',
|
||||
appeal_reopen: 'Reabrir para correção',
|
||||
request_archive: 'Solicitar arquivo',
|
||||
appeal: 'Apresentar recurso',
|
||||
start_review: 'Iniciar validação',
|
||||
request_correction: 'Solicitar correções',
|
||||
pending_opinion: 'Solicitar parecer',
|
||||
respond_opinion: 'Responder ao parecer',
|
||||
reject: 'Rejeitar com fundamento',
|
||||
suspend: 'Suspender',
|
||||
archive: 'Arquivar',
|
||||
};
|
||||
async function send(action: string) {
|
||||
setBusy(true);
|
||||
setActionErrors({});
|
||||
onErrors?.({});
|
||||
try {
|
||||
const result = await profileJson(
|
||||
`/minhas-oportunidades/${id}/workflow`,
|
||||
{
|
||||
etag,
|
||||
action,
|
||||
reason,
|
||||
correction_items: items
|
||||
.split('\n')
|
||||
.map((v) => v.trim())
|
||||
.filter(Boolean),
|
||||
...(['review_gate', 'respond_opinion'].includes(action)
|
||||
? {
|
||||
gate,
|
||||
gate_decision: decision,
|
||||
evidence_document_ids: evidence,
|
||||
}
|
||||
: {}),
|
||||
...(action === 'pending_opinion'
|
||||
? { gate, recipient_user_id: Number(recipient) }
|
||||
: {}),
|
||||
...(action === 'respond_opinion'
|
||||
? { opinion_id: opinion }
|
||||
: {}),
|
||||
...(['assign_review', 'assign_publication'].includes(action)
|
||||
? { assignee_user_id: Number(assignee) }
|
||||
: {}),
|
||||
...(action === 'publish'
|
||||
? {
|
||||
confirm_publication: confirmed,
|
||||
preview_hash: approvalHash,
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
);
|
||||
setMessage(result.message);
|
||||
router.visit(`/minhas-oportunidades/${id}/view`);
|
||||
} catch (error) {
|
||||
if (error instanceof ProfileValidationError) {
|
||||
setActionErrors(error.fields);
|
||||
onErrors?.(error.fields);
|
||||
}
|
||||
setMessage(
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: 'Resposta indisponível. Confira o estado antes de repetir.',
|
||||
);
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
return (
|
||||
<section
|
||||
className="space-y-3 rounded border p-4"
|
||||
aria-label="Workflow interno"
|
||||
>
|
||||
<h2 className="text-lg font-semibold">Workflow interno</h2>
|
||||
<p>
|
||||
Guarde o contrato antes de submeter. Aprovação exige revisões
|
||||
especializadas desta versão; publicação exige outro utilizador
|
||||
autorizado. Uma decisão interna não substitui decisão externa.
|
||||
</p>
|
||||
<p>
|
||||
Aprovação interna confirma a revisão desta versão.
|
||||
Disponibilização restrita não aparece no catálogo público nem dá
|
||||
acesso a todas as contas. A publicação pública exige âmbito
|
||||
público e ação independente do Publicador; não representa uma
|
||||
decisão externa.
|
||||
</p>
|
||||
{!!actions.length && (
|
||||
<>
|
||||
<label className="block">
|
||||
Fundamentação
|
||||
<textarea
|
||||
className={control}
|
||||
minLength={20}
|
||||
maxLength={1000}
|
||||
value={reason}
|
||||
onChange={(e) => setReason(e.target.value)}
|
||||
/>
|
||||
</label>
|
||||
{actions.includes('request_correction') && (
|
||||
<label className="block">
|
||||
Campos ou documentos a corrigir, um por linha
|
||||
<textarea
|
||||
className={control}
|
||||
value={items}
|
||||
onChange={(e) => setItems(e.target.value)}
|
||||
/>
|
||||
</label>
|
||||
)}
|
||||
{(actions.includes('review_gate') ||
|
||||
actions.includes('pending_opinion') ||
|
||||
actions.includes('respond_opinion')) && (
|
||||
<fieldset>
|
||||
<legend>Revisão especializada</legend>
|
||||
<label>
|
||||
Revisão
|
||||
<select
|
||||
className={control}
|
||||
value={gate}
|
||||
onChange={(e) => setGate(e.target.value)}
|
||||
>
|
||||
{gates.map((g) => (
|
||||
<option key={g} value={g}>
|
||||
{g.replaceAll('_', ' ')}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
Decisão
|
||||
<select
|
||||
className={control}
|
||||
value={decision}
|
||||
onChange={(e) =>
|
||||
setDecision(e.target.value)
|
||||
}
|
||||
>
|
||||
<option value="approved">Aprovada</option>
|
||||
<option value="rejected">Rejeitada</option>
|
||||
</select>
|
||||
</label>
|
||||
{documents.map((d) => (
|
||||
<label key={d.id} className="block">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={evidence.includes(d.id)}
|
||||
onChange={(e) =>
|
||||
setEvidence(
|
||||
e.target.checked
|
||||
? [...evidence, d.id]
|
||||
: evidence.filter(
|
||||
(id) => id !== d.id,
|
||||
),
|
||||
)
|
||||
}
|
||||
/>{' '}
|
||||
{d.title}
|
||||
</label>
|
||||
))}
|
||||
</fieldset>
|
||||
)}
|
||||
{(actions.includes('assign_review') ||
|
||||
actions.includes('assign_publication')) && (
|
||||
<label className="block">
|
||||
Responsável a designar
|
||||
<select
|
||||
className={control}
|
||||
value={assignee}
|
||||
onChange={(e) => setAssignee(e.target.value)}
|
||||
>
|
||||
<option value="">Por atribuir</option>
|
||||
{(actions.includes('assign_publication')
|
||||
? publishers
|
||||
: participants
|
||||
).map((p) => (
|
||||
<option key={p.id} value={p.id}>
|
||||
{p.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<span>
|
||||
O servidor verifica o papel, âmbito e
|
||||
independência. Nenhum responsável é atribuído
|
||||
automaticamente.
|
||||
</span>
|
||||
</label>
|
||||
)}
|
||||
{actions.includes('pending_opinion') && (
|
||||
<label className="block">
|
||||
Destinatário do parecer
|
||||
<select
|
||||
className={control}
|
||||
value={recipient}
|
||||
onChange={(e) => setRecipient(e.target.value)}
|
||||
>
|
||||
<option value="">Por designar</option>
|
||||
{participants
|
||||
.filter((p) =>
|
||||
/legal|registry|authority|confidentiality|license/.test(
|
||||
gate,
|
||||
)
|
||||
? p.legal
|
||||
: p.technical,
|
||||
)
|
||||
.map((p) => (
|
||||
<option key={p.id} value={p.id}>
|
||||
{p.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
{actions.includes('respond_opinion') && (
|
||||
<label className="block">
|
||||
Parecer atribuído
|
||||
<select
|
||||
className={control}
|
||||
value={opinion}
|
||||
onChange={(e) => setOpinion(e.target.value)}
|
||||
>
|
||||
<option value="">Selecione</option>
|
||||
{opinions
|
||||
.filter((o) => o.status === 'pending')
|
||||
.map((o) => (
|
||||
<option key={o.id} value={o.id}>
|
||||
{o.gate.replaceAll('_', ' ')} —
|
||||
conta {o.recipient_id}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
{Object.entries(actionErrors)
|
||||
.filter(([key]) => !key.startsWith('canonical'))
|
||||
.map(([key, msg]) => (
|
||||
<p key={key} role="alert">
|
||||
{key}: {msg}
|
||||
</p>
|
||||
))}
|
||||
{actions.includes('publish') && (
|
||||
<label className="block">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={confirmed}
|
||||
onChange={(e) => setConfirmed(e.target.checked)}
|
||||
/>{' '}
|
||||
Confirmei a pré-visualização e o âmbito de
|
||||
divulgação desta versão.
|
||||
</label>
|
||||
)}
|
||||
<div className="flex flex-wrap gap-3">
|
||||
{actions.map((action) => (
|
||||
<button
|
||||
key={action}
|
||||
className={button}
|
||||
disabled={
|
||||
busy || (action === 'publish' && !confirmed)
|
||||
}
|
||||
onClick={() => void send(action)}
|
||||
>
|
||||
{labels[action] ?? action}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
<p role="status" aria-live="polite">
|
||||
{message}
|
||||
</p>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user