prets entre utilisateurs

This commit is contained in:
AISSI-JUDE-CHRIST
2026-06-14 18:23:57 +02:00
parent 0d9f8dc539
commit e8ac8f0c54
6 changed files with 160 additions and 1 deletions
+45
View File
@@ -0,0 +1,45 @@
import { createContext, useContext, useState } from 'react';
const LoanContext = createContext(null);
export function LoanProvider({ children }) {
const [loans, setLoans] = useState(() => {
const saved = localStorage.getItem('loans');
return saved ? JSON.parse(saved) : [];
});
function addLoan(book, borrowerPhone, dueDate) {
const loan = {
loanId: crypto.randomUUID(),
bookId: book.isbn,
bookTitle: book.title,
bookAuthor: book.author,
borrowerPhone,
dueDate,
status: 'ACTIVE',
loanedAt: new Date().toISOString(),
};
const updated = [...loans, loan];
setLoans(updated);
localStorage.setItem('loans', JSON.stringify(updated));
return loan;
}
function returnLoan(loanId) {
const updated = loans.map(l =>
l.loanId === loanId ? { ...l, status: 'RETURNED' } : l
);
setLoans(updated);
localStorage.setItem('loans', JSON.stringify(updated));
}
return (
<LoanContext.Provider value={{ loans, addLoan, returnLoan }}>
{children}
</LoanContext.Provider>
);
}
export function useLoans() {
return useContext(LoanContext);
}