When I click on a customer name in the Customers tab, it has to expand to show the items…
- User
When I click on a customer name in the Customers tab, it has to expand to show the items for that particular customer:
// src/App.js import React, { useState, useEffect } from 'react'; import { db } from './db';
// Import Icons import { AiFillHome } from 'react-icons/ai'; import { FaUsers } from 'react-icons/fa';
function App() { const [customers, setCustomers] = useState([]); const [selectedCustomer, setSelectedCustomer] = useState(null); const [input, setInput] = useState(''); const [newCustomer, setNewCustomer] = useState(''); const [editingCustomer, setEditingCustomer] = useState(null); const [editName, setEditName] = useState(''); const [editingTodoIndex, setEditingTodoIndex] = useState(null); const [editTodoText, setEditTodoText] = useState('');
// New state variables for search, filter, theme, and tabs const [customerSearchTerm, setCustomerSearchTerm] = useState(''); const [todoFilter, setTodoFilter] = useState('all'); const [isDarkMode, setIsDarkMode] = useState(() => { const storedThemePreference = localStorage.getItem('isDarkMode'); return storedThemePreference ? JSON.parse(storedThemePreference) : false; });
// New state variable for active tab const [activeTab, setActiveTab] = useState('home'); // 'home' or 'customers'
useEffect(() => { loadCustomers(); }, []);
useEffect(() => { // Save theme preference localStorage.setItem('isDarkMode', JSON.stringify(isDarkMode)); }, [isDarkMode]);
const toggleDarkMode = () => { setIsDarkMode((prevMode) => !prevMode); };
const loadCustomers = async () => { try { const allCustomers = await db.customers.toArray(); setCustomers(allCustomers); if (allCustomers.length > 0) { // Preserve the selected customer if possible const currentCustomer = allCustomers.find( (c) => c.id === selectedCustomer?.id ); setSelectedCustomer(currentCustomer || null); // Do not automatically select the first customer } else { setSelectedCustomer(null); } } catch (error) { console.error('Error loading customers:', error); } };
const addCustomer = async () => { if (newCustomer.trim()) { try { await db.customers.add({ name: newCustomer, todos: [] }); await loadCustomers(); setNewCustomer(''); } catch (error) { console.error('Error adding customer:', error); } } };
const startEditingCustomer = (customer) => { setEditingCustomer(customer); setEditName(customer.name); };
const saveCustomerEdit = async () => { if (editName.trim() && editingCustomer) { try { await db.customers.update(editingCustomer.id, { name: editName }); const updatedCustomer = { ...editingCustomer, name: editName };
code - Assistant