From 742d807b59855ca6f9ace0da90192f8e3eed4f2c Mon Sep 17 00:00:00 2001 From: Factiven Date: Thu, 20 Jul 2023 17:54:58 +0700 Subject: Update v3.6.7 > Fixed Schedule --- components/home/schedule.js | 167 ++++++++++++++++++-------------------------- 1 file changed, 69 insertions(+), 98 deletions(-) (limited to 'components') diff --git a/components/home/schedule.js b/components/home/schedule.js index f5aa44f..4a85143 100644 --- a/components/home/schedule.js +++ b/components/home/schedule.js @@ -12,50 +12,30 @@ export default function Schedule({ data, scheduleData, time }) { "Schedule"; currentDay = currentDay.replace("Schedule", ""); - const [activeSection, setActiveSection] = useState(currentDay); - - const scrollRef = useRef(null); + const [currentPage, setCurrentPage] = useState(0); + const [days, setDay] = useState(); useEffect(() => { if (scheduleData) { - const index = Object?.keys(scheduleData).indexOf( - activeSection + "Schedule" - ); - if (scrollRef.current) { - scrollRef.current.scrollLeft = scrollRef.current.clientWidth * index; - } + const days = Object.keys(scheduleData); + setDay(days); } - }, [activeSection, scheduleData]); + }, [scheduleData]); - const handleScroll = (e) => { - const { scrollLeft, clientWidth } = e.target; - const index = Math.floor(scrollLeft / clientWidth); - let day = Object?.keys(scheduleData)[index]; - day = day.replace("Schedule", ""); - setActiveSection(day); + const handleNextPage = () => { + setCurrentPage((prevPage) => (prevPage + 1) % days.length); }; - // buttons to scroll horizontally - const scrollLeft = () => { - if (scrollRef.current.scrollLeft === 0) { - scrollRef.current.scrollLeft = scrollRef.current.scrollWidth; - } else { - scrollRef.current.scrollLeft -= scrollRef.current.offsetWidth; - } + const handlePreviousPage = () => { + setCurrentPage((prevPage) => (prevPage - 1 + days.length) % days.length); }; - const scrollRight = () => { - const difference = - scrollRef.current.scrollWidth - - scrollRef.current.offsetWidth - - scrollRef.current.scrollLeft; - if (difference < 5) { - // adjust the threshold as needed - scrollRef.current.scrollLeft = 0; - } else { - scrollRef.current.scrollLeft += scrollRef.current.offsetWidth; - } - }; + useEffect(() => { + const todayIndex = days?.findIndex((day) => + day.toLowerCase().includes(currentDay) + ); + setCurrentPage(todayIndex >= 0 ? todayIndex : 0); + }, [currentDay, days]); return (
@@ -126,84 +106,75 @@ export default function Schedule({ data, scheduleData, time }) {
- {scheduleData && ( + + {scheduleData && days && (
- {Object.entries(scheduleData).map(([section, data], index) => { - const uniqueArray = data.reduce((accumulator, current) => { - if (!accumulator.find((item) => item.id === current.id)) { - accumulator.push(current); - } - return accumulator; - }, []); - - return ( -
-
- {uniqueArray.map((i, index) => { - const currentTime = Date.now(); - const hasAired = i.airingAt < currentTime; +
+ {scheduleData[days[currentPage]] + .filter((show, index, self) => { + return index === self.findIndex((s) => s.id === show.id); + }) + .map((i, index) => { + const currentTime = Date.now(); + const hasAired = i.airingAt < currentTime; - return ( - -
- coverSchedule -
-
-
-

- {i.title.romaji} -

-

- {convertUnixToTime(i.airingAt)} - Episode{" "} - {i.airingEpisode} -

-
-
- -
-
- - ); - })} -
-
- ); - })} + return ( + +
+ {i.coverImage && ( + {`${i.title.romaji} + )} +
+
+
+

+ {i.title.romaji} +

+

+ {convertUnixToTime(i.airingAt)} - Episode{" "} + {i.airingEpisode} +

+
+
+ +
+
+ + ); + })} +
-
{activeSection}
+
+ {days[currentPage]?.replace("Schedule", "")} +
-- cgit v1.2.3