better connections

This commit is contained in:
Matt 2025-05-13 20:48:28 +02:00
parent c29b15cd0b
commit 444ab364a4
2 changed files with 38 additions and 16 deletions

View File

@ -14,7 +14,7 @@ const DEFAULT_PORTFOLIO: Portfolio = {
description: "Carbon sequestration through community tree planting",
shortDescription: "Tree planting projects",
imageUrl: "https://images.unsplash.com/photo-1513836279014-a89f7a76ae86",
pricePerTon: 15.63,
pricePerTon: 284.63,
location: "Global",
type: "Nature Based",
verificationStandard: "Gold Standard",
@ -23,7 +23,7 @@ const DEFAULT_PORTFOLIO: Portfolio = {
}
}
],
pricePerTon: 15.63,
pricePerTon: 284.63,
currency: 'USD'
};
@ -125,20 +125,40 @@ export async function getPortfolios(): Promise<Portfolio[]> {
}
return response.data.portfolios.map((portfolio: any) => {
let pricePerTon = 15.63; // Default price based on the tutorial
let pricePerTon = 18; // Default price based on the Wren Climate Fund average
// Check for both pricePerTon and costPerTon as the API might use different formats
if (portfolio.pricePerTon !== undefined && portfolio.pricePerTon !== null) {
pricePerTon = typeof portfolio.pricePerTon === 'number' ? portfolio.pricePerTon : parseFloat(portfolio.pricePerTon) || 15.63;
// The API returns cost_per_ton in snake_case
if (portfolio.cost_per_ton !== undefined && portfolio.cost_per_ton !== null) {
pricePerTon = typeof portfolio.cost_per_ton === 'number' ? portfolio.cost_per_ton : parseFloat(portfolio.cost_per_ton) || 18;
} else if (portfolio.costPerTon !== undefined && portfolio.costPerTon !== null) {
pricePerTon = typeof portfolio.costPerTon === 'number' ? portfolio.costPerTon : parseFloat(portfolio.costPerTon) || 15.63;
pricePerTon = typeof portfolio.costPerTon === 'number' ? portfolio.costPerTon : parseFloat(portfolio.costPerTon) || 18;
} else if (portfolio.pricePerTon !== undefined && portfolio.pricePerTon !== null) {
pricePerTon = typeof portfolio.pricePerTon === 'number' ? portfolio.pricePerTon : parseFloat(portfolio.pricePerTon) || 18;
}
// Convert from snake_case to camelCase for projects
const projects = portfolio.projects?.map(project => {
return {
id: project.id || `project-${Math.random().toString(36).substring(2, 9)}`,
name: project.name,
description: project.description || '',
shortDescription: project.short_description || project.description || '',
imageUrl: project.image_url, // Map from snake_case API response
pricePerTon: project.cost_per_ton || pricePerTon,
location: project.location || 'Global',
type: project.type || 'Nature Based',
verificationStandard: project.verification_standard || 'Gold Standard',
impactMetrics: {
co2Reduced: project.impact_metrics?.co2_reduced || 5000
}
};
}) || [];
return {
id: portfolio.id,
name: portfolio.name,
description: portfolio.description || '',
projects: portfolio.projects || [],
projects: projects,
pricePerTon,
currency: 'USD'
};
@ -193,12 +213,14 @@ export async function createOffsetOrder(
console.log('[wrenClient] Portfolio data keys:', Object.keys(order.portfolio).join(', '));
}
// Updated field name based on tutorial sample response
let pricePerTon = 15.63;
if (order.portfolio?.costPerTon !== undefined) {
pricePerTon = typeof order.portfolio.costPerTon === 'number' ? order.portfolio.costPerTon : parseFloat(order.portfolio.costPerTon) || 15.63;
// Get price from API response which uses cost_per_ton
let pricePerTon = 18;
if (order.portfolio?.cost_per_ton !== undefined) {
pricePerTon = typeof order.portfolio.cost_per_ton === 'number' ? order.portfolio.cost_per_ton : parseFloat(order.portfolio.cost_per_ton) || 18;
} else if (order.portfolio?.costPerTon !== undefined) {
pricePerTon = typeof order.portfolio.costPerTon === 'number' ? order.portfolio.costPerTon : parseFloat(order.portfolio.costPerTon) || 18;
} else if (order.portfolio?.pricePerTon !== undefined) {
pricePerTon = typeof order.portfolio.pricePerTon === 'number' ? order.portfolio.pricePerTon : parseFloat(order.portfolio.pricePerTon) || 15.63;
pricePerTon = typeof order.portfolio.pricePerTon === 'number' ? order.portfolio.pricePerTon : parseFloat(order.portfolio.pricePerTon) || 18;
}
// Create a safe method to extract properties with fallbacks

View File

@ -114,17 +114,17 @@ export function OffsetOrder({ tons, monetaryAmount, onBack, calculatorType }: Pr
const renderPortfolioPrice = (portfolio: Portfolio) => {
try {
// Get the price per ton from the portfolio
const pricePerTon = portfolio.pricePerTon || 200; // Default to 200 if not set
const pricePerTon = portfolio.pricePerTon || 18; // Default based on Wren Climate Fund average
const targetCurrency = getCurrencyByCode(currency);
return formatCurrency(pricePerTon, targetCurrency);
} catch (err) {
console.error('Error formatting portfolio price:', err);
return formatCurrency(200, currencies.USD); // Default fallback
return formatCurrency(18, currencies.USD); // Updated fallback
}
};
// Calculate offset cost using the portfolio price
const offsetCost = monetaryAmount || (portfolio ? tons * (portfolio.pricePerTon || 200) : 0);
const offsetCost = monetaryAmount || (portfolio ? tons * (portfolio.pricePerTon || 18) : 0);
return (
<div className="bg-white rounded-lg shadow-xl p-8 max-w-4xl w-full">