Skip to content

Commit

Permalink
Add shipping cost indication
Browse files Browse the repository at this point in the history
  • Loading branch information
keybraker committed Jan 15, 2024
1 parent 5a36fe8 commit 6ae0926
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 49 deletions.
59 changes: 19 additions & 40 deletions src/decorators/CorrectFinalPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,39 @@ import { State } from "../types/State";

export class CorrectFinalPrice {
private state: State;
private btsPrice: number | undefined = undefined;

constructor(state: State) {
this.state = state;
this.btsPrice = this.fetchBTSPrice();
}

private fetchBTSPrice() {
const priceElement = document.querySelector(".price");
return priceElement ? this.priceElementToNumber(priceElement) : undefined;
public async start() {
this.finalPriceFixer();
this.shippingCostFixer();
}

private priceElementToNumber(element: Element) {
let priceValue = "";

const leftPart = element.querySelector("span.comma");
if (!leftPart?.previousSibling) {
return undefined;
}

const integerPart = leftPart.previousSibling.textContent;
priceValue = `${priceValue}${integerPart}`;
private finalPriceFixer() {
const finalPriceLabel = document.querySelector("label.toggle-switch-label");

const rightPart = element.querySelector("span.comma + span");
if (!rightPart) {
return undefined;
if (finalPriceLabel) {
finalPriceLabel.textContent += this.state.language === Language.ENGLISH ? " (with cash on delivery)" : " (με αντικαταβολή)";
}

const decimalPart = rightPart.textContent;
priceValue = `${priceValue}.${decimalPart}`;

return parseFloat(priceValue);
}

public async start() {
const BTS_FREE_SHIPPING_THRESHOLD = 20;

if(this.btsPrice && this.btsPrice < BTS_FREE_SHIPPING_THRESHOLD) {
const priceDiv = document.querySelector("div.price");

if (priceDiv) {
const deliverCostDiv = document.createElement("div");

deliverCostDiv.classList.add("deliver-cost");
deliverCostDiv.textContent = this.state.language === Language.ENGLISH ? " (+3,00€ delivery cost)" : " (+3,00€ μεταφορικά)";

priceDiv.appendChild(deliverCostDiv);
}
private shippingCostFixer() {
const priceElement = document.querySelector(".price");
if(!priceElement) {
return;
}

const finalPriceLabel = document.querySelector("label.toggle-switch-label");
const articleEm = document.querySelector("article.offering-card");
const shippingCostEm = articleEm?.querySelector("em");
if(!shippingCostEm) {
return;
}

if (finalPriceLabel) {
finalPriceLabel.textContent += this.state.language === Language.ENGLISH ? " (with cash on delivery)" : " (με αντικαταβολή)";
if (!shippingCostEm.parentElement) {
return;
}
priceElement.appendChild(shippingCostEm.parentElement.cloneNode(true));
}
}
22 changes: 13 additions & 9 deletions src/decorators/PriceCheckerIndicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface LowestPriceData {
export class PriceCheckerIndicator {
private state: State;
private btsPrice: number | undefined = undefined;
private btsDeliveryCost: number | undefined = undefined;
private btsShippingCost: number | undefined = undefined;
private lowestPriceData: LowestPriceData | undefined = undefined;

constructor(state: State) {
Expand All @@ -23,14 +23,18 @@ export class PriceCheckerIndicator {
public async start() {
const offeringCard = document.querySelector("article.offering-card");

if (offeringCard) {
this.lowestPriceData = await marketDataReceiver();
if (this.lowestPriceData) {
this.btsPrice = buyThroughSkroutzRetriever();
this.btsDeliveryCost = buyThroughSkroutzDeliveryCostRetriever();
this.insertPriceIndication(offeringCard);
}
if (!offeringCard) {
return;
}

this.lowestPriceData = await marketDataReceiver();
if (!this.lowestPriceData) {
return;
}

this.btsPrice = buyThroughSkroutzRetriever();
this.btsShippingCost = buyThroughSkroutzDeliveryCostRetriever();
this.insertPriceIndication(offeringCard);
}

private insertPriceIndication(element: Element): void {
Expand All @@ -42,7 +46,7 @@ export class PriceCheckerIndicator {
const priceIndication = document.createElement("div");
const colFlex = document.createElement("div");

const deliveryCost = this.btsDeliveryCost ?? 0;
const deliveryCost = this.btsShippingCost ?? 0;
let isLowestPrice = false;
if (!!this.btsPrice && !!this.lowestPriceData) {
isLowestPrice = this.btsPrice + deliveryCost <= this.lowestPriceData.unformatted;
Expand Down

0 comments on commit 6ae0926

Please sign in to comment.