226 lines
10 KiB
TypeScript
226 lines
10 KiB
TypeScript
import { Link, router } from '@inertiajs/react';
|
||
import { useState } from 'react';
|
||
import { Frame, button, card, input } from './shared';
|
||
import {
|
||
TerritoryFields,
|
||
type TerritoryOption,
|
||
} from '@/components/investment-territory';
|
||
import { businessModalities } from './business-types';
|
||
import { ClassificationSummary, classificationStates, type PublicClassification, type ClassificationOptions } from '@/components/opportunity-classification';
|
||
|
||
type PublicBusiness = {
|
||
multidimensional?: PublicClassification[];
|
||
id: string;
|
||
slug: string;
|
||
title: string;
|
||
modality: string;
|
||
objective?: string | null;
|
||
stage?: string | null;
|
||
amount_min?: string | null;
|
||
amount_max?: string | null;
|
||
currency?: string | null;
|
||
sector?: { name: string; slug: string } | null;
|
||
province?: string | null;
|
||
};
|
||
type PaginationLink = { url: string | null; label: string; active: boolean };
|
||
type Props = {
|
||
results: {
|
||
data: PublicBusiness[];
|
||
links?:
|
||
| PaginationLink[]
|
||
| {
|
||
first: string | null;
|
||
last: string | null;
|
||
prev: string | null;
|
||
next: string | null;
|
||
};
|
||
meta?: { links?: PaginationLink[]; total?: number };
|
||
};
|
||
classificationOptions?: ClassificationOptions;
|
||
authorities?: { id: string; label_pt_mz: string }[];
|
||
filters: {
|
||
[key: string]: string | undefined;
|
||
q?: string;
|
||
modality?: string;
|
||
province_id?: string;
|
||
district_id?: string;
|
||
admin_post_id?: string;
|
||
locality_id?: string;
|
||
};
|
||
territorySelection?: TerritoryOption[];
|
||
};
|
||
|
||
export default function BusinessMarketplace({
|
||
results,
|
||
filters,
|
||
territorySelection = [],
|
||
classificationOptions,
|
||
authorities = [],
|
||
}: Props) {
|
||
const [classification, setClassification] = useState<Record<string, string>>(() => Object.fromEntries(['sector', 'association_type', 'association_target', 'association_purpose', 'association_state', 'chain_id', 'authority_id', 'subsector_id'].map((key) => [key, filters[key] ?? ''])));
|
||
const [query, setQuery] = useState(filters.q ?? '');
|
||
const [modality, setModality] = useState(filters.modality ?? '');
|
||
const [territory, setTerritory] = useState<Record<string, string>>({
|
||
province_id: filters.province_id ?? '',
|
||
district_id: filters.district_id ?? '',
|
||
admin_post_id: filters.admin_post_id ?? '',
|
||
locality_id: filters.locality_id ?? '',
|
||
});
|
||
const returnQuery = new URLSearchParams({
|
||
...(query ? { q: query } : {}),
|
||
...(modality ? { modality } : {}),
|
||
...territory,
|
||
...classification,
|
||
}).toString();
|
||
const paginationLinks =
|
||
results.meta?.links ??
|
||
(Array.isArray(results.links) ? results.links : []);
|
||
return (
|
||
<Frame title="Oportunidades de negócio">
|
||
<p>
|
||
Explore oportunidades aprovadas para publicação. A informação
|
||
está sujeita a diligência independente.
|
||
</p>
|
||
<form
|
||
className="flex flex-wrap gap-3"
|
||
onSubmit={(event) => {
|
||
event.preventDefault();
|
||
router.get(
|
||
'/negocios',
|
||
{
|
||
q: query || undefined,
|
||
modality: modality || undefined,
|
||
...territory,
|
||
...classification,
|
||
},
|
||
{ preserveState: true, preserveScroll: true },
|
||
);
|
||
}}
|
||
>
|
||
<label className="sr-only" htmlFor="business-search">
|
||
Pesquisar negócios
|
||
</label>
|
||
<input
|
||
id="business-search"
|
||
className={input}
|
||
value={query}
|
||
maxLength={120}
|
||
onChange={(event) => setQuery(event.target.value)}
|
||
/>
|
||
<label className="grid gap-1">
|
||
<span>Tipo de oportunidade</span>
|
||
<select
|
||
className={input}
|
||
value={modality}
|
||
onChange={(event) => setModality(event.target.value)}
|
||
>
|
||
<option value="">Todos os tipos</option>
|
||
{Object.entries(businessModalities).map(
|
||
([value, label]) => (
|
||
<option key={value} value={value}>
|
||
{label}
|
||
</option>
|
||
),
|
||
)}
|
||
</select>
|
||
</label>
|
||
<TerritoryFields
|
||
initial={territorySelection}
|
||
onChange={(path) =>
|
||
setTerritory({
|
||
province_id: path[0]?.id ?? '',
|
||
district_id: path[1]?.id ?? '',
|
||
admin_post_id: path[2]?.id ?? '',
|
||
locality_id: path[3]?.id ?? '',
|
||
})
|
||
}
|
||
/>
|
||
{classificationOptions && <fieldset className="w-full rounded border p-3">
|
||
<legend>Classificação multidimensional — filtros combinados</legend>
|
||
<p>Critérios diferentes combinam-se por «e». Tipo, referência, finalidade e estado têm de corresponder à mesma relação pública. O setor principal mantém o seu significado.</p>
|
||
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
|
||
{([
|
||
['sector', 'Setor principal do anúncio', classificationOptions.targets.sector ?? []],
|
||
['association_type', 'Tipo de associação', Object.keys(classificationOptions.types).map((id) => ({ id, label: ({ corridor: 'Corredor', location: 'Localização', cluster: 'Cluster', project: 'Projeto âncora', chain: 'Cadeia de valor', park: 'Parque industrial', zone: 'Regime de zona', sector: 'Setor associado', activity: 'Subsector ou atividade' } as Record<string, string>)[id] ?? id }))],
|
||
['association_target', 'Referência da associação', classificationOptions.targets[classification.association_type] ?? []],
|
||
['association_purpose', 'Finalidade', Object.entries(classificationOptions.purposes).map(([id, label]) => ({ id, label }))],
|
||
['association_state', 'Estado da associação', Object.entries(classificationStates).filter(([id]) => !['archived', 'revoked'].includes(id)).map(([id, label]) => ({ id, label }))],
|
||
['chain_id', 'Cadeia de valor', classificationOptions.targets.chain ?? []],
|
||
['subsector_id', 'Subsector ou atividade', classificationOptions.targets.activity ?? []],
|
||
['authority_id', 'Entidade competente', authorities.map((a) => ({ id: a.id, label: a.label_pt_mz }))],
|
||
] as [string, string, { id: string | number; label: string }[]][]).map(([key, label, options]) => <label key={key} className="grid gap-1"><span>{label}</span><select className={input} value={classification[key]} onChange={(e) => setClassification((old) => ({ ...old, [key]: e.target.value, ...(key === 'association_type' ? { association_target: '' } : {}) }))}><option value="">Todos</option>{options.map((o) => <option key={o.id} value={String(o.id)}>{o.label}</option>)}</select></label>)}
|
||
</div>
|
||
</fieldset>}
|
||
<button className={button}>Pesquisar</button>
|
||
</form>
|
||
<p role="status">
|
||
{results.meta?.total ?? results.data.length} anúncios de
|
||
negócios publicados
|
||
</p>
|
||
{!results.data.length && (
|
||
<p role="status">
|
||
Não existem oportunidades publicadas com estes critérios.
|
||
</p>
|
||
)}
|
||
<ul className="grid gap-4 md:grid-cols-2">
|
||
{results.data.map((record) => (
|
||
<li className={card} key={record.id}>
|
||
<p>
|
||
{businessModalities[record.modality] ??
|
||
record.modality}
|
||
</p>
|
||
<h2 className="text-xl font-bold">
|
||
<Link
|
||
className="underline"
|
||
href={`/negocios/${record.slug}${returnQuery ? `?${returnQuery}` : ''}`}
|
||
>
|
||
{record.title}
|
||
</Link>
|
||
</h2>
|
||
<p>
|
||
{[
|
||
record.sector?.name,
|
||
record.province,
|
||
record.stage,
|
||
]
|
||
.filter(Boolean)
|
||
.join(' · ')}
|
||
</p>
|
||
{(record.amount_min || record.amount_max) && (
|
||
<p>
|
||
{record.amount_min ?? '—'} –{' '}
|
||
{record.amount_max ?? '—'} {record.currency}
|
||
</p>
|
||
)}
|
||
{!!record.multidimensional?.length && <ClassificationSummary rows={record.multidimensional} />}
|
||
{record.objective && (
|
||
<p className="mt-3 line-clamp-3">
|
||
{record.objective}
|
||
</p>
|
||
)}
|
||
</li>
|
||
))}
|
||
</ul>
|
||
<nav
|
||
aria-label="Paginação das oportunidades"
|
||
className="flex flex-wrap gap-3"
|
||
>
|
||
{paginationLinks.map((link) =>
|
||
link.url ? (
|
||
<Link
|
||
key={link.label}
|
||
href={link.url}
|
||
className={
|
||
link.active
|
||
? 'font-bold underline'
|
||
: 'underline'
|
||
}
|
||
dangerouslySetInnerHTML={{ __html: link.label }}
|
||
/>
|
||
) : null,
|
||
)}
|
||
</nav>
|
||
</Frame>
|
||
);
|
||
}
|