-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New #149] Add support of images for media content
- Loading branch information
Showing
2 changed files
with
105 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,93 @@ | ||
import React from "react"; | ||
import Constants from "../constants/Constants"; | ||
import LinkIcon from "./LinkIcon"; | ||
|
||
interface Props { | ||
question: object; | ||
} | ||
|
||
const YOUTUBE_URL = "https://www.youtube.com/"; | ||
const GOOGLE_DRIVE_URL = "https://drive.google.com/"; | ||
|
||
const MediaContent = ({ question }: Props) => { | ||
const renderMedia = () => { | ||
const isGoogleDriveImage = (mediaContentUrl: string) => { | ||
return ( | ||
mediaContentUrl.includes("/file/d/") && | ||
mediaContentUrl.includes(GOOGLE_DRIVE_URL) | ||
); | ||
}; | ||
|
||
const isYoutubeVideo = (mediaContentUrl: string) => { | ||
return ( | ||
mediaContentUrl.includes("watch?v=") && | ||
mediaContentUrl.includes(YOUTUBE_URL) | ||
); | ||
}; | ||
|
||
const getMediaId = (mediaContentUrl: string) => { | ||
if (isGoogleDriveImage(mediaContentUrl)) { | ||
return mediaContentUrl.substring( | ||
mediaContentUrl.indexOf("/file/d/") + 8, | ||
mediaContentUrl.lastIndexOf("/") | ||
); | ||
} | ||
if (isYoutubeVideo(mediaContentUrl)) { | ||
return mediaContentUrl.substring(mediaContentUrl.indexOf("watch?v=") + 8); | ||
} | ||
}; | ||
|
||
const getEmbedLink = (mediaContentUrl: string) => { | ||
if (isGoogleDriveImage(mediaContentUrl)) { | ||
return ( | ||
GOOGLE_DRIVE_URL + "uc?export=view&id=" + getMediaId(mediaContentUrl) | ||
); | ||
} | ||
if (isYoutubeVideo(mediaContentUrl)) { | ||
return YOUTUBE_URL + "embed/" + getMediaId(mediaContentUrl); | ||
} | ||
return mediaContentUrl; | ||
}; | ||
|
||
const getMediaType = (mediaContentUrl: string) => { | ||
if (mediaContentUrl.includes(YOUTUBE_URL)) { | ||
return <iframe src={getEmbedLink(mediaContentUrl)} allowFullScreen />; | ||
} | ||
return ( | ||
<div className="media-content-image"> | ||
<LinkIcon | ||
url={mediaContentUrl} | ||
iconClassContainer="media-content-link" | ||
showOverlay={false} | ||
/> | ||
<img src={getEmbedLink(mediaContentUrl)} alt={mediaContentUrl} /> | ||
</div> | ||
); | ||
}; | ||
|
||
const renderMediaContent = () => { | ||
// @ts-ignore | ||
const mediaContent = question[Constants.HAS_MEDIA_CONTENT]; | ||
|
||
if (mediaContent) { | ||
if (Array.isArray(mediaContent)) { | ||
return ( | ||
<div className="col-6"> | ||
<div className="media-content-items"> | ||
{mediaContent.map((src: string, index: number) => ( | ||
<div | ||
key={index} | ||
className="row embed-responsive-21by9 media-content-video-container mb-3" | ||
> | ||
<iframe | ||
src={mediaContent[index]} | ||
className="embed-responsive-item" | ||
allowFullScreen | ||
/> | ||
<div key={index} className="media-content-item"> | ||
{getMediaType(mediaContent[index])} | ||
</div> | ||
))} | ||
</div> | ||
); | ||
} | ||
return ( | ||
<div className="col-6"> | ||
<iframe | ||
src={mediaContent} | ||
className="embed-responsive-item" | ||
allowFullScreen | ||
/> | ||
</div> | ||
<div className="media-content-item">{getMediaType(mediaContent)}</div> | ||
); | ||
} | ||
return null; | ||
}; | ||
|
||
return renderMedia(); | ||
return <div className="media-content">{renderMediaContent()}</div>; | ||
}; | ||
|
||
export default MediaContent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters