165 lines
6.6 KiB
TypeScript
165 lines
6.6 KiB
TypeScript
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 CountrySelect, { type Country } from '@/components/country-select';
|
|
|
|
export default function Businesses({
|
|
records,
|
|
management,
|
|
modalities,
|
|
filters,
|
|
countries,
|
|
}: {
|
|
records: {
|
|
data: Business[];
|
|
prev_page_url: string | null;
|
|
next_page_url: string | null;
|
|
};
|
|
management: boolean;
|
|
modalities: string[];
|
|
countries: Country[];
|
|
filters: { country?: string; origin?: string };
|
|
}) {
|
|
const [title, setTitle] = useState('');
|
|
const [modality, setModality] = useState(modalities[0]);
|
|
const [error, setError] = useState('');
|
|
const [busy, setBusy] = useState(false);
|
|
const [country, setCountry] = useState(filters.country ?? '');
|
|
const [origin, setOrigin] = useState(filters.origin ?? '');
|
|
return (
|
|
<Frame title={management ? 'Revisão de negócios' : 'Os meus negócios'}>
|
|
<p>
|
|
Área privada de preparação e revisão. A submissão não confirma
|
|
titularidade, validade ou transmissibilidade do negócio, activo
|
|
ou direito.
|
|
</p>
|
|
<form
|
|
className={`${card} flex flex-wrap gap-3`}
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
router.get(
|
|
management
|
|
? '/management/negocios'
|
|
: '/investidor/negocios',
|
|
{ country, origin },
|
|
);
|
|
}}
|
|
>
|
|
<CountrySelect
|
|
countries={countries}
|
|
value={country}
|
|
onChange={setCountry}
|
|
label="Filtrar país do proponente"
|
|
/>
|
|
<label>
|
|
Origem declarada
|
|
<select
|
|
aria-label="Filtrar origem declarada"
|
|
className={input}
|
|
value={origin}
|
|
onChange={(e) => setOrigin(e.target.value)}
|
|
>
|
|
<option value="">Todas</option>
|
|
<option value="unknown">Não informada</option>
|
|
{countries.map((c) => (
|
|
<option key={c.code} value={c.code}>
|
|
{c.name} ({c.code})
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<button className={button}>Filtrar negócios</button>
|
|
</form>
|
|
{!management && (
|
|
<form
|
|
className={`${card} space-y-4`}
|
|
onSubmit={async (e) => {
|
|
e.preventDefault();
|
|
setBusy(true);
|
|
setError('');
|
|
try {
|
|
const result = await api(
|
|
'/api/v1/existing-business/listings',
|
|
{
|
|
modality,
|
|
classification: 'confidential',
|
|
reason: 'Criação de rascunho pelo proprietário',
|
|
payload: {
|
|
title,
|
|
wizard_version: '1',
|
|
wizard_step: '1',
|
|
},
|
|
},
|
|
);
|
|
router.visit(`/investidor/negocios/${result.id}`);
|
|
} catch (err) {
|
|
setError(
|
|
err instanceof Error
|
|
? err.message
|
|
: 'Não foi possível criar o rascunho.',
|
|
);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}}
|
|
>
|
|
<h2 className="text-xl font-bold">Preparar negócio</h2>
|
|
<Field label="Título privado">
|
|
<input
|
|
className={input}
|
|
value={title}
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
required
|
|
maxLength={200}
|
|
/>
|
|
</Field>
|
|
<Field label="Modalidade">
|
|
<select
|
|
className={input}
|
|
value={modality}
|
|
onChange={(e) => setModality(e.target.value)}
|
|
>
|
|
{modalities.map((m) => (
|
|
<option key={m} value={m}>
|
|
{businessModalities[m] ?? m}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
{error && <p role="alert">{error}</p>}
|
|
<button className={button} disabled={busy}>
|
|
{busy ? 'A criar…' : 'Criar rascunho privado'}
|
|
</button>
|
|
</form>
|
|
)}
|
|
{!records.data.length && (
|
|
<p role="status">Não existem negócios nesta lista.</p>
|
|
)}
|
|
{records.data.map((r) => (
|
|
<article className={card} key={r.id}>
|
|
<Link
|
|
className="font-bold underline"
|
|
href={`/investidor/negocios/${r.id}`}
|
|
>
|
|
{r.payload.title}
|
|
</Link>
|
|
<p>
|
|
{businessModalities[r.modality]} ·{' '}
|
|
{businessStates[r.status]} · versão {r.version_no}
|
|
</p>
|
|
</article>
|
|
))}
|
|
<nav aria-label="Paginação dos negócios" className="flex gap-4">
|
|
{records.prev_page_url && (
|
|
<Link href={records.prev_page_url}>Anterior</Link>
|
|
)}
|
|
{records.next_page_url && (
|
|
<Link href={records.next_page_url}>Seguinte</Link>
|
|
)}
|
|
</nav>
|
|
</Frame>
|
|
);
|
|
}
|