From a501906993b93bbd1355b2b21d9e76d27c6bf68d Mon Sep 17 00:00:00 2001
From: Rosyid Haryadi <rosyid_haryadi@protonmail.com>
Date: Mon, 2 Oct 2023 14:32:49 +0700
Subject: barebones libur api fetch

---
 src/Result.jsx | 32 +++++++++++++++++++++++++++-----
 1 file changed, 27 insertions(+), 5 deletions(-)

(limited to 'src/Result.jsx')

diff --git a/src/Result.jsx b/src/Result.jsx
index dbb5507..a2da055 100644
--- a/src/Result.jsx
+++ b/src/Result.jsx
@@ -1,14 +1,26 @@
 import {useEffect, useState} from "react";
 import {NumericFormat} from "react-number-format";
 import {Container, Typography} from "@mui/material";
+import dayjs from "dayjs";
 
 function Result({listEntry, baseSalary}) {
     const [totalOvertimePay, setTotalOvertimePay] = useState(0);
+    const [holidayData, setHolidayData] = useState([]);
 
     useEffect(() => {
         setTotalOvertimePay(getOvertimePayTotal(listEntry, baseSalary));
     }, [listEntry, baseSalary]);
 
+    useEffect(() => {
+        fetchApi(dayjs().year()).then((result) => {
+            setHolidayData((currentData) => [...currentData, {year: dayjs().year(), data: result}]);
+        });
+    }, []);
+
+    useEffect(() => {
+        console.log(holidayData);
+    }, [holidayData]);
+
     if (totalOvertimePay < 0) {
         return (
                 <Container>
@@ -30,7 +42,7 @@ function Result({listEntry, baseSalary}) {
                 return (
                     <div key={entry.id}>
                         <Typography>
-                            {entry.date.format('DD MMMM YYYY')}{weekendDay(entry.date) ? ` (${weekendDay(entry.date)})` : ''}: Rp<NumericFormat displayType="text" decimalScale={0} thousandSeparator={true} value={calculatePerDay(entry, baseSalary)} />
+                            {entry.date.format('DD MMMM YYYY')}{isHoliday(entry.date) ? ` (${isHoliday(entry.date)})` : ''}: Rp<NumericFormat displayType="text" decimalScale={0} thousandSeparator={true} value={calculatePerDay(entry, baseSalary)} />
                         </Typography>
                     </div>
                 )
@@ -39,6 +51,14 @@ function Result({listEntry, baseSalary}) {
     )
 }
 
+function fetchApi(year) {
+    // kemungkinan nanti ada pengecekan taun udah pernah difetch / belum
+    const baseUrl = 'https://dayoffapi.vercel.app/api';
+    return fetch(`${baseUrl}/?year=${year}`)
+        .then((response) => response.json())
+        .then((data) => data);
+}
+
 function segmentTime(duration, segments) {
     const segmentSum = segments.reduce((partialSum, a) => partialSum + a, 0);
     const segmentsFilled = [...segments, 24 - segmentSum];
@@ -56,16 +76,18 @@ function segmentTime(duration, segments) {
     return segmentedHours;
 }
 
-function weekendDay(date) {
+function isHoliday(date, holidayData) {
     if (date.day() === 0) {
         return 'Minggu';
     } else if (date.day() === 6) {
         return 'Sabtu';
     }
+
+
     return '';
 }
 
-function calculatePerDay(entry, baseSalary) {
+function calculatePerDay(entry, baseSalary, holidayData) {
     const hourlyPay = baseSalary / 173;
     const overtimeDuration = entry.finish.diff(entry.start, 'hour', true);
     let multiplier = 0;
@@ -83,9 +105,9 @@ function calculatePerDay(entry, baseSalary) {
         ]
     }
 
-    const segmentPattern = weekendDay(entry.date) ? [8, 1, 15] : [1];
+    const segmentPattern = isHoliday(entry.date) ? [8, 1, 15] : [1];
     const segmentedDuration = segmentTime(overtimeDuration, segmentPattern);
-    const usedMap = weekendDay(entry.date) ? multiplierMap.holidays : multiplierMap.workDays;
+    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];
-- 
cgit v1.2.3-70-g09d2

