feat: Add prop-types dependency, implement currency context, and enhance pricing display in components
- Added `prop-types` for better prop validation in components. - Introduced `CurrencyProvider` to manage currency context and preload exchange rates. - Updated pricing logic in various components to support new price structure and display currency. - Refactored BOMSummary, MotorStep, and PowerSupplyStep to utilize new pricing methods and improve user experience. - Enhanced export utilities to format prices correctly in markdown and Excel outputs. - Updated hardware and component data structures to include detailed pricing information.
This commit is contained in:
39
website/src/hooks/usePriceFormat.js
Normal file
39
website/src/hooks/usePriceFormat.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useCurrency } from '../contexts/CurrencyContext';
|
||||
import { formatPrice as formatPriceUtil } from '../utils/priceFormat';
|
||||
import { convertPrice } from '../utils/currencyService';
|
||||
|
||||
/**
|
||||
* Hook to format prices using the selected currency from context with conversion
|
||||
*/
|
||||
export function usePriceFormat() {
|
||||
const { currency, exchangeRates } = useCurrency();
|
||||
const [convertedPriceCache, setConvertedPriceCache] = useState(new Map());
|
||||
|
||||
const formatPrice = async (price, preferredCurrency = null) => {
|
||||
const displayCurrency = preferredCurrency || currency;
|
||||
|
||||
// Convert price to target currency if needed
|
||||
if (exchangeRates && price) {
|
||||
try {
|
||||
const converted = await convertPrice(price, displayCurrency, exchangeRates);
|
||||
return formatPriceUtil(converted, displayCurrency);
|
||||
} catch (error) {
|
||||
console.warn('Failed to convert price, using original:', error);
|
||||
return formatPriceUtil(price, displayCurrency);
|
||||
}
|
||||
}
|
||||
|
||||
return formatPriceUtil(price, displayCurrency);
|
||||
};
|
||||
|
||||
// Synchronous version for use in render (uses cache or returns promise)
|
||||
const formatPriceSync = (price, preferredCurrency = null) => {
|
||||
const displayCurrency = preferredCurrency || currency;
|
||||
// For now, return the formatted price without conversion in sync mode
|
||||
// Conversion will happen in components that can handle async
|
||||
return formatPriceUtil(price, displayCurrency);
|
||||
};
|
||||
|
||||
return { formatPrice, formatPriceSync, currency, exchangeRates };
|
||||
}
|
||||
Reference in New Issue
Block a user