Files
Investidor-V2/resources/js/pages/crm/investor.tsx
T

227 lines
7.9 KiB
TypeScript

import { Link } from '@inertiajs/react';
import { useCrmRecordEdit } from '@/lib/crm-record-edit';
import CrmEditComparison from '@/components/crm-edit-comparison';
import { Management, Field, control, button } from '../management/shared';
import { leadStates } from './types';
type Contact = {
id: string;
version_no: number;
full_name: string;
email: string | null;
phone: string | null;
};
function ContactForm({
contact,
investorId,
}: {
contact: Contact;
investorId: string;
}) {
const edit = useCrmRecordEdit({
id: contact.id,
kind: 'contact',
version: contact.version_no,
values: {
full_name: contact.full_name,
email: contact.email ?? '',
phone: contact.phone ?? '',
},
});
const form = edit.form;
return (
<form
className="space-y-3 rounded border p-4"
onSubmit={(e) => {
e.preventDefault();
edit.submit(
`/management/crm/investors/${investorId}/contacts/${contact.id}`,
);
}}
>
<Field label="Nome do contacto" error={edit.error('full_name')}>
<input
className={control}
value={edit.values.full_name}
onChange={(e) => edit.change('full_name', e.target.value)}
/>
</Field>
<Field label="Email" error={edit.error('email')}>
<input
className={control}
type="email"
value={edit.values.email}
onChange={(e) => edit.change('email', e.target.value)}
/>
</Field>
<Field label="Telefone" error={edit.error('phone')}>
<input
className={control}
value={edit.values.phone}
onChange={(e) => edit.change('phone', e.target.value)}
/>
</Field>
<CrmEditComparison
editor={edit}
labels={{
full_name: 'Nome do contacto',
email: 'Email',
phone: 'Telefone',
}}
/>
<button
className={button}
disabled={form.processing || edit.blocked}
>
Guardar contacto
</button>
</form>
);
}
export default function InvestorDetail({
investor,
contacts,
leads,
}: {
investor: {
id: string;
version_no: number;
legal_name: string;
investor_type: string;
status: string;
};
contacts: Contact[];
leads: { id: string; title: string; status: string }[];
}) {
const edit = useCrmRecordEdit({
id: investor.id,
kind: 'investor',
version: investor.version_no,
values: {
legal_name: investor.legal_name,
investor_type: investor.investor_type,
status: investor.status,
},
});
const form = edit.form;
return (
<Management title={investor.legal_name}>
<form
className="max-w-xl space-y-4"
onSubmit={(e) => {
e.preventDefault();
edit.submit(`/management/crm/investors/${investor.id}`);
}}
>
<Field label="Nome legal" error={edit.error('legal_name')}>
<input
className={control}
value={edit.values.legal_name}
onChange={(e) =>
edit.change('legal_name', e.target.value)
}
/>
</Field>
<Field
label="Tipo de investidor"
error={edit.error('investor_type')}
>
<select
className={control}
value={edit.values.investor_type}
onChange={(e) =>
edit.change('investor_type', e.target.value)
}
>
{Object.entries({
domestic: 'Nacional',
foreign: 'Estrangeiro',
institutional: 'Institucional',
individual: 'Individual',
}).map(([key, label]) => (
<option key={key} value={key}>
{label}
</option>
))}
</select>
</Field>
<Field label="Estado" error={edit.error('status')}>
<select
className={control}
value={edit.values.status}
onChange={(e) => edit.change('status', e.target.value)}
>
{Object.entries({
prospect: 'Potencial investidor',
active: 'Activo',
inactive: 'Inactivo',
}).map(([key, label]) => (
<option key={key} value={key}>
{label}
</option>
))}
</select>
</Field>
<CrmEditComparison
editor={edit}
labels={{
legal_name: 'Nome legal',
investor_type: 'Tipo de investidor',
status: 'Estado',
}}
display={(key, value) =>
key === 'investor_type'
? ({
domestic: 'Nacional',
foreign: 'Estrangeiro',
institutional: 'Institucional',
individual: 'Individual',
}[value] ?? value)
: key === 'status'
? ({
prospect: 'Potencial investidor',
active: 'Activo',
inactive: 'Inactivo',
}[value] ?? value)
: value
}
/>
<button
className={button}
disabled={form.processing || edit.blocked}
>
Guardar investidor
</button>
</form>
<section className="max-w-xl space-y-4">
<h2 className="text-lg font-semibold">Contactos</h2>
<p>
Um contacto pode representar vários investidores. Alterações
ao contacto são partilhadas entre essas relações.
</p>
{contacts.map((contact) => (
<ContactForm
key={contact.id}
contact={contact}
investorId={investor.id}
/>
))}
</section>
<section className="space-y-3">
<h2 className="text-lg font-semibold">Leads associados</h2>
{leads.map((lead) => (
<p key={lead.id}>
<Link
className="underline"
href={`/management/crm/leads/${lead.id}`}
>
{lead.title}
</Link>{' '}
· {leadStates[lead.status]}
</p>
))}
</section>
</Management>
);
}