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';
import type { Country } from '@/components/country-select';
import BusinessPointSummary, {
type RegisteredBusinessLocation,
} from '@/components/business-point-summary';
import { businessPointTerritoryLabel } from '@/lib/business-locations';
type Props = {
countries: Country[];
formSchema: FormSchema;
modalityFormType?: string | null;
sectors: { id: number; name: string }[];
record: Business;
canEdit: boolean;
registeredLocation?: RegisteredBusinessLocation | null;
pointPublicationAvailable?: boolean;
owner: boolean;
canReview: boolean;
canTriage: boolean;
canApproveLevelOne: boolean;
canApproveLevelTwo: boolean;
canPublish: 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,
canTriage,
canApproveLevelOne,
canApproveLevelTwo,
canPublish,
modalities,
history,
objects,
objectKinds,
verificationChecks,
verifications,
formSchema,
sectors,
registeredLocation,
pointPublicationAvailable,
}: 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 (
Classificação multidimensional deste anúncio
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, field]) =>
!key.startsWith('location_point_') &&
![
'location_latitude',
'location_longitude',
].includes(key) &&
Object.entries(field.when ?? {}).every(
([dependency, values]) =>
values.includes(
record.payload[dependency] ?? '',
),
) &&
![
'title',
'description',
'declared_reason',
'source_reference',
'valid_until',
].includes(key),
)
.map(([key, field]) => (
{field.pt}
{record.payload[key] || '—'}
))}
)}
{owner && canEdit && (
setConsent(e.target.checked)}
/>
Autorizo a análise interna desta submissão pelos revisores
designados. Esta autorização não permite publicação.
)}
{(canTriage ||
canReview ||
canApproveLevelOne ||
canApproveLevelTwo ||
canPublish) && (
{busy
? 'Aguarde a conclusão da ação em curso.'
: note.trim().length < 10
? 'Para disponibilizar a ação, preencha «Motivo da alteração ou nota para a revisão» com pelo menos 10 caracteres. A nota fica no histórico reservado.'
: 'Nota preenchida. Pode executar a ação disponível para o seu papel e o estado atual.'}
)}
{owner && canEdit && (
void run('submitted')}
>
Submeter para análise
)}
{canReview && record.status === 'submitted' && (
void run('in_review')}
>
Iniciar revisão
)}
{canReview && record.status === 'in_review' && (
void run('needs_information')}
>
Pedir informação
)}
{canTriage && record.status === 'submitted' && (
void run('triage')}
>
Iniciar triagem
)}
{canReview && record.status === 'triage' && (
void run('in_review')}
>
Encaminhar para revisão
)}
{canReview && record.status === 'in_review' && (
<>
void run('approval_l1')}
>
Enviar para aprovação nÃvel 1
void run('rejected')}
>
Rejeitar
>
)}
{canApproveLevelOne && record.status === 'approval_l1' && (
<>
void run('approval_l2')}
>
Aprovar nÃvel 1 e enviar ao nÃvel 2
void run('rejected')}
>
Rejeitar
>
)}
{canApproveLevelTwo && record.status === 'approval_l2' && (
<>
void run('approved')}
>
Aprovar nÃvel 2
void run('rejected')}
>
Rejeitar
>
)}
{canPublish && record.status === 'approved' && (
void run('published')}
>
Publicar
)}
{owner && record.status !== 'withdrawn' && (
void run('withdrawn')}
>
Retirar submissão
)}
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}
))}
);
}