Skip to content

Commit afa9edf

Browse files
committed
fix: [Carousel] Change behabior autoplay with video
+ When video is selected in first element of the carousel ==> then play it in repetitive mode + Carousel issue : when video is selected in first element, it's doubled ==> we havev to play from the two div which have the same id "video-0"
1 parent 9425acc commit afa9edf

File tree

1 file changed

+57
-7
lines changed

1 file changed

+57
-7
lines changed

src/components/CarouselDVC.tsx

+57-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ts-nocheck
22

3-
import { FC, useState, useRef } from "react";
3+
import { FC, useState, useRef, useEffect } from "react";
44
import { observer } from "mobx-react-lite";
55
import "react-responsive-carousel/lib/styles/carousel.min.css"; // requires a loader
66
import { Carousel } from 'react-responsive-carousel';
@@ -50,14 +50,18 @@ export const CarouselDVC: FC<CarouselDVCProps> = observer(({
5050
var videoListRefs = useRef([]);
5151
var dataMediaList: any[] | undefined = undefined;
5252
const [isAutoPlay, setIsAutoPlay] = useState(true);
53+
const [isPlayingVideo, setIsPlayingVideo] = useState(false);
5354

5455
// Parse media json list
5556
if (typeof data_media_list_json == 'string') {
5657
dataMediaList = JSON.parse(data_media_list_json);
5758
}
5859

5960
const handleVideoEnded = (videoElement) => {
60-
if (carouselRef.current != null) {
61+
// More than one element
62+
if (carouselRef.current != null && carouselRef.current.itemsRef.length > 1 && isPlayingVideo) {
63+
setIsPlayingVideo(false);
64+
6165
// Reset video after ended
6266
$(videoElement.target).get(0).pause();
6367
$(videoElement.target).get(0).currentTime = 0;
@@ -67,9 +71,42 @@ export const CarouselDVC: FC<CarouselDVCProps> = observer(({
6771
// Next slide
6872
$("#arrow-next").trigger("click");
6973

74+
} else if (carouselRef.current != null && carouselRef.current.itemsRef.length == 1 && isPlayingVideo) {
75+
// Exactly one element --> then repeat if it's a video
76+
77+
// Reset video after ended
78+
$(videoElement.target).get(0).pause();
79+
$(videoElement.target).get(0).currentTime = 0;
80+
81+
// Replay
82+
$(videoElement.target).get(0).play();
7083
}
7184
}
7285

86+
const handleLoadedData = (videoElement) => {
87+
if (carouselRef.current != null && videoElement.target.id === "video-0") {
88+
const videoList = $(".selected").find("video");
89+
90+
for (var video of videoList) {
91+
$(video).get(0).play();
92+
}
93+
setIsPlayingVideo(true);
94+
}
95+
}
96+
97+
useEffect(() => {
98+
if (carouselRef.current != null) {
99+
const videoList = $(".selected").find("video");
100+
101+
for (var video of videoList) {
102+
if (video.id === "video-0")
103+
$(video).get(0).play();
104+
}
105+
106+
setIsPlayingVideo(true);
107+
}
108+
}, carouselRef);
109+
73110
const defaultOnChange = (index: number, item: React.ReactNode) => {
74111
// Check if sibling element are video ?
75112

@@ -112,16 +149,27 @@ export const CarouselDVC: FC<CarouselDVCProps> = observer(({
112149
setIsAutoPlay(false);
113150
const currentVideo = videoListRefs.current.filter(video => video.id === itemVideo[0].props.id);
114151

115-
// Get video item and then play
116-
$(currentVideo).get(0).play();
152+
// TODO : If first element is a video ==>
153+
// Play the other video (because first element of a carousel has two videos and catch the event of only one)
154+
if (itemVideo[0].props.id === "video-0") {
155+
const videoList = $(".selected").find("video");
156+
157+
for (var video of videoList) {
158+
$(video).get(0).play();
159+
};
160+
} else {
161+
// Get video item and then play
162+
$(currentVideo).get(0).play();
163+
}
164+
165+
setIsPlayingVideo(true);
117166

118167
} else {
119168
setIsAutoPlay(true);
120169
}
121170

122171

123172
}
124-
125173
return (
126174
<Carousel
127175
ref={carouselRef}
@@ -165,8 +213,10 @@ export const CarouselDVC: FC<CarouselDVCProps> = observer(({
165213
className="diapo-container"
166214
src={media.url}
167215
type={media.mime_type}
168-
preload="none"
169-
muted="muted"/>
216+
preload="auto"
217+
muted="muted"
218+
onLoadeddata={handleLoadedData}
219+
/>
170220
}
171221
</div>);
172222
})

0 commit comments

Comments
 (0)