feat: enhance pedidos functionality by adding new submenu options for creating and planning orders, improving user navigation and access control in the sidebar; also implement URL-based prefill for adding items, ensuring a smoother user experience when creating pedidos
This commit is contained in:
@@ -20,9 +20,10 @@
|
||||
X,
|
||||
XCircle
|
||||
} from 'lucide-svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
import { afterNavigate, goto } from '$app/navigation';
|
||||
import { resolve } from '$app/paths';
|
||||
import { page } from '$app/state';
|
||||
import { onMount } from 'svelte';
|
||||
import { maskCurrencyBRL } from '$lib/utils/masks';
|
||||
import { formatarDataBR } from '$lib/utils/datas';
|
||||
|
||||
@@ -167,7 +168,6 @@
|
||||
itemsQuery.isLoading ||
|
||||
historyQuery.isLoading ||
|
||||
objetosQuery.isLoading ||
|
||||
objetosQuery.isLoading ||
|
||||
acoesQuery.isLoading ||
|
||||
permissionsQuery.isLoading ||
|
||||
requestsQuery.isLoading ||
|
||||
@@ -394,6 +394,7 @@
|
||||
|
||||
// Add Item State
|
||||
let showAddItem = $state(false);
|
||||
let hasAppliedAddItemPrefill = $state(false);
|
||||
let newItem = $state({
|
||||
objetoId: '' as string,
|
||||
valorEstimado: '',
|
||||
@@ -404,7 +405,46 @@
|
||||
});
|
||||
let addingItem = $state(false);
|
||||
|
||||
let hasAppliedPrefill = $state(false);
|
||||
function applyAddItemPrefillFromUrl() {
|
||||
if (hasAppliedAddItemPrefill) return;
|
||||
|
||||
const obj = page.url.searchParams.get('obj');
|
||||
if (!obj) return;
|
||||
|
||||
const qtdRaw = page.url.searchParams.get('qtd');
|
||||
const qtd = qtdRaw ? Number.parseInt(qtdRaw, 10) : 1;
|
||||
|
||||
const mod = page.url.searchParams.get('mod') ?? '';
|
||||
const acao = page.url.searchParams.get('acao') ?? '';
|
||||
const ata = page.url.searchParams.get('ata') ?? '';
|
||||
|
||||
showAddItem = true;
|
||||
newItem.objetoId = obj;
|
||||
newItem.quantidade = Number.isFinite(qtd) && qtd > 0 ? qtd : 1;
|
||||
newItem.modalidade = coerceModalidade(mod);
|
||||
newItem.acaoId = acao;
|
||||
newItem.ataId = ata;
|
||||
|
||||
const objeto = objetos.find((o: Doc<'objetos'>) => o._id === obj);
|
||||
newItem.valorEstimado = maskCurrencyBRL(objeto?.valorEstimado || '');
|
||||
|
||||
void loadAtasForObjeto(obj);
|
||||
|
||||
hasAppliedAddItemPrefill = true;
|
||||
void goto(resolve(`/pedidos/${pedidoId}`), {
|
||||
replaceState: true,
|
||||
noScroll: true,
|
||||
keepFocus: true
|
||||
});
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
applyAddItemPrefillFromUrl();
|
||||
});
|
||||
|
||||
afterNavigate(() => {
|
||||
applyAddItemPrefillFromUrl();
|
||||
});
|
||||
|
||||
// Edit SEI State
|
||||
let editingSei = $state(false);
|
||||
@@ -470,47 +510,6 @@
|
||||
selectedObjeto = null;
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (hasAppliedPrefill) return;
|
||||
if (objetosQuery.isLoading || acoesQuery.isLoading) return;
|
||||
|
||||
const url = page.url;
|
||||
const obj = url.searchParams.get('obj');
|
||||
const qtdStr = url.searchParams.get('qtd');
|
||||
const mod = url.searchParams.get('mod') as Modalidade | null;
|
||||
const acao = url.searchParams.get('acao');
|
||||
const ata = url.searchParams.get('ata');
|
||||
|
||||
if (!obj) return;
|
||||
|
||||
const objeto = objetos.find((o) => o._id === obj);
|
||||
if (!objeto) return;
|
||||
|
||||
let quantidade = parseInt(qtdStr || '1', 10);
|
||||
if (!Number.isFinite(quantidade) || quantidade <= 0) {
|
||||
quantidade = 1;
|
||||
}
|
||||
|
||||
const modalidade: Modalidade =
|
||||
mod === 'dispensa' || mod === 'inexgibilidade' || mod === 'adesao' || mod === 'consumo'
|
||||
? mod
|
||||
: 'consumo';
|
||||
|
||||
showAddItem = true;
|
||||
newItem = {
|
||||
objetoId: obj,
|
||||
valorEstimado: maskCurrencyBRL(objeto.valorEstimado || ''),
|
||||
quantidade,
|
||||
modalidade,
|
||||
acaoId: acao || '',
|
||||
ataId: ata || ''
|
||||
};
|
||||
|
||||
void loadAtasForObjeto(obj);
|
||||
|
||||
hasAppliedPrefill = true;
|
||||
});
|
||||
|
||||
async function handleAddItem() {
|
||||
if (!pedido || !newItem.objetoId || !newItem.valorEstimado) return;
|
||||
|
||||
@@ -519,17 +518,19 @@
|
||||
if (items.length > 0) {
|
||||
const referenceItem = items[0];
|
||||
|
||||
const referenceModalidade = referenceItem.modalidade as Modalidade;
|
||||
const referenceModalidade = (referenceItem.modalidade as Modalidade | undefined) ?? undefined;
|
||||
const referenceAtaId = (('ataId' in referenceItem ? referenceItem.ataId : undefined) ??
|
||||
null) as string | null;
|
||||
|
||||
const newAtaId = newItem.ataId || null;
|
||||
|
||||
const sameModalidade = newItem.modalidade === referenceModalidade;
|
||||
const sameModalidade = !referenceModalidade || newItem.modalidade === referenceModalidade;
|
||||
const sameAta = referenceAtaId === newAtaId;
|
||||
|
||||
if (!sameModalidade || !sameAta) {
|
||||
const refModalidadeLabel = formatModalidade(referenceModalidade);
|
||||
const refModalidadeLabel = referenceModalidade
|
||||
? formatModalidade(referenceModalidade)
|
||||
: 'Não definida';
|
||||
const refAtaLabel =
|
||||
referenceAtaId === null ? 'sem Ata vinculada' : 'com uma Ata específica';
|
||||
|
||||
@@ -740,7 +741,7 @@
|
||||
|
||||
return {
|
||||
valorEstimado: maskCurrencyBRL(item.valorEstimado || ''),
|
||||
modalidade: item.modalidade,
|
||||
modalidade: coerceModalidade((item.modalidade as string | undefined) ?? 'consumo'),
|
||||
acaoId: item.acaoId ?? '',
|
||||
ataId: item.ataId ?? ''
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user