Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions INVENTORY_SETUP_GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# 🔧 Inventory System Setup Guide

## 🚨 **Current Issue: No Products Showing**

The inventory system is not showing products because the **Supabase database connection is not configured**.

## ✅ **Quick Fix**

### **Step 1: Configure Supabase Connection**

1. **Create a Supabase project** (if you don't have one):
- Go to [supabase.com](https://supabase.com)
- Create a new project
- Note down your project URL and anon key

2. **Update your `.env` file** with your actual Supabase credentials:
```env
VITE_SUPABASE_URL=https://your-project-id.supabase.co
VITE_SUPABASE_ANON_KEY=your_actual_anon_key_here
```

3. **Run the database migrations**:
```bash
# Apply the inventory schema
npx supabase db push
```

### **Step 2: Test the Connection**

Run the diagnostic script to verify the connection:
```bash
node debug_inventory.js
```

### **Step 3: Start the Application**

```bash
npm run dev
```

## 🎯 **Expected Results**

After proper configuration, you should see:
- ✅ Products loading in the inventory page
- ✅ Categories and suppliers data
- ✅ No "Database connection failed" errors

## 🔍 **Troubleshooting**

### **Error: "Invalid API key"**
- Check that your `VITE_SUPABASE_ANON_KEY` is correct
- Make sure there are no extra spaces or quotes

### **Error: "fetch failed"**
- Check your internet connection
- Verify the `VITE_SUPABASE_URL` is correct

### **Error: "No products found"**
- Run the database migrations
- Check if the `lats_products` table exists
- Verify RLS policies allow reading

## 📊 **Fallback Data**

If you can't connect to Supabase immediately, the system will show sample products so you can test the UI functionality.

## 🆘 **Need Help?**

1. Check the browser console for detailed error messages
2. Verify your Supabase project is active
3. Ensure your database has the required tables
4. Check RLS policies allow public read access

---

**Note**: The inventory system is designed to work offline with sample data, but for full functionality, you need a working Supabase connection.
2 changes: 1 addition & 1 deletion src/components/StockValueCalculator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ const StockValueCalculator: React.FC = () => {
<span className="font-semibold text-yellow-600">{data.lowStockProducts}</span>
</div>
<div className="flex justify-between">
<span className="text-green-600">✅ Well Stocked (>5):</span>
<span className="text-green-600">✅ Well Stocked ({'>'}5):</span>
<span className="font-semibold text-green-600">{data.wellStockedProducts}</span>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/features/lats/pages/UnifiedInventoryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,11 @@ const UnifiedInventoryPage: React.FC = () => {
{dbStatus === 'connected' ? 'Database Connected' :
dbStatus === 'connecting' ? 'Connecting...' : 'Connection Error'}
</span>
{dbStatus === 'error' && (
<span className="text-xs text-red-500 ml-2">
Check Supabase config in .env
</span>
)}
{isDataLoading && (
<div className="flex items-center gap-1">
<RefreshCw className="w-3 h-3 animate-spin text-blue-500" />
Expand Down
21 changes: 17 additions & 4 deletions src/features/lats/stores/useInventoryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,8 @@ export const useInventoryStore = create<InventoryState>()(
};

// Check cache if no filters applied and cache is valid
// Temporarily disabled to test supplier data loading and ensure fresh data
if (false && !filters && state.isCacheValid('products')) {
console.log('🔍 [useInventoryStore] Using cached products (supplier data may be outdated)');

if (!filters && state.isCacheValid('products')) {
console.log('🔍 [useInventoryStore] Using cached products');
set({ products: state.dataCache.products || [] });
return;
}
Expand Down Expand Up @@ -840,6 +838,21 @@ export const useInventoryStore = create<InventoryState>()(
}
} catch (error) {
console.error('Exception in loadProducts:', error);

// Check if it's a connection or authentication error
if (error instanceof Error && (
error.message.includes('fetch failed') ||
error.message.includes('Invalid API key') ||
error.message.includes('connection')
)) {
console.error('❌ Database connection failed - check Supabase configuration');
set({
error: 'Database connection failed. Please check your Supabase configuration in .env file.',
isLoading: false,
isDataLoading: false
});
return;
}

// Create fallback sample categories and products so the interface works

Expand Down