199 lines
7.2 KiB
TypeScript
199 lines
7.2 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';
|
||
|
||
type PublicBusiness = {
|
||
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 };
|
||
};
|
||
filters: {
|
||
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 = [],
|
||
}: Props) {
|
||
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,
|
||
}).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,
|
||
},
|
||
{ 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 ?? '',
|
||
})
|
||
}
|
||
/>
|
||
<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.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>
|
||
);
|
||
}
|