fix: correct work-order navbar state and actions
Use prefix path matching so nested putni-nalozi routes highlight the correct nav item, load current user data in Navbar when auth token exists, and group PDF/DOCX work-order download buttons in one action block. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -4,7 +4,7 @@ import AuthWidget from './AuthWidget';
|
||||
import UserDisplay from './ui/UserDisplay';
|
||||
import { useEffect, useRef, useState } from 'preact/hooks';
|
||||
import { useSpring, animated } from '@react-spring/web';
|
||||
import { hydrateAuthFromStorage } from '../stores/authStore';
|
||||
import { hydrateAuthFromStorage, loadCurrentUser, $user, $accessToken } from '../stores/authStore';
|
||||
|
||||
const ITEMS = [
|
||||
{ id: 'dashboard', label: 'Dashboard', href: '/' },
|
||||
@@ -21,8 +21,12 @@ function normalizePath(p) {
|
||||
}
|
||||
|
||||
function getActiveIndex(path) {
|
||||
const idx = ITEMS.findIndex((item) => item.href === path);
|
||||
return idx >= 0 ? idx : 0;
|
||||
// Exact match (e.g. '/' → Dashboard)
|
||||
const exact = ITEMS.findIndex((item) => item.href === path);
|
||||
if (exact >= 0) return exact;
|
||||
// Prefix match for sub-paths (e.g. '/putni-nalozi/racuni' → Putni nalozi)
|
||||
const prefix = ITEMS.findIndex((item) => item.href !== '/' && path.startsWith(item.href));
|
||||
return prefix >= 0 ? prefix : 0;
|
||||
}
|
||||
|
||||
export default function Navbar({ minimal = false }) {
|
||||
@@ -55,6 +59,10 @@ export default function Navbar({ minimal = false }) {
|
||||
// Mount: izmjeri početnu poziciju bez animacije, pa uključi animaciju za buduće navigacije
|
||||
useEffect(() => {
|
||||
hydrateAuthFromStorage();
|
||||
// Učitaj korisnika ako token postoji ali $user još nije popunjen
|
||||
if (!$user.get() && $accessToken.get()) {
|
||||
loadCurrentUser();
|
||||
}
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
measureIndicator();
|
||||
|
||||
@@ -321,6 +321,7 @@ export default function WorkOrderInvoicesPdfPage() {
|
||||
{workOrder ? `Dizalica: ${workOrder.crane_label || workOrder.crane || '-'}` : 'Pregled računa prije kreiranja PDF-a.'}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => downloadWorkOrderPdf(workOrderId)}
|
||||
@@ -339,6 +340,7 @@ export default function WorkOrderInvoicesPdfPage() {
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-xl border border-border-hairline bg-canvas-elevated p-4">
|
||||
<div className="mb-3 flex flex-wrap items-center justify-between gap-2">
|
||||
|
||||
Reference in New Issue
Block a user