feat: consolidar desenvolvimentos do portal InvestMoz
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { Link, router, useForm } from '@inertiajs/react';
|
||||
import { Frame, Field, input, button, card } from './shared';
|
||||
import { Link, router } from '@inertiajs/react';
|
||||
import { Frame, button, card } from './shared';
|
||||
import PartnershipSourceLinks from '@/components/partnership-source-links';
|
||||
import CountrySelect, { type Country } from '@/components/country-select';
|
||||
import { useEffect, useId } from 'react';
|
||||
import { type Country } from '@/components/country-select';
|
||||
import { useCallback, useState } from 'react';
|
||||
import Editor from '@/components/investment-profile-editor';
|
||||
import OrganizationEditor from '@/components/organization-country-editor';
|
||||
import { usePagePreparationGuard } from '@/lib/page-preparation-guard';
|
||||
|
||||
type RecordData = { version: number; payload: Record<string, string> };
|
||||
type Company = RecordData & {
|
||||
@@ -37,19 +40,38 @@ const labels: Record<string, string> = {
|
||||
|
||||
export default function Profile({
|
||||
account,
|
||||
actorId,
|
||||
companies,
|
||||
countries,
|
||||
organizations = [],
|
||||
organizationCountryAvailable = false,
|
||||
}: {
|
||||
account: RecordData;
|
||||
actorId: number;
|
||||
companies: Company[];
|
||||
countries: Country[];
|
||||
organizations?: Organization[];
|
||||
organizationCountryAvailable?: boolean;
|
||||
}) {
|
||||
const [preparations, setPreparations] = useState<Record<string, boolean>>(
|
||||
{},
|
||||
);
|
||||
const reportDirty = useCallback(
|
||||
(id: string, dirty: boolean) =>
|
||||
setPreparations((prior) =>
|
||||
prior[id] === dirty ? prior : { ...prior, [id]: dirty },
|
||||
),
|
||||
[],
|
||||
);
|
||||
usePagePreparationGuard(
|
||||
Object.values(preparations).some(Boolean),
|
||||
'Existem dados por guardar nesta página. Sair e perder a preparação?',
|
||||
);
|
||||
return (
|
||||
<Frame title="Perfil de investimento e empresas">
|
||||
<Link className="underline" href="/formas-juridicas">
|
||||
Comparar formas jurídicas antes de constituir uma entidade
|
||||
</Link>
|
||||
<Link href="/investidor/manifestacoes" className="underline">
|
||||
Voltar à área do investidor
|
||||
</Link>
|
||||
@@ -62,7 +84,11 @@ export default function Profile({
|
||||
.
|
||||
</p>
|
||||
<Editor
|
||||
key={'account-' + account.version}
|
||||
key={'account-' + actorId}
|
||||
actorId={actorId}
|
||||
editorId="account"
|
||||
labels={labels}
|
||||
reportDirty={reportDirty}
|
||||
record={account}
|
||||
countries={countries}
|
||||
fields={[
|
||||
@@ -102,8 +128,13 @@ export default function Profile({
|
||||
</p>
|
||||
)}
|
||||
<Editor
|
||||
key={company.version}
|
||||
key={company.id + '-' + actorId}
|
||||
actorId={actorId}
|
||||
editorId={company.id}
|
||||
labels={labels}
|
||||
reportDirty={reportDirty}
|
||||
record={company}
|
||||
organizations={organizations}
|
||||
countries={countries}
|
||||
fields={[
|
||||
'name',
|
||||
@@ -114,42 +145,20 @@ export default function Profile({
|
||||
'description',
|
||||
]}
|
||||
/>
|
||||
{organizations
|
||||
.filter((o) => o.country)
|
||||
.map((org) => (
|
||||
<div key={org.id} className="mt-3 space-y-2">
|
||||
<p>
|
||||
País mestre de {org.name}: {org.country}. A
|
||||
reutilização altera somente esta declaração;
|
||||
manifestações anteriores conservam o seu
|
||||
valor.
|
||||
</p>
|
||||
<button
|
||||
className={button}
|
||||
onClick={() =>
|
||||
router.post(
|
||||
`/investidor/companies/${company.id}/organization-country`,
|
||||
{
|
||||
version: company.version,
|
||||
organization_id: org.id,
|
||||
organization_version:
|
||||
org.version,
|
||||
},
|
||||
{ preserveScroll: true },
|
||||
)
|
||||
}
|
||||
>
|
||||
Reutilizar país de {org.name}
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
<button
|
||||
className={button + ' mt-4'}
|
||||
onClick={() =>
|
||||
onClick={() => {
|
||||
if (
|
||||
Object.values(preparations).some(Boolean) &&
|
||||
!window.confirm(
|
||||
'Existem dados por guardar nesta página. Sair e perder a preparação?',
|
||||
)
|
||||
)
|
||||
return;
|
||||
router.post('/investidor/manifestacoes', {
|
||||
company_id: company.id,
|
||||
})
|
||||
}
|
||||
});
|
||||
}}
|
||||
>
|
||||
Preparar manifestação desta empresa
|
||||
</button>
|
||||
@@ -159,7 +168,11 @@ export default function Profile({
|
||||
<section className={card}>
|
||||
<h3 className="text-xl font-bold">Acrescentar empresa</h3>
|
||||
<Editor
|
||||
key={'new-' + companies.length}
|
||||
key={'new-' + actorId}
|
||||
actorId={actorId}
|
||||
editorId="new-company"
|
||||
labels={labels}
|
||||
reportDirty={reportDirty}
|
||||
record={{ version: 0, payload: {} }}
|
||||
company
|
||||
countries={countries}
|
||||
@@ -195,7 +208,9 @@ export default function Profile({
|
||||
)}
|
||||
{organizations.map((org) => (
|
||||
<OrganizationEditor
|
||||
key={org.id + org.version}
|
||||
key={org.id + '-' + actorId}
|
||||
actorId={actorId}
|
||||
reportDirty={reportDirty}
|
||||
organization={org}
|
||||
countries={countries}
|
||||
/>
|
||||
@@ -203,201 +218,3 @@ export default function Profile({
|
||||
</Frame>
|
||||
);
|
||||
}
|
||||
function OrganizationEditor({
|
||||
organization,
|
||||
countries,
|
||||
}: {
|
||||
organization: Organization;
|
||||
countries: Country[];
|
||||
}) {
|
||||
const form = useForm({
|
||||
version: organization.version,
|
||||
country: organization.country ?? '',
|
||||
source_reference: organization.provenance?.reference ?? '',
|
||||
});
|
||||
const id = useId();
|
||||
useEffect(() => {
|
||||
if (Object.keys(form.errors).length)
|
||||
document
|
||||
.getElementById(id)
|
||||
?.querySelector<HTMLElement>('[aria-invalid="true"]')
|
||||
?.focus();
|
||||
}, [form.errors, id]);
|
||||
return (
|
||||
<section
|
||||
className={card}
|
||||
aria-label={`Cadastro mestre: ${organization.name}`}
|
||||
>
|
||||
<h3 className="text-xl font-bold">{organization.name}</h3>
|
||||
<p>
|
||||
Versão {organization.version}; origem do país:{' '}
|
||||
{organization.provenance?.kind === 'representative_declaration'
|
||||
? 'declaração do representante'
|
||||
: 'não informada'}
|
||||
.
|
||||
</p>
|
||||
<form
|
||||
className="mt-4 space-y-4"
|
||||
id={id}
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.put(
|
||||
`/investidor/organizations/${organization.id}/country`,
|
||||
{ preserveScroll: true },
|
||||
);
|
||||
}}
|
||||
>
|
||||
<Field label="País de constituição da organização">
|
||||
<CountrySelect
|
||||
id={id + '-country'}
|
||||
label="País de constituição da organização"
|
||||
countries={countries}
|
||||
value={form.data.country}
|
||||
disabled={!organization.canEdit}
|
||||
error={form.errors.country}
|
||||
onChange={(country) => form.setData('country', country)}
|
||||
/>
|
||||
</Field>
|
||||
<label className="block" htmlFor={id + '-source'}>
|
||||
Fonte ou referência deste país
|
||||
</label>
|
||||
<textarea
|
||||
id={id + '-source'}
|
||||
className={input}
|
||||
value={form.data.source_reference}
|
||||
disabled={!organization.canEdit}
|
||||
maxLength={2000}
|
||||
aria-invalid={!!form.errors.source_reference}
|
||||
aria-describedby={
|
||||
form.errors.source_reference
|
||||
? id + '-source-error'
|
||||
: undefined
|
||||
}
|
||||
onChange={(event) =>
|
||||
form.setData('source_reference', event.target.value)
|
||||
}
|
||||
/>
|
||||
{form.errors.source_reference && (
|
||||
<p id={id + '-source-error'} role="alert">
|
||||
{form.errors.source_reference}
|
||||
</p>
|
||||
)}
|
||||
{form.errors.version && (
|
||||
<p role="alert">{form.errors.version}</p>
|
||||
)}
|
||||
<button
|
||||
className={button}
|
||||
disabled={!organization.canEdit || form.processing}
|
||||
>
|
||||
Guardar país da organização
|
||||
</button>
|
||||
</form>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
function Editor({
|
||||
record,
|
||||
fields,
|
||||
company = false,
|
||||
countries,
|
||||
}: {
|
||||
record: RecordData & { id?: string };
|
||||
fields: string[];
|
||||
company?: boolean;
|
||||
countries: Country[];
|
||||
}) {
|
||||
const isCompany = company || !!record.id;
|
||||
const form = useForm({
|
||||
version: record.version,
|
||||
payload: Object.fromEntries(
|
||||
[...fields, 'country'].map((key) => [
|
||||
key,
|
||||
record.payload[key] ?? '',
|
||||
]),
|
||||
),
|
||||
});
|
||||
return (
|
||||
<form
|
||||
className="mt-4 space-y-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
const options = { preserveScroll: true };
|
||||
if (isCompany)
|
||||
form.post(
|
||||
'/investidor/companies' +
|
||||
(record.id ? '/' + record.id : ''),
|
||||
options,
|
||||
);
|
||||
else form.put('/investidor/profile', options);
|
||||
}}
|
||||
>
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
{record.payload.country && (
|
||||
<p>
|
||||
País anteriormente registado: {record.payload.country}.
|
||||
Conservado sem atribuir automaticamente residência,
|
||||
constituição ou origem da solicitação.
|
||||
</p>
|
||||
)}
|
||||
{fields.map((key) => (
|
||||
<Field key={key} label={labels[key]}>
|
||||
{key.endsWith('_country_code') ? (
|
||||
<CountrySelect
|
||||
id={key + (record.id ?? 'account')}
|
||||
countries={countries}
|
||||
label={labels[key]}
|
||||
error={form.errors[`payload.${key}`]}
|
||||
value={form.data.payload[key]}
|
||||
onChange={(value) =>
|
||||
form.setData('payload', {
|
||||
...form.data.payload,
|
||||
[key]: value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
) : (
|
||||
<input
|
||||
className={input}
|
||||
value={form.data.payload[key]}
|
||||
required={
|
||||
isCompany &&
|
||||
['name', 'country', 'role'].includes(key)
|
||||
}
|
||||
maxLength={
|
||||
key === 'description' ||
|
||||
key === 'experience'
|
||||
? 2000
|
||||
: key === 'investment_capacity'
|
||||
? 1000
|
||||
: 200
|
||||
}
|
||||
onChange={(e) =>
|
||||
form.setData('payload', {
|
||||
...form.data.payload,
|
||||
[key]: e.target.value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
)}
|
||||
{!key.endsWith('_country_code') &&
|
||||
form.errors[`payload.${key}`] && (
|
||||
<p role="alert" className="text-red-700">
|
||||
{form.errors[`payload.${key}`]}
|
||||
</p>
|
||||
)}
|
||||
</Field>
|
||||
))}
|
||||
</div>
|
||||
{Object.entries(form.errors).map(([key, value]) => (
|
||||
<p role="alert" key={key} className="text-red-700">
|
||||
{value}
|
||||
</p>
|
||||
))}
|
||||
<button className={button} disabled={form.processing}>
|
||||
{isCompany
|
||||
? 'Guardar declaração da empresa'
|
||||
: 'Guardar perfil de investimento'}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user