88 Package ,
99 ChevronDown ,
1010} from 'lucide-react' ;
11- import { productsAPI } from '../services/api' ;
11+ import { productsAPI , inventoryAPI } from '../services/api' ;
1212import type { Product } from '../services/api' ;
1313import { useAuth } from '../contexts/AuthContext' ;
1414
@@ -27,7 +27,7 @@ const InventoryPage: React.FC = () => {
2727
2828 // Fetch products from backend
2929 useEffect ( ( ) => {
30- const fetchProducts = async ( ) => {
30+ const fetchInventory = async ( ) => {
3131 if ( ! isAuthenticated ) {
3232 setError ( 'You must log in to view inventory' ) ;
3333 setLoading ( false ) ;
@@ -36,21 +36,60 @@ const InventoryPage: React.FC = () => {
3636
3737 try {
3838 setLoading ( true ) ;
39- const response = await productsAPI . getAll ( ) ;
40- if ( response . success ) {
41- setInventoryItems ( response . products ) ;
42- } else {
43- setError ( 'Error loading products' ) ;
39+ const response = await inventoryAPI . getUserInventory ( ) ;
40+
41+ if ( ! response . success ) {
42+ setError ( 'Error loading inventory' ) ;
43+ return ;
4444 }
45+
46+ const mappedProducts : Product [ ] = response . inventory . map ( ( item ) => {
47+ const anyItem = item as unknown as Record < string , unknown > ;
48+
49+ // If backend returns an embedded product object
50+ if ( anyItem . product && typeof anyItem . product === 'object' ) {
51+ return anyItem . product as Product ;
52+ }
53+
54+ // If inventory item already contains product-like fields
55+ if ( anyItem . name || anyItem . price !== undefined ) {
56+ return {
57+ id : ( anyItem . product_id as number ) ?? ( anyItem . id as number ) ?? 0 ,
58+ name :
59+ ( anyItem . name as string ) ??
60+ `Product ${
61+ ( anyItem . product_id as number ) ?? ( anyItem . id as number ) ?? ''
62+ } `,
63+ description : ( anyItem . description as string ) ?? null ,
64+ price : Number ( anyItem . price ?? 0 ) ,
65+ stock : Number ( anyItem . quantity ?? 0 ) ,
66+ category : ( anyItem . category as string ) ?? null ,
67+ status : ( anyItem . status as 'active' | 'inactive' ) ?? 'inactive' ,
68+ } as Product ;
69+ }
70+
71+ // Fallback minimal product constructed from inventory item
72+ return {
73+ id : ( anyItem . product_id as number ) ?? ( anyItem . id as number ) ?? 0 ,
74+ name : `Product ${
75+ ( anyItem . product_id as number ) ?? ( anyItem . id as number ) ?? ''
76+ } `,
77+ price : 0 ,
78+ stock : Number ( anyItem . quantity ?? 0 ) ,
79+ } as Product ;
80+ } ) ;
81+
82+ setInventoryItems ( mappedProducts ) ;
83+ setError ( null ) ;
4584 } catch ( err ) {
46- console . error ( 'Error fetching products :' , err ) ;
47- setError ( 'Error loading products ' ) ;
85+ console . error ( 'Error fetching inventory :' , err ) ;
86+ setError ( 'Error loading inventory ' ) ;
4887 } finally {
4988 setLoading ( false ) ;
5089 }
5190 } ;
5291
53- fetchProducts ( ) ;
92+ fetchInventory ( ) ;
5493 } , [ isAuthenticated ] ) ;
5594
5695 const categories = [
0 commit comments