Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit

Permalink
Merge pull request #11 from pedro-mealha/add-battery-price
Browse files Browse the repository at this point in the history
add battery price
  • Loading branch information
pedro-mealha authored May 3, 2024
2 parents 6d3ca23 + c6e9151 commit eb9dd08
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
1 change: 0 additions & 1 deletion assets/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ body {
}

#products-table {
width: 50% !important;
font-family: "Open Sans", sans-serif;
color: rgb(36 36 38);
margin-bottom: 0;
Expand Down
64 changes: 29 additions & 35 deletions assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { CSVToArray } from './parseCsv.js';

const PRODUCT_NAME_INDEX = 0;
const PRODUCT_ID_INDEX = 1;
const PRODUCT_DESCRIPTION_INDEX = 3;
const PRODUCT_PRICING_OPTION_INDEX = 7;
const PRODUCT_PRICE_INDEX = 8;

const EXCHANGE_PRICE = "Exchange Price";
const BATTERY_ONLY_PRICE = "Battery Only Price";

let oldCsvData;
let newCsvData;
Expand Down Expand Up @@ -76,63 +78,55 @@ window.onload = () => {
}

function buildData(oldCsvData, newCsvData) {
const products = [];

oldCsvData.shift(); // remove headers
newCsvData.shift(); // remove headers

for (const row of oldCsvData) {
if (row[PRODUCT_PRICING_OPTION_INDEX] === EXCHANGE_PRICE) {
continue;
}

const { id, name, index } = getProductBasicDetails(row);

if (!id) {
continue;
}
let products = parseCsvRows(oldCsvData);
products = parseCsvRows(newCsvData, products, false);

products[index] = { id, name, oldPrice: parseFloat(row[PRODUCT_PRICE_INDEX]) };
}
for (const product of Object.values(products)) {
const index = `${product.name} ${product.id} ${product.priceType}`;

for (const row of newCsvData) {
const { id, name, index } = getProductBasicDetails(row);
const newPrice = parseFloat(row[PRODUCT_PRICE_INDEX]);
product.oldPrice = product.oldPrice ?? null;
product.newPrice = product.newPrice ?? null;

if (!id || row[PRODUCT_PRICING_OPTION_INDEX] === EXCHANGE_PRICE) {
continue;
}
const priceSameOrInvalid = (!product.oldPrice && product.oldPrice !== null && !product.newPrice && product.newPrice !== null) || (product.oldPrice === product.newPrice);
const removed = product.newPrice === null;

if (!products[index]) {
products[index] = { id, name, oldPrice: null, newPrice };
continue;
}

if (products[index].oldPrice === newPrice) {
if (priceSameOrInvalid || removed) {
delete products[index];
continue;
}

products[index].newPrice = newPrice;
}

for (const product of Object.values(products)) {
const index = `${product.name} ${product.id}`;
return Object.values(products);
}

if (!product.newPrice || (!product.oldPrice && product.oldPrice !== null)) {
delete products[index];
function parseCsvRows(rows, products = [], oldCsv = true) {
const priceField = oldCsv ? 'oldPrice' : 'newPrice';

for (const row of rows) {
const { id, name, description, price, priceOption, index } = getProductBasicDetails(row);

if (!id || (priceOption !== EXCHANGE_PRICE && priceOption !== BATTERY_ONLY_PRICE)) {
continue;
}

products[index] = { id, name, description, priceType: priceOption, ...products[index] };
products[index][priceField] = price;
}

return Object.values(products);
return products;
}

function getProductBasicDetails(row) {
const id = row[PRODUCT_ID_INDEX];
const name = row[PRODUCT_NAME_INDEX];
const index = `${name} ${id}`;
const description = row[PRODUCT_DESCRIPTION_INDEX];
const priceOption = row[PRODUCT_PRICING_OPTION_INDEX];
const price = parseFloat(row[PRODUCT_PRICE_INDEX]);
const index = `${name} ${id} ${priceOption}`;

return { id, name, index };
return { id, name, description, priceOption, price, index };
}
};
8 changes: 5 additions & 3 deletions assets/js/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ export async function populateHtml(products) {
autoWidth: false,
data: products,
columns: [
{ data: 'id', title: 'ID', width: '20%' },
{ data: 'name', title: 'Name', width: '60%' },
{ data: 'id', title: 'ID', width: '10%' },
{ data: 'name', title: 'Name', width: '35%' },
{ data: 'description', title: 'Description', width: '45%' },
{ data: 'priceType', title: 'Pricing', width: '10%' },
{ data: 'oldPrice', title: 'Old Price', type: 'num-fmt', width: '10%' },
{ data: 'newPrice', title: 'New Price', type: 'num-fmt', width: '10%' }
{ data: 'newPrice', title: 'New Price', type: 'num-fmt', width: '10%' },
]
});

Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</div>

<div id="content">
<div class="container">
<div class="container-fluid">
<div class="row" style="margin-bottom: 5%;">
<div>
<div class="table-responsive">
Expand Down

0 comments on commit eb9dd08

Please sign in to comment.