From 9638cd2bfafee76effe75651e3f2cf5d363138e1 Mon Sep 17 00:00:00 2001
From: Rosyid Haryadi <rosyid_haryadi@protonmail.com>
Date: Mon, 2 Oct 2023 20:10:58 +0700
Subject: holiday name

---
 src/Result.jsx | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)

(limited to 'src/Result.jsx')

diff --git a/src/Result.jsx b/src/Result.jsx
index 0dc9b9a..3f89c90 100644
--- a/src/Result.jsx
+++ b/src/Result.jsx
@@ -5,22 +5,18 @@ import dayjs from "dayjs";
 
 function Result({listEntry, baseSalary}) {
     const [totalOvertimePay, setTotalOvertimePay] = useState(0);
-    const [holidayData, setHolidayData] = useState([]);
+    const [holidayData, setHolidayData] = useState(null);
 
     useEffect(() => {
-        setTotalOvertimePay(getOvertimePayTotal(listEntry, baseSalary));
-    }, [listEntry, baseSalary]);
+        setTotalOvertimePay(getOvertimePayTotal(listEntry, baseSalary, holidayData));
+    }, [listEntry, baseSalary, holidayData]);
 
     useEffect(() => {
         fetchApi(dayjs().year()).then((result) => {
-            setHolidayData((currentData) => [...currentData, {year: dayjs().year(), data: result}]);
-        });
+            setHolidayData(result);
+        })
     }, []);
 
-    useEffect(() => {
-        console.log(holidayData);
-    }, [holidayData]);
-
     if (totalOvertimePay < 0) {
         return (
                 <Container>
@@ -39,10 +35,11 @@ function Result({listEntry, baseSalary}) {
             </Typography>
             <Typography>Rincian</Typography>
             { listEntry.map((entry) => {
+                const holidayName = isHoliday(entry.date, holidayData);
                 return (
                     <div key={entry.id}>
                         <Typography>
-                            {entry.date.format('DD MMMM YYYY')}{isHoliday(entry.date) ? ` (${isHoliday(entry.date)})` : ''}: Rp<NumericFormat displayType="text" decimalScale={0} thousandSeparator={true} value={calculatePerDay(entry, baseSalary)} />
+                            {entry.date.format('DD MMMM YYYY')}{holidayName ? ` (${holidayName})` : ''}: Rp<NumericFormat displayType="text" decimalScale={0} thousandSeparator={true} value={calculatePerDay(entry, baseSalary)} />
                         </Typography>
                     </div>
                 )
@@ -60,8 +57,6 @@ function fetchApi(year) {
 }
 
 function segmentTime(duration, segments) {
-    // const segmentSum = segments.reduce((partialSum, a) => partialSum + a, 0);
-    // const segmentsFilled = [...segments, 24 - segmentSum];
     const segmentedHours = [];
 
     for (const[index, segmentDuration] of segments.entries()) {
@@ -82,9 +77,18 @@ function isHoliday(date, holidayData) {
     } else if (date.day() === 6) {
         return 'Sabtu';
     }
-
-
-    return '';
+    let holidayName = '';
+    if (holidayData) {
+        console.log(holidayData);
+        holidayData.every((day) => {
+            if (dayjs(day['tanggal'], 'YYYY-MM-D').isSame(date, 'day')) {
+                holidayName = day['keterangan'];
+                return false;
+            }
+            return true;
+        })
+    }
+    return holidayName;
 }
 
 function calculatePerDay(entry, baseSalary, holidayData) {
@@ -104,7 +108,7 @@ function calculatePerDay(entry, baseSalary, holidayData) {
         ]
     }
 
-    const segmentPattern = isHoliday(entry.date) ? segmentMap.holidays : segmentMap.workDays;
+    const segmentPattern = isHoliday(entry.date, holidayData) ? segmentMap.holidays : segmentMap.workDays;
     const segmentedDuration = segmentTime(overtimeDuration, segmentPattern);
     for (let i = 0; i < segmentedDuration.length; i++) {
         if (segmentedDuration[i])
@@ -114,10 +118,10 @@ function calculatePerDay(entry, baseSalary, holidayData) {
     return multiplier * hourlyPay;
 }
 
-function getOvertimePayTotal(listEntry, baseSalary) {
+function getOvertimePayTotal(listEntry, baseSalary, holidayData) {
     let total = 0;
     listEntry.forEach((entry) => {
-        total += calculatePerDay(entry, baseSalary)
+        total += calculatePerDay(entry, baseSalary, holidayData)
     });
     return total;
 }
-- 
cgit v1.2.3-70-g09d2

