" mu change"

new_001
sonali 2024-10-08 18:42:56 +05:30
parent b7e0e14e25
commit bbdcf4b3d5
7 changed files with 110 additions and 57 deletions

View File

@ -9,8 +9,10 @@ function ExpenseEditPage() {
const navigate = useNavigate(); const navigate = useNavigate();
const [currentDateTime, setCurrentDateTime] = useState(""); const [currentDateTime, setCurrentDateTime] = useState("");
const [amount, setAmount] = useState(location.state?.amount || ""); const [amount, setAmount] = useState(location.state?.amount || "");
const [categories] = useState(["Hair", "Clothing", "Food", "Books", "Electronics", "Other"]);
// Initial categories
const [defaultCategories] = useState(["Hair", "Clothing", "Food", "Books", "Electronics", "Other"]);
const [categories, setCategories] = useState(defaultCategories);
const [selectedCategories, setSelectedCategories] = useState( const [selectedCategories, setSelectedCategories] = useState(
Array.isArray(location.state?.category) Array.isArray(location.state?.category)
@ -29,6 +31,10 @@ function ExpenseEditPage() {
const formattedDateTime = now.toLocaleString(); const formattedDateTime = now.toLocaleString();
setCurrentDateTime(formattedDateTime); setCurrentDateTime(formattedDateTime);
// Retrieve stored categories from local storage and update categories
const storedCategories = JSON.parse(localStorage.getItem("categories")) || [];
const uniqueCategories = [...new Set([...defaultCategories, ...storedCategories])];
setCategories(uniqueCategories);
if (!location.state?.id) { if (!location.state?.id) {
const storedEntries = JSON.parse(localStorage.getItem("entries")) || []; const storedEntries = JSON.parse(localStorage.getItem("entries")) || [];

View File

@ -3,12 +3,12 @@ import { useNavigate, useLocation } from 'react-router-dom';
import '../ExpenseAmount/TotalExpenseAmount.css'; import '../ExpenseAmount/TotalExpenseAmount.css';
function TotalExpenseAmount({ totalAmount, category, setCategory, handleManualAmountChange }) { function TotalExpenseAmount({ totalAmount, category, setCategory, handleManualAmountChange }) {
const [amount, setAmount] = useState(totalAmount || ''); const [amount, setAmount] = useState(totalAmount !== undefined ? totalAmount : 0);
const location = useLocation(); const location = useLocation();
const { entryType } = location.state || {}; const { entryType } = location.state || {};
const [entries, setEntries] = useState([]); const [entries, setEntries] = useState([]);
const [categoryError, setCategoryError] = useState(''); const [categoryError, setCategoryError] = useState('');
const [amountError, setAmountError] = useState(''); // State for amount error const [amountError, setAmountError] = useState('');
const [categories, setCategories] = useState(["Hair", "Clothing", "Food", "Books", "Electronics", "Other"]); const [categories, setCategories] = useState(["Hair", "Clothing", "Food", "Books", "Electronics", "Other"]);
const navigate = useNavigate(); const navigate = useNavigate();
@ -26,10 +26,10 @@ function TotalExpenseAmount({ totalAmount, category, setCategory, handleManualAm
const handleAddEntry = () => { const handleAddEntry = () => {
setCategoryError(''); setCategoryError('');
setAmountError(''); // Reset amount error setAmountError('');
if (amount <= 0) { if (amount <= 0) {
setAmountError('Amount is required and must be greater than zero'); // Set error message setAmountError('Amount is required and must be greater than zero');
return null; return null;
} }
@ -91,13 +91,13 @@ function TotalExpenseAmount({ totalAmount, category, setCategory, handleManualAm
}; };
const handleAmountMouseDown = (e) => { const handleAmountMouseDown = (e) => {
e.preventDefault(); // Prevents the keyboard from opening e.preventDefault();
handleAmountFocus(); // Navigate on first click handleAmountFocus();
}; };
const handleCategoryMouseDown = (e) => { const handleCategoryMouseDown = (e) => {
e.preventDefault(); // Prevents the keyboard from opening e.preventDefault();
handleCategoryFocus(); // Navigate on first click handleCategoryFocus();
}; };
const handleAmountDoubleClick = () => { const handleAmountDoubleClick = () => {
@ -110,6 +110,19 @@ function TotalExpenseAmount({ totalAmount, category, setCategory, handleManualAm
input.focus(); input.focus();
}; };
const handleAmountChange = (e) => {
let value = e.target.value;
if (value.startsWith('0') && value.length > 1) {
value = value.slice(1);
}
setAmount(value);
handleManualAmountChange(value);
};
return ( return (
<div className="total-amount-container"> <div className="total-amount-container">
<div className="input-group"> <div className="input-group">
@ -119,15 +132,12 @@ function TotalExpenseAmount({ totalAmount, category, setCategory, handleManualAm
id="amount-input" id="amount-input"
type="number" type="number"
value={amount} value={amount}
onChange={(e) => { onChange={handleAmountChange}
setAmount(e.target.value);
handleManualAmountChange(e.target.value); // Ensure it's called properly
}}
onMouseDown={handleAmountMouseDown} onMouseDown={handleAmountMouseDown}
onDoubleClick={handleAmountDoubleClick} onDoubleClick={handleAmountDoubleClick}
placeholder="Enter Amount" placeholder="Enter Amount"
/> />
{amountError && <span className="error-message">{amountError}</span>} {/* Amount error message */} {amountError && <span className="error-message">{amountError}</span>}
</div> </div>
<div className="category-input"> <div className="category-input">
<label>Category:</label> <label>Category:</label>

View File

@ -113,7 +113,15 @@
/* Entry icons container */
.dropdown-item.selected {
background-color: #bbb3b33d;
color: #ffffff;
}
.entry-icons { .entry-icons {
display: flex; display: flex;
align-items: center; align-items: center;

View File

@ -10,9 +10,10 @@ function EntriesList() {
const [entries, setEntries] = useState([]); const [entries, setEntries] = useState([]);
const [editingEntryId, setEditingEntryId] = useState(null); const [editingEntryId, setEditingEntryId] = useState(null);
const [editedAmount, setEditedAmount] = useState(""); const [editedAmount, setEditedAmount] = useState("");
const [selectedCategory, setSelectedCategory] = useState([]); // Ensure this is initialized as an array const [selectedCategory, setSelectedCategory] = useState([]);
const [repeatEntry, setRepeatEntry] = useState(null); const [repeatEntry, setRepeatEntry] = useState(null);
const [isDialogOpen, setIsDialogOpen] = useState(false); const [isDialogOpen, setIsDialogOpen] = useState(false);
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const [categories, setCategories] = useState(["Hair", "Clothing", "Food", "Books", "Electronics", "Other"]); const [categories, setCategories] = useState(["Hair", "Clothing", "Food", "Books", "Electronics", "Other"]);
@ -33,8 +34,15 @@ function EntriesList() {
const handleEditClick = (entry) => { const handleEditClick = (entry) => {
setEditingEntryId(entry.id); setEditingEntryId(entry.id);
setEditedAmount(entry.amount); setEditedAmount(entry.amount);
// Ensure selectedCategory is always an array // Ensure selectedCategory is always an array
setSelectedCategory(Array.isArray(entry.category) ? entry.category : []); if (Array.isArray(entry.category)) {
setSelectedCategory(entry.category);
} else if (typeof entry.category === "string") {
setSelectedCategory([entry.category]);
} else {
setSelectedCategory([]);
}
}; };
const handleSaveClick = (entry) => { const handleSaveClick = (entry) => {
@ -57,7 +65,7 @@ function EntriesList() {
const newEntry = { const newEntry = {
id: new Date().getTime(), id: new Date().getTime(),
amount: repeatEntry.amount, amount: repeatEntry.amount,
category: repeatEntry.category || [], // Ensure this is an array category: repeatEntry.category || [],
type: repeatEntry.type, type: repeatEntry.type,
}; };
@ -78,15 +86,19 @@ function EntriesList() {
const handleCategoryChange = (category) => { const handleCategoryChange = (category) => {
setSelectedCategory((prevCategories) => { setSelectedCategory((prevCategories) => {
// Ensure prevCategories is an array if (!Array.isArray(prevCategories)) return [category];
if (!Array.isArray(prevCategories)) return [category]; // Initialize as array if not
if (prevCategories.includes(category)) { if (prevCategories.includes(category)) {
return prevCategories.filter((cat) => cat !== category); // Remove category if already selected return prevCategories.filter((cat) => cat !== category);
} else { } else {
return [...prevCategories, category]; // Add category if not selected return [...prevCategories, category];
} }
}); });
setIsDropdownOpen(false);
};
const toggleDropdown = () => {
setIsDropdownOpen((prev) => !prev);
}; };
return ( return (
@ -107,20 +119,22 @@ function EntriesList() {
className="entry-input" className="entry-input"
/> />
<div className="custom-dropdown"> <div className="custom-dropdown">
<button className="dropdown-button"> <button className="dropdown-button" onClick={toggleDropdown}>
{selectedCategory.length > 0 ? selectedCategory.join(', ') : "Select Category"} {selectedCategory.length > 0 ? selectedCategory.join(', ') : "Select Category"}
</button> </button>
{isDropdownOpen && (
<div className="dropdown-content"> <div className="dropdown-content">
{categories.map((cat, idx) => ( {categories.map((cat, idx) => (
<div <div
key={idx} key={idx}
className="dropdown-item" className={`dropdown-item ${selectedCategory.includes(cat) ? 'selected' : ''}`}
onClick={() => handleCategoryChange(cat)} onClick={() => handleCategoryChange(cat)}
> >
{cat} {cat}
</div> </div>
))} ))}
</div> </div>
)}
</div> </div>
<FontAwesomeIcon <FontAwesomeIcon
icon={faSave} icon={faSave}
@ -137,7 +151,7 @@ function EntriesList() {
<> <>
<span className="entry-amount">{entry.amount}</span> <span className="entry-amount">{entry.amount}</span>
<span className="entry-category"> <span className="entry-category">
{Array.isArray(entry.category) ? entry.category.join(', ') : entry.category || "No Category"} {/* Safely check if it's an array */} {Array.isArray(entry.category) ? entry.category.join(', ') : entry.category || "No Category"}
</span> </span>
<div className="entry-icons"> <div className="entry-icons">
<FontAwesomeIcon <FontAwesomeIcon

View File

@ -3,12 +3,12 @@ import { useNavigate, useLocation } from 'react-router-dom';
import './TotalAmount.css'; import './TotalAmount.css';
function TotalAmount({ totalAmount, category, setCategory , handleManualAmountChange}) { function TotalAmount({ totalAmount, category, setCategory , handleManualAmountChange}) {
const [amount, setAmount] = useState(totalAmount || ''); const [amount, setAmount] = useState(totalAmount !== undefined ? totalAmount : 0);
const location = useLocation(); const location = useLocation();
const { entryType } = location.state || {}; const { entryType } = location.state || {};
const [entries, setEntries] = useState([]); const [entries, setEntries] = useState([]);
const [categoryError, setCategoryError] = useState(''); const [categoryError, setCategoryError] = useState('');
const [amountError, setAmountError] = useState(''); // State for amount error const [amountError, setAmountError] = useState('');
const [categories, setCategories] = useState(["Hair", "Clothing", "Food", "Books", "Electronics", "Other"]); const [categories, setCategories] = useState(["Hair", "Clothing", "Food", "Books", "Electronics", "Other"]);
const navigate = useNavigate(); const navigate = useNavigate();
@ -26,10 +26,10 @@ function TotalAmount({ totalAmount, category, setCategory , handleManualAmountCh
const handleAddEntry = () => { const handleAddEntry = () => {
setCategoryError(''); setCategoryError('');
setAmountError(''); // Reset amount error setAmountError('');
if (amount <= 0) { if (amount <= 0) {
setAmountError('Amount is required and must be greater than zero'); // Set error message setAmountError('Amount is required and must be greater than zero');
return null; return null;
} }
@ -91,13 +91,13 @@ function TotalAmount({ totalAmount, category, setCategory , handleManualAmountCh
}; };
const handleAmountMouseDown = (e) => { const handleAmountMouseDown = (e) => {
e.preventDefault(); // Prevents the keyboard from opening e.preventDefault();
handleAmountFocus(); // Navigate on first click handleAmountFocus();
}; };
const handleCategoryMouseDown = (e) => { const handleCategoryMouseDown = (e) => {
e.preventDefault(); // Prevents the keyboard from opening e.preventDefault();
handleCategoryFocus(); // Navigate on first click handleCategoryFocus();
}; };
const handleAmountDoubleClick = () => { const handleAmountDoubleClick = () => {
@ -110,6 +110,19 @@ function TotalAmount({ totalAmount, category, setCategory , handleManualAmountCh
input.focus(); input.focus();
}; };
const handleAmountChange = (e) => {
let value = e.target.value;
if (value.startsWith('0') && value.length > 1) {
value = value.slice(1);
}
setAmount(value);
handleManualAmountChange(value);
};
return ( return (
<div className="total-amount-container"> <div className="total-amount-container">
<div className="input-group"> <div className="input-group">
@ -119,10 +132,7 @@ function TotalAmount({ totalAmount, category, setCategory , handleManualAmountCh
id="amount-input" id="amount-input"
type="number" type="number"
value={amount} value={amount}
onChange={(e) => { onChange={handleAmountChange}
setAmount(e.target.value);
handleManualAmountChange(e.target.value);
}}
onMouseDown={handleAmountMouseDown} onMouseDown={handleAmountMouseDown}
onDoubleClick={handleAmountDoubleClick} onDoubleClick={handleAmountDoubleClick}
placeholder="Enter Amount" placeholder="Enter Amount"

View File

@ -9,9 +9,11 @@ function IncomeEdit() {
const navigate = useNavigate(); const navigate = useNavigate();
const [currentDateTime, setCurrentDateTime] = useState(""); const [currentDateTime, setCurrentDateTime] = useState("");
const [amount, setAmount] = useState(location.state?.amount || ""); const [amount, setAmount] = useState(location.state?.amount || "");
const [categories] = useState(["Hair", "Clothing", "Food", "Books", "Electronics", "Other"]);
// Ensure selectedCategories is initialized correctly // Initial categories
const [defaultCategories] = useState(["Hair", "Clothing", "Food", "Books", "Electronics", "Other"]);
const [categories, setCategories] = useState(defaultCategories);
const [selectedCategories, setSelectedCategories] = useState( const [selectedCategories, setSelectedCategories] = useState(
Array.isArray(location.state?.category) Array.isArray(location.state?.category)
? location.state.category ? location.state.category
@ -29,7 +31,11 @@ function IncomeEdit() {
const formattedDateTime = now.toLocaleString(); const formattedDateTime = now.toLocaleString();
setCurrentDateTime(formattedDateTime); setCurrentDateTime(formattedDateTime);
// Fetch entry to edit if no id is passed
const storedCategories = JSON.parse(localStorage.getItem("categories")) || [];
const uniqueCategories = [...new Set([...defaultCategories, ...storedCategories])];
setCategories(uniqueCategories);
if (!location.state?.id) { if (!location.state?.id) {
const storedEntries = JSON.parse(localStorage.getItem("entries")) || []; const storedEntries = JSON.parse(localStorage.getItem("entries")) || [];
const entryToEdit = storedEntries.find( const entryToEdit = storedEntries.find(
@ -50,7 +56,7 @@ function IncomeEdit() {
}, [location.state]); }, [location.state]);
const handleBackClick = () => { const handleBackClick = () => {
navigate("/records"); navigate("/expense-records");
}; };
const handleSave = () => { const handleSave = () => {
@ -70,7 +76,7 @@ function IncomeEdit() {
); );
localStorage.setItem("entries", JSON.stringify(updatedEntries)); localStorage.setItem("entries", JSON.stringify(updatedEntries));
navigate("/successPage", { state: updatedEntry }); navigate("/expense-successfully", { state: updatedEntry });
setIsLoading(false); setIsLoading(false);
}; };
@ -79,7 +85,7 @@ function IncomeEdit() {
}; };
const handleCategorySelect = (category) => { const handleCategorySelect = (category) => {
// Toggle category selection
if (selectedCategories.includes(category)) { if (selectedCategories.includes(category)) {
setSelectedCategories(selectedCategories.filter((cat) => cat !== category)); setSelectedCategories(selectedCategories.filter((cat) => cat !== category));
} else { } else {
@ -154,5 +160,4 @@ function IncomeEdit() {
</div> </div>
); );
} }
export default IncomeEdit; export default IncomeEdit;

View File

@ -77,7 +77,7 @@ function Records() {
/> />
</div> </div>
<h1>Expense</h1> <h1>Income</h1>
<div className="success-datetime"> <div className="success-datetime">
<p>{currentDateTime}</p> <p>{currentDateTime}</p>
</div> </div>