Skip to content

Commit

Permalink
neon ki jai
Browse files Browse the repository at this point in the history
  • Loading branch information
R0gue-one committed Jan 27, 2025
1 parent 2978627 commit d80a3cd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
1 change: 1 addition & 0 deletions backend/app/routes/admin_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def manage_credits():
if request.method == 'POST':
data = request.json
new_credit = Credit(
id=data['creditId'],
name=data['name'],
amount=data['amount'],
price=data['price'],
Expand Down
2 changes: 1 addition & 1 deletion client/src/api/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';

const API_URL = process.env.REACT_APP_API_URL || 'http://localhost:5000';
const API_URL = 'http://localhost:5000/api';

const api = axios.create({
baseURL: API_URL,
Expand Down
33 changes: 18 additions & 15 deletions client/src/components/AdminDashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,36 @@ const AdminDashboard = () => {
fetchCredits();
}, []);

const [newCredit, setNewCredit] = useState({creditId:0, name: '', amount: 0, price: 0 });
const [newCredit, setNewCredit] = useState({creditId:0, name: '', amount: '', price: '' });

const handleCreateCredit = async (e) => {
e.preventDefault();

if (!newCredit.name || !newCredit.amount || !newCredit.price) {
alert("Please fill in all fields!");
return;
}

try {

let newCreditId = getNextCreditId();
setNewCredit((prevState) => ({ ...prevState, creditId: newCreditId }));

await generateCredit(newCredit.amount, newCredit.price);
const response = await createAdminCredit(newCredit);

const newCreditId = await getNextCreditId(); // Resolve the promise
console.log("Resolved newCredit ID:", newCreditId);

const updatedCredit = { ...newCredit, creditId: Number(newCreditId) }; // Update with the resolved ID
console.log("Updated Credit Object:", updatedCredit);

await generateCredit(updatedCredit.amount, updatedCredit.price); // Use updated credit here
const response = await createAdminCredit(updatedCredit);

// Refetch the updated credit list after successful creation
const updatedCredits = await getAdminCredits();
setMyCredits(updatedCredits.data);

setNewCredit({ name: '', amount: 0, price: 0 });
setNewCredit({ name: "", amount: 0, price: 0, creditId: 0 });
} catch (error) {
console.error('Failed to create credit:', error);
console.error("Failed to create credit:", error);
}
};


const handleInputChange = (e) => {
setNewCredit({ ...newCredit, [e.target.name]: e.target.value });
Expand Down Expand Up @@ -136,7 +139,7 @@ const AdminDashboard = () => {

const handleExpireCredit = async (creditId) => {
console.log(`Expire credit called for credit ID: ${creditId}`);
const SC_Credit_Id = creditId - 1;
const SC_Credit_Id = creditId;

try {
const response = await expireCreditApi(creditId);
Expand Down Expand Up @@ -244,7 +247,7 @@ const AdminDashboard = () => {
>
<div className="w-0 flex-1 flex items-center">
<span className="ml-2 flex-1 w-0 truncate">
{credit.id - 1}: {credit.name} - Amount: {credit.amount}, Price: {credit.price} ETH
{credit.id}: {credit.name} - Amount: {credit.amount}, Price: {credit.price} ETH
</span>
</div>
{!credit.is_expired && (
Expand Down
10 changes: 5 additions & 5 deletions client/src/components/BuyerDashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ const BuyerDashboard = () => {
setError(null);
//NOTE: the -1 is temporary
// const price = await getPrice();
const credit = await getCreditDetails(creditId - 1);
const credit = await getCreditDetails(creditId );
// Convert the price from wei to ether for the transaction
const priceInEther = ethers.formatEther(credit.price);
console.log("id, price: ", creditId - 1, priceInEther);
await buyCredit(creditId - 1, priceInEther);
console.log("id, price: ", creditId , priceInEther);
await buyCredit(creditId , priceInEther);
await purchaseCredit({ credit_id: creditId, amount: 1 });
await fetchAllCredits(); // Refresh both available and purchased credits
} catch (error) {
Expand Down Expand Up @@ -124,7 +124,7 @@ const BuyerDashboard = () => {
console.log(`Credit put on sale with price: ${updatedCredit.salePrice}`);

// Call API to mark credit as on sale in the backend and contract
await sellCredit(creditId - 1, updatedCredit.salePrice);
await sellCredit(creditId , updatedCredit.salePrice);
const respose = await sellCreditApi({ credit_id: creditId, salePrice: updatedCredit.salePrice });
console.log(respose);
await fetchAllCredits();
Expand All @@ -145,7 +145,7 @@ const BuyerDashboard = () => {
);

// Call API to remove the credit from sale in the backend
await removeFromSale(creditId - 1);
await removeFromSale(creditId );
await removeSaleCreditApi({ credit_id: creditId })
console.log(`Removed credit ID ${creditId} from sale`);
await fetchAllCredits();
Expand Down

0 comments on commit d80a3cd

Please sign in to comment.