@@ -1,4 +1,7 @@
|
||||
import { Head, useForm } from '@inertiajs/react';
|
||||
import { useProcurementPreparationIdentity } from '@/hooks/use-procurement-preparation-identity';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import { Head, router, useForm } from '@inertiajs/react';
|
||||
import {
|
||||
TableViewport,
|
||||
FormErrors,
|
||||
@@ -27,23 +30,48 @@ type Props = {
|
||||
roles: { value: string; label: string }[];
|
||||
};
|
||||
|
||||
export default function CompanyPage({
|
||||
export default function CompanyPage(props: Props) {
|
||||
const identity = useProcurementPreparationIdentity(
|
||||
`company-team:${props.company.id}`,
|
||||
);
|
||||
return <CompanyFields key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
function CompanyFields({
|
||||
company,
|
||||
departments,
|
||||
costCenters,
|
||||
users,
|
||||
roles,
|
||||
}: Props) {
|
||||
const department = useForm({ name: '' });
|
||||
const center = useForm({ department_id: '', code: '', name: '' });
|
||||
const user = useForm({
|
||||
identity,
|
||||
}: Props & { identity: string }) {
|
||||
const initialDepartment = { name: '' };
|
||||
const department = useForm(`${identity}:department`, initialDepartment);
|
||||
const initialCenter = { department_id: '', code: '', name: '' };
|
||||
const center = useForm(`${identity}:center`, initialCenter);
|
||||
const initialUser = {
|
||||
name: '',
|
||||
email: '',
|
||||
department_id: '',
|
||||
purchasing_role: 'requester',
|
||||
password: '',
|
||||
password_confirmation: '',
|
||||
};
|
||||
const user = useForm(`${identity}:user`, initialUser).dontRemember(
|
||||
'password',
|
||||
'password_confirmation',
|
||||
);
|
||||
const departmentDirty = hasPreparationChanges(
|
||||
initialDepartment,
|
||||
department.data,
|
||||
);
|
||||
const departmentRef = usePreparationDirty(departmentDirty);
|
||||
const centerDirty = hasPreparationChanges(initialCenter, center.data);
|
||||
const centerRef = usePreparationDirty(centerDirty);
|
||||
const userDirty = hasPreparationChanges(initialUser, {
|
||||
...initialUser,
|
||||
...user.data,
|
||||
});
|
||||
const userRef = usePreparationDirty(userDirty);
|
||||
return (
|
||||
<>
|
||||
<Head title="Empresa e equipa" />
|
||||
@@ -69,11 +97,27 @@ export default function CompanyPage({
|
||||
))}
|
||||
</div>
|
||||
<form
|
||||
ref={departmentRef}
|
||||
data-preparation-dirty={
|
||||
departmentDirty ? 'true' : 'false'
|
||||
}
|
||||
className="space-y-3"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
department.post('/empresa/departamentos', {
|
||||
onSuccess: () => department.reset(),
|
||||
onSuccess: () => {
|
||||
department.setData(initialDepartment);
|
||||
department.clearErrors();
|
||||
|
||||
router.remember(
|
||||
initialDepartment,
|
||||
`${identity}:department:data`,
|
||||
);
|
||||
router.remember(
|
||||
{},
|
||||
`${identity}:department:errors`,
|
||||
);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -115,11 +159,27 @@ export default function CompanyPage({
|
||||
))}
|
||||
</div>
|
||||
<form
|
||||
ref={centerRef}
|
||||
data-preparation-dirty={
|
||||
centerDirty ? 'true' : 'false'
|
||||
}
|
||||
className="space-y-3"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
center.post('/empresa/centros-de-custo', {
|
||||
onSuccess: () => center.reset(),
|
||||
onSuccess: () => {
|
||||
center.setData(initialCenter);
|
||||
center.clearErrors();
|
||||
|
||||
router.remember(
|
||||
initialCenter,
|
||||
`${identity}:center:data`,
|
||||
);
|
||||
router.remember(
|
||||
{},
|
||||
`${identity}:center:errors`,
|
||||
);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -214,12 +274,30 @@ export default function CompanyPage({
|
||||
</table>
|
||||
</TableViewport>
|
||||
<form
|
||||
ref={userRef}
|
||||
data-preparation-dirty={userDirty ? 'true' : 'false'}
|
||||
className="mt-6 space-y-4 border-t pt-5"
|
||||
autoComplete="off"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
user.post('/empresa/utilizadores', {
|
||||
onSuccess: () => user.reset(),
|
||||
onSuccess: () => {
|
||||
user.setData(initialUser);
|
||||
user.clearErrors();
|
||||
const {
|
||||
password: _password,
|
||||
password_confirmation: _confirmation,
|
||||
...remembered
|
||||
} = initialUser;
|
||||
router.remember(
|
||||
remembered,
|
||||
`${identity}:user:data`,
|
||||
);
|
||||
router.remember(
|
||||
{},
|
||||
`${identity}:user:errors`,
|
||||
);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -270,7 +348,7 @@ export default function CompanyPage({
|
||||
: undefined
|
||||
}
|
||||
required
|
||||
value={user.data[field.key]}
|
||||
value={user.data[field.key] ?? ''}
|
||||
onChange={(e) =>
|
||||
user.setData(
|
||||
field.key,
|
||||
@@ -330,6 +408,10 @@ export default function CompanyPage({
|
||||
etapa as contas são criadas pelo administrador;
|
||||
convites por email ficam para uma próxima etapa.
|
||||
</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
As palavras-passe não são conservadas ao sair deste
|
||||
ecrã.
|
||||
</p>
|
||||
<Button disabled={user.processing}>
|
||||
Criar utilizador
|
||||
</Button>
|
||||
|
||||
Reference in New Issue
Block a user