From 4defd9e08d1acab49e30058dc48f532b29a455c4 Mon Sep 17 00:00:00 2001
From: Rosyid Haryadi <rosyid_haryadi@protonmail.com>
Date: Mon, 2 Oct 2023 16:04:51 +0700
Subject: refactor: more robust segment-multiplier relation

---
 src/Result.jsx | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)

(limited to 'src/Result.jsx')

diff --git a/src/Result.jsx b/src/Result.jsx
index c8a367e..0dc9b9a 100644
--- a/src/Result.jsx
+++ b/src/Result.jsx
@@ -60,13 +60,13 @@ function fetchApi(year) {
 }
 
 function segmentTime(duration, segments) {
-    const segmentSum = segments.reduce((partialSum, a) => partialSum + a, 0);
-    const segmentsFilled = [...segments, 24 - segmentSum];
+    // const segmentSum = segments.reduce((partialSum, a) => partialSum + a, 0);
+    // const segmentsFilled = [...segments, 24 - segmentSum];
     const segmentedHours = [];
 
-    for (const[index, segmentDuration] of segmentsFilled.entries()) {
-        if (index < segmentsFilled.length - 1) {
-            const hours = Math.min(duration, segmentDuration);
+    for (const[index, segmentDuration] of segments.entries()) {
+        if (index < segments.length - 1) {
+            const hours = Math.min(duration, segmentDuration.segment);
             segmentedHours.push(hours);
             duration -= hours;
         } else {
@@ -92,25 +92,23 @@ function calculatePerDay(entry, baseSalary, holidayData) {
     const overtimeDuration = entry.finish.diff(entry.start, 'hour', true);
     let multiplier = 0;
 
-    // TODO: find better way
-    const multiplierMap = {
+    const segmentMap = {
         workDays: [
-            1.5, // jam pertama
-            2
+            {segment: 1, multiplier: 1.5}, // jam pertama
+            {segment: 23, multiplier: 2}
         ],
         holidays: [
-            2, // 8 jam pertama
-            3, // jam ke 9
-            4
+            {segment: 8, multiplier: 2}, // 8 jam pertama
+            {segment: 1, multiplier: 3}, // jam ke 9
+            {segment: 15, multiplier: 4}
         ]
     }
 
-    const segmentPattern = isHoliday(entry.date) ? [8, 1, 15] : [1, 23];
+    const segmentPattern = isHoliday(entry.date) ? segmentMap.holidays : segmentMap.workDays;
     const segmentedDuration = segmentTime(overtimeDuration, segmentPattern);
-    const usedMap = isHoliday(entry.date) ? multiplierMap.holidays : multiplierMap.workDays;
     for (let i = 0; i < segmentedDuration.length; i++) {
         if (segmentedDuration[i])
-            multiplier += segmentedDuration[i] * usedMap[i];
+            multiplier += segmentedDuration[i] * segmentPattern[i].multiplier;
     }
 
     return multiplier * hourlyPay;
-- 
cgit v1.2.3-70-g09d2

