428 lines
18 KiB
TypeScript
428 lines
18 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 {
|
||
BusinessDistance,
|
||
GeographicCoverage,
|
||
type BusinessGeographicCoverage,
|
||
} from '@/components/similar-businesses';
|
||
import {
|
||
ClassificationSummary,
|
||
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;
|
||
distance_km?: number | null;
|
||
location_recorded_at?: string | null;
|
||
location_source_reference?: string | null;
|
||
location_source_date?: 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[];
|
||
geographicCoverage?: BusinessGeographicCoverage | null;
|
||
};
|
||
|
||
export default function BusinessMarketplace({
|
||
results,
|
||
filters,
|
||
territorySelection = [],
|
||
classificationOptions,
|
||
authorities = [],
|
||
geographicCoverage,
|
||
}: Props) {
|
||
const [radiusSelection, setRadiusSelection] = useState({
|
||
origin: filters.near_business,
|
||
value: filters.radius_km ?? '',
|
||
});
|
||
const radius =
|
||
radiusSelection.origin === filters.near_business
|
||
? radiusSelection.value
|
||
: (filters.radius_km ?? '');
|
||
const proximity =
|
||
filters.near_business && radius
|
||
? { near_business: filters.near_business, radius_km: radius }
|
||
: {};
|
||
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 ?? '',
|
||
});
|
||
// Detail links describe the results actually retrieved, even while the next search is being edited.
|
||
const returnQuery = new URLSearchParams(
|
||
Object.fromEntries(
|
||
Object.entries(filters).filter(
|
||
(entry): entry is [string, string] =>
|
||
entry[1] !== undefined && entry[1] !== '',
|
||
),
|
||
),
|
||
).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,
|
||
...proximity,
|
||
},
|
||
{ preserveState: true, preserveScroll: true },
|
||
);
|
||
}}
|
||
>
|
||
{filters.near_business && (
|
||
<fieldset className="w-full rounded border p-3">
|
||
<legend>
|
||
Distância a partir da ficha de referência
|
||
</legend>
|
||
<label>
|
||
Raio em km (0,1 a 2000)
|
||
<input
|
||
className={input}
|
||
type="number"
|
||
min="0.1"
|
||
max="2000"
|
||
step="any"
|
||
value={radius}
|
||
onChange={(event) =>
|
||
setRadiusSelection({
|
||
origin: filters.near_business,
|
||
value: event.target.value,
|
||
})
|
||
}
|
||
/>
|
||
</label>
|
||
<p className="text-sm">
|
||
Referência:{' '}
|
||
{geographicCoverage?.origin_title ??
|
||
'ficha pública seleccionada'}
|
||
. Limpar o raio retira o filtro de distância na
|
||
próxima pesquisa. Um filtro territorial adicional
|
||
restringe também os resultados.
|
||
</p>
|
||
</fieldset>
|
||
)}
|
||
<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>
|
||
{classificationOptions && (
|
||
<label className="grid gap-1">
|
||
<span>Sector</span>
|
||
<select
|
||
className={input}
|
||
value={classification.sector}
|
||
onChange={(event) =>
|
||
setClassification((current) => ({
|
||
...current,
|
||
sector: event.target.value,
|
||
subsector_id: '',
|
||
chain_id: '',
|
||
}))
|
||
}
|
||
>
|
||
<option value="">Todos</option>
|
||
{(classificationOptions.targets.sector ?? []).map(
|
||
(option) => (
|
||
<option
|
||
key={option.id}
|
||
value={String(option.id)}
|
||
>
|
||
{/fintech/i.test(option.label)
|
||
? 'Fintech — especialização do Mercado Financeiro'
|
||
: option.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 && (
|
||
<details className="w-full rounded border p-3">
|
||
<summary className="cursor-pointer font-semibold">
|
||
Mais filtros
|
||
</summary>
|
||
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
|
||
{(
|
||
[
|
||
[
|
||
'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 })),
|
||
],
|
||
[
|
||
'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>
|
||
</details>
|
||
)}
|
||
<button className={button}>Pesquisar</button>
|
||
</form>
|
||
{geographicCoverage && (
|
||
<GeographicCoverage coverage={geographicCoverage} />
|
||
)}
|
||
{results.data.length === 0 && (
|
||
<p>
|
||
Não foram encontradas fichas públicas com estes critérios. A
|
||
cobertura do catálogo não demonstra ausência de negócios no
|
||
mercado.
|
||
</p>
|
||
)}
|
||
<ul className="grid gap-4 md:grid-cols-2">
|
||
{results.data.map((record) => (
|
||
<li className={card} key={record.id}>
|
||
<p>
|
||
{businessModalities[record.modality] ??
|
||
'Modalidade por indicar'}
|
||
</p>
|
||
<BusinessDistance record={record} />
|
||
<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>
|
||
);
|
||
}
|