carget
Version:
optimization js code for carget website
162 lines (141 loc) • 5.53 kB
JavaScript
// API курсы валют
async function fetchCurrencyRates() {
try {
const storedData = localStorage.getItem('currencyRates')
if (!storedData) {
const responseCNY = await fetch('https://open.er-api.com/v6/latest/CNY')
const dataCNY = await responseCNY.json()
const responseUSD = await fetch('https://open.er-api.com/v6/latest/USD')
const dataUSD = await responseUSD.json()
const rates = { CNY: dataCNY.rates.RUB, USD: dataUSD.rates.RUB }
await saveCurrencyRatesToLocalStorage(rates)
} else {
const currencyRates = JSON.parse(storedData)
const lastUpdated = new Date(currencyRates.lastUpdated)
const currentDate = new Date()
const currentDateString = `${currentDate.getDate()}-${
currentDate.getMonth() + 1
}-${currentDate.getFullYear()}`
const lastUpdatedString = `${lastUpdated.getDate()}-${
lastUpdated.getMonth() + 1
}-${lastUpdated.getFullYear()}`
if (currentDateString !== lastUpdatedString) {
const responseCNY = await fetch('https://open.er-api.com/v6/latest/CNY')
const dataCNY = await responseCNY.json()
const responseUSD = await fetch('https://open.er-api.com/v6/latest/USD')
const dataUSD = await responseUSD.json()
const rates = { CNY: dataCNY.rates.RUB, USD: dataUSD.rates.RUB }
await saveCurrencyRatesToLocalStorage(rates)
}
}
} catch (error) {
console.error('Ошибка при загрузке курсов валют:', error)
throw error // Обработка ошибки
}
}
async function saveCurrencyRatesToLocalStorage(data) {
const currentDate = new Date().toISOString() // Получить текущую дату и время
const currencyRates = { rates: data, lastUpdated: currentDate }
await localStorage.setItem('currencyRates', JSON.stringify(currencyRates))
return data
}
function convertCarState(carState) {
const storedData = localStorage.getItem('currencyRates')
const currencyRates = JSON.parse(storedData)
const usdToRubExchangeRate = currencyRates.rates.USD
const cnyToRubExchangeRate = currencyRates.rates.CNY
console.log('carrensy', usdToRubExchangeRate, cnyToRubExchangeRate)
// Конвертация МОДЕЛЕЙ в рубли
const priceInRub = {}
modelNames.forEach((modelName) => {
const modelPrice = carState.models.find((model) => model[modelName])
const modelPriceCNY = modelPrice ? modelPrice[modelName].price : 0
priceInRub[modelName] = modelPriceCNY * cnyToRubExchangeRate
})
// Обновление объекта состояния с новыми значениями в рублях
const updatedCarState = {
...carState,
models: carState.models.map((model) => {
const modelName = Object.keys(model)[0]
const priceInRub = model[modelName].price * cnyToRubExchangeRate
return { [modelName]: priceInRub }
}),
delivery: {
...carState.delivery,
fromTurgartToBishkek:
carState.delivery.fromTurgartToBishkek * usdToRubExchangeRate,
customs: carState.delivery.customs * usdToRubExchangeRate,
otherExpenses: carState.delivery.otherExpenses * usdToRubExchangeRate,
fromBishkekToRussia:
carState.delivery.fromBishkekToRussia * usdToRubExchangeRate,
},
labFees: carState.labFees,
recyclingFee: carState.recyclingFee,
carLocalization: carState.carLocalization,
}
return updatedCarState
}
function sumCarModelsPrices(carState) {
// Получение всех цен из объекта состояния БЕЗ ДОП ОПЦИЙ
const {
models,
delivery: {
fromTurgartToBishkek,
customs,
otherExpenses,
fromBishkekToRussia,
},
labFees,
recyclingFee,
carLocalization,
marga,
} = carState
// Суммирование всех цен
const totalPrices = modelNames.map((modelName) => {
const modelPriceCNY =
models.find((model) => model[modelName])?.[modelName] || 0
const total =
modelPriceCNY +
fromTurgartToBishkek +
customs +
otherExpenses +
fromBishkekToRussia +
labFees +
recyclingFee +
carLocalization
const totalWithMarga = total + total * (marga / 100)
return roundNumberToNChars(totalWithMarga, 4)
})
return totalPrices
}
function sumCarPrices(carState, myPriceModels) {
// Получение всех цен из объекта состояния БЕЗ ДОП ОПЦИЙ
const {
options: { color, wheels, interiorColor, runningBoards },
} = carState
const selectedModel = carState.model[1]
const modelIndex = modelNames.indexOf(selectedModel)
// Добавим консольные логи для отслеживания
// Проверим, что modelIndex не равен -1
if (modelIndex === -1) {
console.error('Выбранная модель не найдена в modelNames.')
return // Возвращаем undefined в случае ошибки
}
const modelPrice = myPriceModels[modelIndex]
let total =
modelPrice +
color[0] +
wheels[0] +
interiorColor[0] /* - saleAll если нужно делать вычет */
if (carState.options.runningBoards[1]) {
const options = carState.options.runningBoards[0]
options.forEach((option) => {
if (option.show) {
total += option.price
}
})
}
totalPrice = total
return total
}
export { convertCarState, sumCarModelsPrices, sumCarPrices }