616 lines
29 KiB
TypeScript
616 lines
29 KiB
TypeScript
import { Link, useForm } from '@inertiajs/react';
|
|
import { Management, Field, button, control } from './shared';
|
|
import { states } from '@/lib/editorial-states';
|
|
import { labels as portfolioLabels } from '../portfolio/shared';
|
|
import SubjectForm from './editorial-subject-form';
|
|
import type { SubjectField } from './editorial-subject-form';
|
|
|
|
type Edition = {
|
|
id: string;
|
|
revision_no: number;
|
|
version: number;
|
|
state: string;
|
|
content_hash: string;
|
|
legal_required: boolean;
|
|
fresh: boolean;
|
|
payload: {
|
|
resource: {
|
|
title: string;
|
|
description?: string;
|
|
editorial_content?: string | null;
|
|
source?: string;
|
|
publication_version?: string;
|
|
observations?: number;
|
|
sha256?: string;
|
|
dataset_hash?: string;
|
|
report?: { issue_counts?: Record<string, number> };
|
|
content?: Record<string, string | null>;
|
|
_structure?: {
|
|
fields: Record<string, string | number | null>;
|
|
territory_context?: { id: string; name: string }[];
|
|
};
|
|
_subject?: {
|
|
kind: string;
|
|
key: string;
|
|
fields: Record<string, string | number | boolean | null>;
|
|
new_record: boolean;
|
|
withdraw?: boolean;
|
|
};
|
|
};
|
|
files: {
|
|
id: string;
|
|
file_name: string;
|
|
version_no: number;
|
|
sha256: string;
|
|
}[];
|
|
links?: {
|
|
target_type: string;
|
|
target_key: string;
|
|
context?: { name?: string; code?: string; sector?: string };
|
|
}[];
|
|
};
|
|
approval: string | null;
|
|
legal_validation: string | null;
|
|
};
|
|
export default function EditorialEdition({
|
|
resource,
|
|
edition,
|
|
manager,
|
|
legalReviewer,
|
|
history,
|
|
editions,
|
|
reviewers,
|
|
current,
|
|
canApprove,
|
|
independentRequired,
|
|
subject = 'resource',
|
|
subjectKind,
|
|
editUrl,
|
|
subjectFields = [],
|
|
subjectInitial = {},
|
|
}: {
|
|
subject?: 'resource' | 'portfolio' | 'demography' | 'structural';
|
|
subjectKind?: string;
|
|
editUrl?: string;
|
|
subjectFields?: SubjectField[];
|
|
subjectInitial?: Record<string, string | number | boolean | null>;
|
|
resource: {
|
|
id: number | string;
|
|
title: string;
|
|
published: boolean;
|
|
public_edition_no?: number | null;
|
|
};
|
|
edition: Edition | null;
|
|
manager: boolean;
|
|
legalReviewer: boolean;
|
|
history: {
|
|
id: number;
|
|
action: string;
|
|
actor_user_id: number;
|
|
after_data: string;
|
|
occurred_at: string;
|
|
}[];
|
|
editions: { id: string; revision_no: number; state: string }[];
|
|
reviewers: { id: number; name: string }[];
|
|
current: boolean;
|
|
canApprove: boolean;
|
|
independentRequired: boolean;
|
|
}) {
|
|
const editionBase =
|
|
subject === 'structural'
|
|
? `/management/editorial/subjects/${subjectKind}/${resource.id}`
|
|
: `/management/editorial/${subject === 'resource' ? 'resources' : subject}/${resource.id}`;
|
|
const form = useForm({
|
|
version: edition?.version ?? 1,
|
|
action: '',
|
|
reason: '',
|
|
assigned_to: '',
|
|
});
|
|
const available: Record<string, string[]> = {
|
|
draft: ['submit', 'archive'],
|
|
returned: ['submit', 'archive'],
|
|
submitted: ['start_review', 'archive'],
|
|
in_review: ['return', 'reject', 'legal_validate', 'approve', 'archive'],
|
|
approved: ['publish', 'archive'],
|
|
rejected: ['archive'],
|
|
published: ['archive'],
|
|
archived: [],
|
|
};
|
|
const labels: Record<string, string> = {
|
|
submit: 'Submeter',
|
|
start_review: 'Iniciar revisão',
|
|
return: 'Devolver',
|
|
reject: 'Rejeitar',
|
|
legal_validate: 'Validar juridicamente',
|
|
approve: 'Aprovar',
|
|
publish: edition?.payload.resource._subject?.withdraw
|
|
? 'Publicar retirada'
|
|
: 'Publicar',
|
|
archive: 'Arquivar',
|
|
};
|
|
return (
|
|
<Management title={`Edição — ${resource.title}`}>
|
|
<div className="flex flex-wrap gap-4">
|
|
<Link className="underline" href="/management/editorial">
|
|
Fila e notificações
|
|
</Link>
|
|
{subject === 'structural' ? (
|
|
<Link
|
|
className="underline"
|
|
href={editUrl ?? '/management/editorial/subjects'}
|
|
>
|
|
Alterar no contexto original
|
|
</Link>
|
|
) : subject === 'demography' ? (
|
|
<Link
|
|
className="underline"
|
|
href={`/management/demography?batch=${resource.id}`}
|
|
>
|
|
Conferir lote, correspondências e advertências
|
|
</Link>
|
|
) : subject === 'portfolio' ? (
|
|
<Link
|
|
className="underline"
|
|
href={`/management/portfolio/${resource.id}`}
|
|
>
|
|
Alterar ficha da carteira
|
|
</Link>
|
|
) : (
|
|
<>
|
|
<Link
|
|
className="underline"
|
|
href={`/management/portal-services/resources/${resource.id}/article`}
|
|
>
|
|
Alterar artigo
|
|
</Link>
|
|
</>
|
|
)}
|
|
{subject === 'resource' && (
|
|
<Link
|
|
className="underline"
|
|
href="/management/portal-services"
|
|
>
|
|
Alterar metadados/documento
|
|
</Link>
|
|
)}
|
|
</div>
|
|
<p>
|
|
{resource.published
|
|
? 'A versão pública continua disponível até uma publicação ou retirada explícita.'
|
|
: 'Ainda não existe uma versão pública disponível.'}
|
|
</p>
|
|
{resource.public_edition_no && (
|
|
<p>Versão pública: edição {resource.public_edition_no}.</p>
|
|
)}
|
|
{!edition && subject === 'structural' && (
|
|
<SubjectForm
|
|
base={editionBase}
|
|
revision={1}
|
|
fields={subjectFields}
|
|
initial={subjectInitial}
|
|
/>
|
|
)}
|
|
{edition ? (
|
|
<>
|
|
<h2 className="font-semibold">
|
|
Edição {edition.revision_no} ·{' '}
|
|
{states[edition.state] ?? edition.state}
|
|
</h2>
|
|
{!edition.fresh && (
|
|
<p role="alert">
|
|
O conteúdo, contexto ou anexos mudaram ou deixaram
|
|
de estar disponíveis. Guarde uma nova edição e volte
|
|
a conferir antes da aprovação.
|
|
</p>
|
|
)}
|
|
<p className="text-sm break-all">
|
|
Identificador da edição: {edition.id}
|
|
<br />
|
|
Hash do conteúdo e{' '}
|
|
{['portfolio', 'structural'].includes(subject)
|
|
? 'contexto'
|
|
: 'anexos'}
|
|
: {edition.content_hash}
|
|
</p>
|
|
<h3 className="font-semibold">Conferir conteúdo</h3>
|
|
<p>{edition.payload.resource.title}</p>
|
|
{edition.payload.resource._subject?.withdraw && (
|
|
<p role="status">
|
|
Proposta de retirada: o registo actual só será
|
|
retirado depois da publicação desta decisão.
|
|
</p>
|
|
)}
|
|
{subject === 'structural' &&
|
|
edition.payload.resource._subject && (
|
|
<>
|
|
<dl className="grid gap-2 sm:grid-cols-2">
|
|
{subjectFields.map((field) => {
|
|
const value =
|
|
edition.payload.resource._subject
|
|
?.fields[field.name];
|
|
return (
|
|
<div key={field.name}>
|
|
<dt className="font-medium">
|
|
{field.label}
|
|
</dt>
|
|
<dd className="break-words whitespace-pre-wrap">
|
|
{value === null ||
|
|
value === '' ||
|
|
value === undefined
|
|
? 'Não indicado'
|
|
: typeof value ===
|
|
'boolean'
|
|
? value
|
|
? 'Sim'
|
|
: 'Não'
|
|
: String(value)}
|
|
</dd>
|
|
</div>
|
|
);
|
|
})}
|
|
</dl>
|
|
{current && (
|
|
<SubjectForm
|
|
key={edition.id}
|
|
base={editionBase}
|
|
revision={edition.revision_no}
|
|
fields={subjectFields}
|
|
initial={
|
|
edition.payload.resource._subject
|
|
.fields
|
|
}
|
|
withdraw={
|
|
edition.payload.resource._subject
|
|
.withdraw
|
|
}
|
|
/>
|
|
)}
|
|
</>
|
|
)}
|
|
<p className="whitespace-pre-wrap">
|
|
{edition.payload.resource.description}
|
|
</p>
|
|
{subject === 'demography' && (
|
|
<section className="space-y-2 rounded border p-3">
|
|
<p>Fonte: {edition.payload.resource.source}</p>
|
|
<p>
|
|
Exemplar/versão fornecida:{' '}
|
|
{edition.payload.resource.publication_version}
|
|
</p>
|
|
<p>
|
|
{edition.payload.resource.observations}{' '}
|
|
observações conferidas, sem alteração dos
|
|
valores originais.
|
|
</p>
|
|
<a
|
|
className="underline"
|
|
href={`${editionBase}/original`}
|
|
>
|
|
Consultar original fornecido
|
|
</a>
|
|
<p className="text-sm break-all">
|
|
SHA256 do original:{' '}
|
|
{edition.payload.resource.sha256}
|
|
</p>
|
|
<p className="text-sm break-all">
|
|
Integridade das observações:{' '}
|
|
{edition.payload.resource.dataset_hash}
|
|
</p>
|
|
<ul>
|
|
{Object.entries(
|
|
edition.payload.resource.report
|
|
?.issue_counts ?? {},
|
|
).map(([code, count]) => (
|
|
<li key={code}>
|
|
{code}: {count} advertências conservadas
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<p>
|
|
Aprovar esta edição não disponibiliza a série.
|
|
Confirme publicar numa operação separada.
|
|
</p>
|
|
{edition.payload.links?.map((link) => (
|
|
<p key={link.target_key}>
|
|
Território: {link.context?.name}
|
|
</p>
|
|
))}
|
|
</section>
|
|
)}
|
|
{subject === 'portfolio' && (
|
|
<>
|
|
<p>Fonte: {edition.payload.resource.source}</p>
|
|
<dl>
|
|
{Object.entries(
|
|
edition.payload.resource.content ?? {},
|
|
).map(([key, value]) => (
|
|
<div key={key}>
|
|
<dt className="font-semibold">
|
|
{portfolioLabels[key] ?? key}
|
|
</dt>
|
|
<dd className="whitespace-pre-wrap">
|
|
{value}
|
|
</dd>
|
|
</div>
|
|
))}
|
|
</dl>
|
|
</>
|
|
)}
|
|
{subject === 'portfolio' &&
|
|
edition.payload.resource._structure && (
|
|
<section className="rounded border p-3">
|
|
<h3 className="font-semibold">
|
|
Alterações propostas nos dados-base
|
|
</h3>
|
|
<dl>
|
|
{Object.entries(
|
|
edition.payload.resource._structure
|
|
.fields,
|
|
)
|
|
.filter(
|
|
([key]) =>
|
|
![
|
|
'primary_territory_id',
|
|
'territory_version',
|
|
].includes(key),
|
|
)
|
|
.map(([key, value]) => (
|
|
<div key={key}>
|
|
<dt className="font-semibold">
|
|
{(
|
|
{
|
|
name: 'Designação',
|
|
code: 'Código',
|
|
status: 'Estado operacional',
|
|
sector: 'Sector',
|
|
description:
|
|
'Descrição',
|
|
budget_amount:
|
|
'Orçamento',
|
|
budget_currency:
|
|
'Moeda',
|
|
start_date:
|
|
'Data inicial',
|
|
end_date:
|
|
'Data final',
|
|
} as Record<
|
|
string,
|
|
string
|
|
>
|
|
)[key] ?? key}
|
|
</dt>
|
|
<dd>
|
|
{value ?? 'Por indicar'}
|
|
</dd>
|
|
</div>
|
|
))}
|
|
</dl>
|
|
{'primary_territory_id' in
|
|
edition.payload.resource._structure
|
|
.fields && (
|
|
<p>
|
|
Localização proposta:{' '}
|
|
{edition.payload.resource._structure.territory_context
|
|
?.map((node) => node.name)
|
|
.join(' / ') ||
|
|
'Sem localização associada'}
|
|
</p>
|
|
)}
|
|
</section>
|
|
)}
|
|
{subject === 'portfolio' &&
|
|
edition.payload.links?.map((link) => (
|
|
<p key={link.target_key}>
|
|
Contexto: {link.context?.name} ·{' '}
|
|
{link.context?.code}
|
|
{link.context?.sector
|
|
? ` · ${link.context.sector}`
|
|
: ''}
|
|
</p>
|
|
))}
|
|
{edition.payload.resource.editorial_content && (
|
|
<p className="whitespace-pre-wrap">
|
|
{
|
|
(
|
|
JSON.parse(
|
|
edition.payload.resource
|
|
.editorial_content,
|
|
) as { body?: string }
|
|
).body
|
|
}
|
|
</p>
|
|
)}
|
|
<ul>
|
|
{edition.payload.files.map((file) => (
|
|
<li key={file.id}>
|
|
<a
|
|
className="underline"
|
|
href={`/management/editorial/resources/${resource.id}/editions/${edition.id}/files/${file.id}`}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
>
|
|
{file.file_name} · versão {file.version_no}
|
|
</a>
|
|
<span className="block text-xs break-all">
|
|
SHA256: {file.sha256}
|
|
</span>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<p>
|
|
{edition.legal_required
|
|
? 'Validação jurídica obrigatória para esta edição.'
|
|
: 'Sem validação jurídica obrigatória definida para este tipo de conteúdo.'}
|
|
</p>
|
|
<p>
|
|
{edition.state === 'published' && !edition.approval
|
|
? 'Publicação anterior conservada, sem fabricar novas aprovações.'
|
|
: `Aprovação: ${edition.approval ? 'Registada nesta edição' : 'Pendente'}`}{' '}
|
|
· Validação jurídica:{' '}
|
|
{edition.legal_validation
|
|
? 'Registada nesta edição'
|
|
: 'Não registada'}
|
|
</p>
|
|
{independentRequired && current && (
|
|
<p>
|
|
Esta edição exige um responsável independente para a
|
|
aprovação.
|
|
</p>
|
|
)}
|
|
{current ? (
|
|
<form
|
|
key={edition.version}
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
form.transform((data) => ({
|
|
...data,
|
|
version: edition.version,
|
|
assigned_to: data.assigned_to || null,
|
|
}));
|
|
form.post(
|
|
`${editionBase}/editions/${edition.id}`,
|
|
{
|
|
onSuccess: () =>
|
|
form.reset('action', 'reason'),
|
|
},
|
|
);
|
|
}}
|
|
className="space-y-3"
|
|
>
|
|
<Field label="Operação">
|
|
<select
|
|
required
|
|
className={control}
|
|
value={form.data.action}
|
|
onChange={(e) =>
|
|
form.setData('action', e.target.value)
|
|
}
|
|
>
|
|
<option value="">Seleccionar</option>
|
|
{(available[edition.state] ?? [])
|
|
.filter(
|
|
(action) =>
|
|
(manager ||
|
|
[
|
|
'submit',
|
|
'archive',
|
|
].includes(action)) &&
|
|
(action !== 'legal_validate' ||
|
|
(legalReviewer &&
|
|
canApprove)) &&
|
|
(action !== 'approve' ||
|
|
canApprove) &&
|
|
(![
|
|
'portfolio',
|
|
'structural',
|
|
].includes(subject) ||
|
|
action !== 'publish' ||
|
|
canApprove) &&
|
|
(edition.fresh ||
|
|
[
|
|
'archive',
|
|
'return',
|
|
'reject',
|
|
'start_review',
|
|
].includes(action)),
|
|
)
|
|
.map((action) => (
|
|
<option key={action} value={action}>
|
|
{labels[action]}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
{form.data.action === 'submit' && (
|
|
<Field label="Responsável pela revisão">
|
|
<select
|
|
className={control}
|
|
value={form.data.assigned_to}
|
|
onChange={(e) =>
|
|
form.setData(
|
|
'assigned_to',
|
|
e.target.value,
|
|
)
|
|
}
|
|
>
|
|
<option value="">Por atribuir</option>
|
|
{reviewers.map((reviewer) => (
|
|
<option
|
|
key={reviewer.id}
|
|
value={reviewer.id}
|
|
>
|
|
{reviewer.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
)}
|
|
<Field label="Fundamento">
|
|
<textarea
|
|
required
|
|
minLength={3}
|
|
maxLength={5000}
|
|
className={control}
|
|
value={form.data.reason}
|
|
onChange={(e) =>
|
|
form.setData('reason', e.target.value)
|
|
}
|
|
/>
|
|
</Field>
|
|
{Object.entries(form.errors).map(([key, error]) => (
|
|
<p key={key} role="alert">
|
|
{error}
|
|
</p>
|
|
))}
|
|
<button
|
|
className={button}
|
|
disabled={form.processing || !form.data.action}
|
|
>
|
|
Registar operação
|
|
</button>
|
|
</form>
|
|
) : (
|
|
<p>
|
|
Versão histórica para consulta.{' '}
|
|
<Link className="underline" href={editionBase}>
|
|
Voltar à edição actual
|
|
</Link>
|
|
</p>
|
|
)}
|
|
</>
|
|
) : (
|
|
<p>
|
|
{subject === 'demography' && resource.published
|
|
? 'Este lote já estava disponibilizado e permanece preservado. Uma nova fonte deve entrar como um lote próprio.'
|
|
: 'Prepare uma edição no editor existente para iniciar a revisão.'}
|
|
</p>
|
|
)}
|
|
<h2 className="font-semibold">Versões conservadas</h2>
|
|
<ul>
|
|
{editions.map((e) => (
|
|
<li key={e.id}>
|
|
<Link
|
|
className="underline"
|
|
href={`${editionBase}?edition=${e.id}`}
|
|
>
|
|
Edição {e.revision_no}: {states[e.state] ?? e.state}
|
|
</Link>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<h2 className="font-semibold">Histórico de operações</h2>
|
|
<ul>
|
|
{history.map((event) => (
|
|
<li key={event.id}>
|
|
{event.occurred_at} ·{' '}
|
|
{labels[event.action.replace('editorial_', '')] ??
|
|
'Edição guardada'}{' '}
|
|
· Utilizador {event.actor_user_id}
|
|
<p>{JSON.parse(event.after_data)?.reason}</p>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</Management>
|
|
);
|
|
}
|