diff --git a/src/pages/Reports.js b/src/pages/Reports.js index bed2ff1..df6dc74 100644 --- a/src/pages/Reports.js +++ b/src/pages/Reports.js @@ -112,7 +112,8 @@ const RoundedRectangle = ({ const App = ({ forCafe = true, cafeId = -1, handleClose, otherCafes, coupons, setModal, user }) => { - const [modalStatus, setModalStatus] = useState(null); + const [couponList, setCouponList] = useState(coupons); + const [modalStatus, setModalStatus] = useState(null); const [selectedCafeId, setSelectedCafeId] = useState(cafeId); const [analytics, setAnalytics] = useState({}); const [loading, setLoading] = useState(true); @@ -128,10 +129,10 @@ const App = ({ forCafe = true, cafeId = -1, setLoading(true); // Fetch the analytics data with the selected filter let analyticsData = null; - if(user.roleId == 1) + if (user.roleId == 1) analyticsData = (selectedCafeId !== '' && selectedCafeId !== 0) - ? await getReports(selectedCafeId, filter) - : await getAnalytics(filter); + ? await getReports(selectedCafeId, filter) + : await getAnalytics(filter); else analyticsData = await getAnalytics(filter, selectedCafeId); console.log(analyticsData); @@ -311,12 +312,12 @@ const App = ({ forCafe = true, cafeId = -1, let updatedFullTexts; if (otherCafes.length === 0) { - updatedFullTexts = [[user.roleId == 0 ? "Buat Voucher":"Buat bisnis", 0]]; + updatedFullTexts = [[user.roleId == 0 ? "Buat Voucher" : "Buat bisnis", 0]]; setSelectedCafeId(-1); } else if (otherCafes.length === 1) { updatedFullTexts = [ [otherCafes[0].name || otherCafes[0].username, otherCafes[0].cafeId || otherCafes[0].userId], - [user.roleId == 0 ? "Buat Voucher":"Buat bisnis", -1] + [user.roleId == 0 ? "Buat Voucher" : "Buat bisnis", -1] ]; setSelectedCafeId(otherCafes[0].cafeId); // Get the cafeId (second part of the pair) @@ -324,7 +325,7 @@ const App = ({ forCafe = true, cafeId = -1, updatedFullTexts = [ ["semua", 0], // First entry is "semua" ...otherCafes.map(item => [item.name || item.username, item.cafeId || item.userId]), // Map over cafes to get name and cafeId pairs - [user.roleId == 0 ? "Buat Voucher":"Tambah Bisnis +", -1] // Add the "+" entry + [user.roleId == 0 ? "Buat Voucher" : "Tambah Bisnis +", -1] // Add the "+" entry ]; setSelectedCafeId(0); @@ -386,11 +387,33 @@ const App = ({ forCafe = true, cafeId = -1, // Get the cafeId from the selected text based on the index const selectedText = texts[index]; // Get the selected name from the texts array + let unSelectedText; + if (texts[index - 1] != undefined) unSelectedText = texts[index - 1]; // Get the selected name from the texts array const selectedItem = fullTexts.find(item => item[0] === selectedText); // Find the corresponding full item + const unSelectedItem = fullTexts.find(item => item[0] === unSelectedText); // Find the corresponding full item if (selectedItem) { setSelectedCafeId(selectedItem[1]); // Get the cafeId (second part of the pair) } - if(selectedItem[1] == -1 && user.roleId == 0) setModal('create_coupon', {}, () => {setSelectedSwitch(0);setSelectedCafeId(0)}); + let nextSelectedId = selectedItem[1] + if (selectedItem[1] == -1 && user.roleId == 0) setModal('create_coupon', {}, () => { setSelectedSwitch(1); setSelectedCafeId(unSelectedItem[1]); nextSelectedId=unSelectedItem[1] }); + console.log(analytics) + if (user && user.roleId === 0 && analytics) { + // Filter the analytics items based on userId + if(selectedItem[1] != 0 && selectedItem[1] != -1){ + const filteredData = analytics.items.filter( + (data) => data.userId === nextSelectedId + ); + + // Extract coupons from the filtered data + const couponsToAdd = filteredData.flatMap((data) => data.coupons); + + // Log the coupons to be added + console.log(couponsToAdd); + // Assuming setCouponList is a function that updates the coupon list + setCouponList(couponsToAdd || []); + } + else setCouponList([]) + } setResetKey((prevKey) => prevKey + 1); // Increase the key to force re-render }; @@ -409,10 +432,10 @@ const App = ({ forCafe = true, cafeId = -1, if (!create) { setModalStatus('failed'); } - + // Add a 2-second delay before proceeding await delay(2000); - + handleClose(); setModalStatus(null); // Reset status window.location.reload(); // Reload the page @@ -423,7 +446,7 @@ const App = ({ forCafe = true, cafeId = -1, if (!create) { setModalStatus('failed'); } - + // Add a 2-second delay before proceeding await delay(2000); handleClose(); @@ -431,7 +454,7 @@ const App = ({ forCafe = true, cafeId = -1, window.location.reload(); // Reload the page } }; - + return (