Can we have a feature where we can edit the name of the todo item, provide another…
- User
Can we have a feature where we can edit the name of the todo item, provide another button to the left of complete and delete:
// src/App.js import React, { useState, useEffect } from 'react'; import { db } from './db';
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('');
useEffect(() => { loadCustomers(); }, []);
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 };
codeif (selectedCustomer && selectedCustomer.id === updatedCustomer.id) { setSelectedCustomer(updatedCustomer); } setCustomers((prevCustomers) => prevCustomers.map((customer) => customer.id === updatedCustomer.id ? updatedCustomer : customer ) ); setEditingCustomer(null); setEditName(''); } catch (error) { console.error('Error updating customer:', error); } }};
const toggleComplete = async (index) => { if (selectedCustomer) { try { const updatedTodos = selectedCustomer.todos.map((todo, i) => i === index ? { ...todo, completed: !todo.completed } : todo ); const updatedCustomer = { ...selectedCustomer, todos: updatedTodos };
code - Assistant