From 45d78a37e9faaefeef2ec7c62b963aa83319f034 Mon Sep 17 00:00:00 2001 From: moanos Date: Wed, 31 Jul 2024 15:50:46 +0200 Subject: [PATCH] fix: restructure code so vaildator gets initialized/calculator updates only after translation are loaded --- src/index.js | 121 +++++++++++++++++++++++++++------------------------ 1 file changed, 63 insertions(+), 58 deletions(-) diff --git a/src/index.js b/src/index.js index 5211b24..4436346 100644 --- a/src/index.js +++ b/src/index.js @@ -52,6 +52,7 @@ function bindLocaleSwitcher(initialValue) { await initI18next(); translatePageElements(); bindLocaleSwitcher(i18next.resolvedLanguage); + update(); })(); //////////////// @@ -74,15 +75,66 @@ const FAILED_MINIMUM_LENGTH_LONG_SIDE = "length_long_side"; const FAILED_MINIMUM_LENGTH_SHORT_SIDE= "length_short_side"; const FAILED_FLOOR_HEIGHT = "floor_height" -const FAIL_CRITERIA = { - [FAILED_BASE_AREA]: `Die Mindestgrundfläche des Käfigs muss ${MINIMUM_BASE_AREA}m² (also z.B. 100x50cm) betragen.`, - [FAILED_OVERALL_AREA]: "Die Gesamtfläche im Käfig ist zu klein.", - [FAILED_FALL_HEIGHT]: `Die mögliche Fallhöhe darf nicht mehr als ${(MAXIMUM_FALL_HEIGHT * 100).toFixed(0)}cm betragen.`, - [FAILED_FLOOR_HEIGHT]: `Der Mindestabstand zwischen Ebenen muss ${(MINIMUM_FLOOR_HEIGHT * 100).toFixed(0)}cm betragen.`, - [FAILED_NUM_RATS]: "Es müssen mindestens 3 Ratten zusammenleben, Paarhaltung ist nicht artgerecht.", - [FAILED_MINIMUM_LENGTH_LONG_SIDE]: `Die lange Seite des Käfig muss mindestens ${(MINIMUM_LENGTH_LONG_SIDE * 100).toFixed(0)}cm lang sein um Rennen zu ermöglichen.`, - [FAILED_MINIMUM_LENGTH_SHORT_SIDE]: `Die kurze Seite des Käfig muss mindestens ${(MINIMUM_LENGTH_SHORT_SIDE * 100).toFixed(0)}cm lang sein.`, -}; + +class Validator { + constructor() { + this.FAIL_CRITERIA = { + [FAILED_BASE_AREA]: i18next.t('failed-base-area'), + [FAILED_OVERALL_AREA]: i18next.t("failed-overall-area"), + [FAILED_FALL_HEIGHT]: i18next.t("failed-fall-height"), + [FAILED_FLOOR_HEIGHT]: `Der Mindestabstand zwischen Ebenen muss ${(MINIMUM_FLOOR_HEIGHT * 100).toFixed(0)}cm betragen.`, + [FAILED_NUM_RATS]: "Es müssen mindestens 3 Ratten zusammenleben, Paarhaltung ist nicht artgerecht.", + [FAILED_MINIMUM_LENGTH_LONG_SIDE]: `Die lange Seite des Käfig muss mindestens ${(MINIMUM_LENGTH_LONG_SIDE * 100).toFixed(0)}cm lang sein um Rennen zu ermöglichen.`, + [FAILED_MINIMUM_LENGTH_SHORT_SIDE]: `Die kurze Seite des Käfig muss mindestens ${(MINIMUM_LENGTH_SHORT_SIDE * 100).toFixed(0)}cm lang sein.`, + }; + } + + overallAreaNeeded(numOfRats) { + if (numOfRats < 3 || numOfRats > 15) { + throw new Error("This formula works only from 3 to 15 rats"); + } + return MINIMUM_AREA_THREE_RATS + (numOfRats - 3) * AREA_PER_ADDITIONAL_RAT; + } + + cageCheck(dimensions, numRats, numFullFloors) { + let failedCriteria = {}; + + if (numRats < 2 || numRats > 15) { + failedCriteria[FAILED_NUM_RATS] = this.FAIL_CRITERIA[FAILED_NUM_RATS]; + } + + const baseArea = dimensions.depth * dimensions.width; + if (baseArea < MINIMUM_BASE_AREA) { + failedCriteria[FAILED_BASE_AREA] = this.FAIL_CRITERIA[FAILED_BASE_AREA]; + } + + const areaNeeded = this.overallAreaNeeded(numRats); + if (baseArea * numFullFloors < areaNeeded) { + failedCriteria[FAILED_OVERALL_AREA] = this.FAIL_CRITERIA[FAILED_OVERALL_AREA]; + } + + if (dimensions.height / numFullFloors > MAXIMUM_FALL_HEIGHT) { + failedCriteria[FAILED_FALL_HEIGHT] = this.FAIL_CRITERIA[FAILED_FALL_HEIGHT]; + } + + if (dimensions.width < MINIMUM_LENGTH_LONG_SIDE && dimensions.depth < MINIMUM_LENGTH_LONG_SIDE) { + failedCriteria[FAILED_MINIMUM_LENGTH_LONG_SIDE] = this.FAIL_CRITERIA[FAILED_MINIMUM_LENGTH_LONG_SIDE]; + } + + if (dimensions.width < MINIMUM_LENGTH_SHORT_SIDE || dimensions.depth < MINIMUM_LENGTH_SHORT_SIDE) { + failedCriteria[FAILED_MINIMUM_LENGTH_SHORT_SIDE] = this.FAIL_CRITERIA[FAILED_MINIMUM_LENGTH_SHORT_SIDE]; + } + + if (dimensions.height / numFullFloors < MINIMUM_FLOOR_HEIGHT) { + failedCriteria[FAILED_FLOOR_HEIGHT] = this.FAIL_CRITERIA[FAILED_FLOOR_HEIGHT]; + } + + return failedCriteria; + } + + + +} class Dimensions { constructor(width, depth, height) { @@ -101,48 +153,6 @@ class Dimensions { } } -function overallAreaNeeded(numOfRats) { - if (numOfRats < 3 || numOfRats > 15) { - throw new Error("This formula works only from 3 to 15 rats"); - } - return MINIMUM_AREA_THREE_RATS + (numOfRats - 3) * AREA_PER_ADDITIONAL_RAT; -} - -function cageCheck(dimensions, numRats, numFullFloors) { - let failedCriteria = {}; - - if (numRats < 2 || numRats > 15) { - failedCriteria[FAILED_NUM_RATS] = FAIL_CRITERIA[FAILED_NUM_RATS]; - } - - const baseArea = dimensions.depth * dimensions.width; - if (baseArea < MINIMUM_BASE_AREA) { - failedCriteria[FAILED_BASE_AREA] = FAIL_CRITERIA[FAILED_BASE_AREA]; - } - - const areaNeeded = overallAreaNeeded(numRats); - if (baseArea * numFullFloors < areaNeeded) { - failedCriteria[FAILED_OVERALL_AREA] = FAIL_CRITERIA[FAILED_OVERALL_AREA]; - } - - if (dimensions.height / numFullFloors > MAXIMUM_FALL_HEIGHT) { - failedCriteria[FAILED_FALL_HEIGHT] = FAIL_CRITERIA[FAILED_FALL_HEIGHT]; - } - - if (dimensions.width < MINIMUM_LENGTH_LONG_SIDE && dimensions.depth < MINIMUM_LENGTH_LONG_SIDE) { - failedCriteria[FAILED_MINIMUM_LENGTH_LONG_SIDE] = FAIL_CRITERIA[FAILED_MINIMUM_LENGTH_LONG_SIDE]; - } - - if (dimensions.width < MINIMUM_LENGTH_SHORT_SIDE || dimensions.depth < MINIMUM_LENGTH_SHORT_SIDE) { - failedCriteria[FAILED_MINIMUM_LENGTH_SHORT_SIDE] = FAIL_CRITERIA[FAILED_MINIMUM_LENGTH_SHORT_SIDE]; - } - - if (dimensions.height / numFullFloors < MINIMUM_FLOOR_HEIGHT) { - failedCriteria[FAILED_FLOOR_HEIGHT] = FAIL_CRITERIA[FAILED_FLOOR_HEIGHT]; - } - - return failedCriteria; -} ////////////////////////// // DOCUMENT INTERACTION // @@ -192,7 +202,6 @@ function updateCage(event) { selectedCage.checked = true; const cageName = selectedCage.id; - console.log(cageName); var dim = getCageDimensions(cageName); inputWidth.value = dim.width; inputDepth.value = dim.depth; @@ -223,7 +232,6 @@ ratSlider.oninput = function () { var fullFloorNum = document.getElementById("numFullFloors"); function getCageDimensions(cageName) { - console.log(cageName); if (cageName == "SavicSuiteRoyaleXL") { return new Dimensions(115, 67.5, 153); } @@ -236,7 +244,6 @@ function getCageDimensions(cageName) { } function getResultFromChecks(checks) { - console.log(checks.length) if (Object.keys(checks).length > 0) { const ul = document.createElement('ul'); for (const key in checks) { @@ -263,8 +270,8 @@ function update() { const depth = inputDepth.value const height = inputHeight.value const dimensions = new Dimensions(width / 100, depth / 100, height / 100); - const failed_checks = cageCheck(dimensions, ratSlider.value, fullFloorNum.value); - console.log(failed_checks); + const validator = new Validator(); + const failed_checks = validator.cageCheck(dimensions, ratSlider.value, fullFloorNum.value); let resultsDiv = document.getElementById("resultsDiv"); resultsDiv.innerHTML = `Ergebnis`; @@ -289,5 +296,3 @@ function increaseFloorNum() { input.value = value + 1; update(); } - -update();