57 lines
2.0 KiB
TypeScript
57 lines
2.0 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { useParams, useSearchParams } from 'react-router';
|
|
import { useAuth } from '../auth/AuthContext';
|
|
import { hasAdministratorRole } from '../auth/adminAccess';
|
|
import { Alert, LoadingBlock, errorMessage } from '../components/Feedback';
|
|
import { getAsset } from '../lib/api';
|
|
import type { AssetDetail } from '../lib/api';
|
|
import { AssetEditorPage } from './AssetEditorPage';
|
|
import { CompanyInventoryPage } from './CompanyInventoryPage';
|
|
import { SimpleInventoryDetailPage, isSimpleInventoryAsset } from './SimpleInventoryDetailPage';
|
|
import { TerritorialInventoryPage, isTerritorialInventoryAsset } from './TerritorialInventoryPage';
|
|
|
|
export function InventoryDetailPage() {
|
|
const { id } = useParams();
|
|
const [params] = useSearchParams();
|
|
const { user } = useAuth();
|
|
const [asset, setAsset] = useState<AssetDetail | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState('');
|
|
|
|
useEffect(() => {
|
|
if (!id) {
|
|
setError('No se encontró el registro solicitado.');
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
setLoading(true);
|
|
setError('');
|
|
getAsset(id)
|
|
.then(setAsset)
|
|
.catch((requestError) => setError(errorMessage(requestError)))
|
|
.finally(() => setLoading(false));
|
|
}, [id]);
|
|
|
|
if (loading) return <LoadingBlock label="Cargando registro…" />;
|
|
if (!asset) return <Alert>{error || 'No se pudo cargar el registro.'}</Alert>;
|
|
|
|
const advancedRequested = params.get('advanced') === '1';
|
|
const advancedAllowed = hasAdministratorRole(user);
|
|
const advanced = advancedRequested && advancedAllowed;
|
|
const isCompany = asset.type.code.toLowerCase() === 'empresa';
|
|
|
|
if (isCompany && !advanced) {
|
|
return <CompanyInventoryPage initialAsset={asset} />;
|
|
}
|
|
|
|
if (isSimpleInventoryAsset(asset) && !advanced) {
|
|
return <SimpleInventoryDetailPage initialAsset={asset} />;
|
|
}
|
|
|
|
if (isTerritorialInventoryAsset(asset) && !advanced) {
|
|
return <TerritorialInventoryPage initialAsset={asset} />;
|
|
}
|
|
|
|
return <AssetEditorPage />;
|
|
}
|