Atalho Nova requisição só no contexto de Compras, como o menu
This commit is contained in:
@@ -36,6 +36,7 @@ import type {
|
|||||||
import { roleLabels } from '@/types/procurement';
|
import { roleLabels } from '@/types/procurement';
|
||||||
import type { WorkContextState } from '@/types/work-context';
|
import type { WorkContextState } from '@/types/work-context';
|
||||||
import { WorkContextChange } from '@/components/work-context-change';
|
import { WorkContextChange } from '@/components/work-context-change';
|
||||||
|
import { contextOffersPath } from '@/lib/work-context-paths';
|
||||||
import { ProcurementMenuResults } from '@/components/procurement-menu-results';
|
import { ProcurementMenuResults } from '@/components/procurement-menu-results';
|
||||||
import {
|
import {
|
||||||
matchesMenuSearch,
|
matchesMenuSearch,
|
||||||
@@ -559,7 +560,11 @@ export function SgcWorkspaceShell({ children }: { children: ReactNode }) {
|
|||||||
<Link href="/login">Entrar</Link>
|
<Link href="/login">Entrar</Link>
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
{page.props.canRequest === true && (
|
{page.props.canRequest === true &&
|
||||||
|
contextOffersPath(
|
||||||
|
context,
|
||||||
|
'/requisicoes/criar',
|
||||||
|
) && (
|
||||||
<Button
|
<Button
|
||||||
asChild
|
asChild
|
||||||
size="sm"
|
size="sm"
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import type { WorkContextState } from '@/types/work-context';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indica se um atalho para `path` deve aparecer no contexto de trabalho actual.
|
||||||
|
* Segue a mesma regra do menu: com contextos activos, só aparecem os caminhos do contexto
|
||||||
|
* escolhido. Não decide permissões: o servidor continua a autorizar cada pedido.
|
||||||
|
*/
|
||||||
|
export function contextOffersPath(
|
||||||
|
context: WorkContextState | null | undefined,
|
||||||
|
path: string,
|
||||||
|
): boolean {
|
||||||
|
if (!context?.enforced) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return context.paths.includes(path.split('?')[0]);
|
||||||
|
}
|
||||||
@@ -9,8 +9,10 @@ import {
|
|||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import { PageTitle, Panel, RequisitionTable } from '@/components/procurement';
|
import { PageTitle, Panel, RequisitionTable } from '@/components/procurement';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { contextOffersPath } from '@/lib/work-context-paths';
|
||||||
import { money } from '@/types/procurement';
|
import { money } from '@/types/procurement';
|
||||||
import type { Company, Requisition } from '@/types/procurement';
|
import type { Company, Requisition } from '@/types/procurement';
|
||||||
|
import type { WorkContextState } from '@/types/work-context';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
company: Company | null;
|
company: Company | null;
|
||||||
@@ -30,8 +32,14 @@ export default function Dashboard({
|
|||||||
recent,
|
recent,
|
||||||
approvals,
|
approvals,
|
||||||
}: Props) {
|
}: Props) {
|
||||||
const canRequest = usePage().props.canRequest === true;
|
const { props } = usePage();
|
||||||
const publicNavigation = !!usePage().props.publicNavigation;
|
const canRequest =
|
||||||
|
props.canRequest === true &&
|
||||||
|
contextOffersPath(
|
||||||
|
props.workContext as WorkContextState | null | undefined,
|
||||||
|
'/requisicoes/criar',
|
||||||
|
);
|
||||||
|
const publicNavigation = !!props.publicNavigation;
|
||||||
const cards = [
|
const cards = [
|
||||||
{
|
{
|
||||||
label: 'Requisições',
|
label: 'Requisições',
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import { expect, test } from 'vitest';
|
||||||
|
import { contextOffersPath } from '@/lib/work-context-paths';
|
||||||
|
import type { WorkContextState } from '@/types/work-context';
|
||||||
|
|
||||||
|
const context = (
|
||||||
|
active: string | null,
|
||||||
|
paths: string[],
|
||||||
|
enforced = true,
|
||||||
|
): WorkContextState => ({
|
||||||
|
enforced,
|
||||||
|
active,
|
||||||
|
available: ['procurement.internal', 'people.internal'],
|
||||||
|
purchased: [],
|
||||||
|
paths,
|
||||||
|
sharedPaths: ['/dashboard'],
|
||||||
|
permissions: [],
|
||||||
|
options: {},
|
||||||
|
});
|
||||||
|
|
||||||
|
test('sem contextos activos, o atalho segue só a permissão', () => {
|
||||||
|
expect(contextOffersPath(null, '/requisicoes/criar')).toBe(true);
|
||||||
|
expect(contextOffersPath(undefined, '/requisicoes/criar')).toBe(true);
|
||||||
|
expect(
|
||||||
|
contextOffersPath(context(null, [], false), '/requisicoes/criar'),
|
||||||
|
).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('no contexto Pessoas não aparece o atalho para nova requisição', () => {
|
||||||
|
const people = context('people.internal', ['/dashboard', '/pessoas']);
|
||||||
|
|
||||||
|
expect(contextOffersPath(people, '/requisicoes/criar')).toBe(false);
|
||||||
|
expect(contextOffersPath(people, '/pessoas')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('no contexto Compras o atalho aparece, mesmo com parâmetros', () => {
|
||||||
|
const procurement = context('procurement.internal', [
|
||||||
|
'/dashboard',
|
||||||
|
'/requisicoes',
|
||||||
|
'/requisicoes/criar',
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(contextOffersPath(procurement, '/requisicoes/criar')).toBe(true);
|
||||||
|
expect(contextOffersPath(procurement, '/requisicoes?view=mine')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('sem contexto escolhido só aparecem caminhos comuns', () => {
|
||||||
|
const none = context(null, ['/dashboard']);
|
||||||
|
|
||||||
|
expect(contextOffersPath(none, '/requisicoes/criar')).toBe(false);
|
||||||
|
expect(contextOffersPath(none, '/dashboard')).toBe(true);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user