Unverified Commit 8ad018fb authored by Yohann MARTZOLFF's avatar Yohann MARTZOLFF Committed by GitHub

Example/with apivideo (#45272)

### Documentation / Examples
Hello Next.js 👋🏼

This PR aims to update the with-apivideo example by:
- Updating images domain name
- Switch to `export default function` for pages
- Updating the uploader playable and ingested status

### What do we use
In this example, we use several Next.js features:
- Routes and dynamic routes
- API and dynamic API routes

And api.video tools:
- NodeJS client
- React player (instead of our Player SDK)
- TypeScript uploader

Thank's in advance for your review 🙏🏼
parent 4d04a82d
...@@ -17,15 +17,15 @@ Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_mediu ...@@ -17,15 +17,15 @@ Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_mediu
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example: Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
```bash ```bash
npx create-next-app --example with-apivideo-app npx create-next-app --example with-apivideo
``` ```
```bash ```bash
yarn create next-app --example with-apivideo-app yarn create next-app --example with-apivideo
``` ```
```bash ```bash
pnpm create next-app --example with-apivideo-app pnpm create next-app --example with-apivideo
``` ```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
......
...@@ -7,7 +7,7 @@ interface ILoaderProps { ...@@ -7,7 +7,7 @@ interface ILoaderProps {
} }
const Loader: React.FC<ILoaderProps> = ({ done }): JSX.Element => const Loader: React.FC<ILoaderProps> = ({ done }): JSX.Element =>
done ? ( done ? (
<Image src="/check.png" width={30} height={30} /> <Image src="/check.png" alt="Loader" width={30} height={30} />
) : ( ) : (
<div className={styles.spinner} /> <div className={styles.spinner} />
) )
......
...@@ -5,7 +5,7 @@ const nextConfig = { ...@@ -5,7 +5,7 @@ const nextConfig = {
styledComponents: true, styledComponents: true,
}, },
images: { images: {
domains: ['cdn.api.video'], domains: ['vod.api.video'],
}, },
} }
......
...@@ -8,10 +8,10 @@ ...@@ -8,10 +8,10 @@
"dependencies": { "dependencies": {
"@api.video/nodejs-client": "2.2.5", "@api.video/nodejs-client": "2.2.5",
"@api.video/react-player": "^1.0.1", "@api.video/react-player": "^1.0.1",
"@api.video/video-uploader": "1.0.4", "@api.video/video-uploader": "^1.1.3",
"next": "latest", "next": "latest",
"react": "18.1.0", "react": "18.2.0",
"react-dom": "18.1.0" "react-dom": "18.2.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "17.0.23", "@types/node": "17.0.23",
......
import type { NextPage } from 'next'
import Head from 'next/head' import Head from 'next/head'
import Image from 'next/image' import Image from 'next/image'
import React from 'react' import React from 'react'
const Home: NextPage = () => ( export default function Home() {
<div className="global-container"> return (
<Head> <div className="global-container">
<title>api.video sample app</title> <Head>
<meta <title>api.video sample app</title>
name="description" <meta
content="Generated by create next app & created by api.video" name="description"
/> content="Generated by create next app & created by api.video"
<link rel="icon" href="/favicon.ico" /> />
</Head> <link rel="icon" href="/favicon.ico" />
</Head>
<header> <header>
<span>api.video sample app</span> <span>api.video sample app</span>
</header> </header>
<main> <main>
<div className="texts-container"> <div className="texts-container">
<p> <p>
Hey fellow dev! 👋 <br /> Hey fellow dev! 👋 <br />
In this basic sample app, you will find{' '} In this basic sample app, you will find{' '}
<a href="https://api.video" target="_blank" rel="noopener noreferrer"> <a
api.video href="https://api.video"
</a>{' '} target="_blank"
features examples such as <a href="/uploader">video uploader</a>,{' '} rel="noopener noreferrer"
<a href="/videos">videos list</a> and player components. >
</p> api.video
<p> </a>{' '}
api.video provides APIs and clients to handle all your video needs. features examples such as <a href="/uploader">video uploader</a>,{' '}
<br /> <a href="/videos">videos list</a> and player components.
This app is built with the api.video{' '} </p>
<a <p>
href="https://github.com/apivideo/api.video-nodejs-client" api.video provides APIs and clients to handle all your video needs.
target="_blank" <br />
rel="noopener noreferrer" This app is built with the api.video{' '}
> <a
Node.js client href="https://github.com/apivideo/api.video-nodejs-client"
</a> target="_blank"
,{' '} rel="noopener noreferrer"
<a >
href="https://github.com/apivideo/api.video-typescript-uploader" Node.js client
target="_blank" </a>
rel="noopener noreferrer" ,{' '}
> <a
Typescript uploader href="https://github.com/apivideo/api.video-typescript-uploader"
</a>{' '} target="_blank"
and{' '} rel="noopener noreferrer"
<a >
target="_blank" Typescript uploader
rel="noopener noreferrer" </a>{' '}
href="https://github.com/apivideo/api.video-react-player" and{' '}
> <a
React player component target="_blank"
rel="noopener noreferrer"
href="https://github.com/apivideo/api.video-react-player"
>
React player component
</a>
.
</p>
<p>
You can{' '}
<a
href="https://github.com/vercel/next.js/tree/canary/examples/with-apivideo"
target="_blank"
rel="noopener noreferrer"
>
check the source code on GitHub
</a>
.
</p>
<p>Please, choose a feature to explore.</p>
</div>
<div className="status-container">
<a className="button" href="/uploader">
Upload a video
</a> </a>
. <a className="button" href="/videos">
</p> See my videos
<p>
You can{' '}
<a
href="https://github.com/vercel/next.js/tree/canary/examples/with-apivideo"
target="_blank"
rel="noopener noreferrer"
>
check the source code on GitHub
</a> </a>
. </div>
</p> </main>
<p>Please, choose a feature to explore.</p>
</div>
<div className="status-container"> <footer>
<a className="button" href="/uploader"> <a
Upload a video href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a> </a>
<a className="button" href="/videos"> <span>and</span>
See my videos <a href="https://api.video" target="_blank" rel="noopener noreferrer">
api.video
</a> </a>
</div> </footer>
</main> </div>
)
<footer> }
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
<span>and</span>
<a href="https://api.video" target="_blank" rel="noopener noreferrer">
api.video
</a>
</footer>
</div>
)
export default Home
import type { NextPage } from 'next'
import Head from 'next/head' import Head from 'next/head'
import Image from 'next/image' import Image from 'next/image'
import React, { ChangeEvent, useEffect, useRef, useState } from 'react' import React, { ChangeEvent, useEffect, useRef, useState } from 'react'
...@@ -7,7 +6,7 @@ import { VideoUploader, VideoUploadResponse } from '@api.video/video-uploader' ...@@ -7,7 +6,7 @@ import { VideoUploader, VideoUploadResponse } from '@api.video/video-uploader'
import Status from '../../components/Status' import Status from '../../components/Status'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
const Uploader: NextPage = () => { export default function Uploader() {
const [uploadToken, setUploadToken] = useState<{ token: string } | undefined>( const [uploadToken, setUploadToken] = useState<{ token: string } | undefined>(
undefined undefined
) )
...@@ -16,10 +15,7 @@ const Uploader: NextPage = () => { ...@@ -16,10 +15,7 @@ const Uploader: NextPage = () => {
) )
const [video, setVideo] = useState<VideoUploadResponse | undefined>(undefined) const [video, setVideo] = useState<VideoUploadResponse | undefined>(undefined)
const [ready, setReady] = useState<boolean>(false) const [ready, setReady] = useState<boolean>(false)
const [status, setStatus] = useState<{ ingested: boolean; encoded: boolean }>( const [playable, setPlayable] = useState<boolean>(false)
{ ingested: false, encoded: false }
)
const [interId, setInterId] = useState<number | undefined>(undefined)
const inputRef = useRef<HTMLInputElement>(null) const inputRef = useRef<HTMLInputElement>(null)
const router = useRouter() const router = useRouter()
...@@ -28,30 +24,6 @@ const Uploader: NextPage = () => { ...@@ -28,30 +24,6 @@ const Uploader: NextPage = () => {
.then((res) => res.json()) .then((res) => res.json())
.then((res) => setUploadToken(res)) .then((res) => setUploadToken(res))
}, []) }, [])
useEffect(() => {
if (video) {
const fetchVideoStatus = async (videoId: string): Promise<void> => {
const { status } = await fetch(`/api/${videoId}`).then((res) =>
res.json()
)
const { encoding, ingest } = status
setStatus({
ingested: ingest.status === 'uploaded',
encoded: encoding.playable,
})
if (ingest.status === 'uploaded' && encoding.playable) setReady(true)
}
const intervalId = window.setInterval(() => {
fetchVideoStatus(video.videoId)
}, 1000)
setInterId(intervalId)
return () => window.clearInterval(intervalId)
}
}, [video, ready])
useEffect(() => {
ready && window.clearInterval(interId)
}, [interId, ready])
const handleSelectFile = async ( const handleSelectFile = async (
e: ChangeEvent<HTMLInputElement> e: ChangeEvent<HTMLInputElement>
...@@ -60,7 +32,7 @@ const Uploader: NextPage = () => { ...@@ -60,7 +32,7 @@ const Uploader: NextPage = () => {
if (!uploadToken || !uploadToken.token) return if (!uploadToken || !uploadToken.token) return
const clearState = (): void => { const clearState = (): void => {
setReady(false) setReady(false)
setStatus({ ingested: false, encoded: false }) setPlayable(false)
setVideo(undefined) setVideo(undefined)
setUploadProgress(undefined) setUploadProgress(undefined)
} }
...@@ -74,6 +46,10 @@ const Uploader: NextPage = () => { ...@@ -74,6 +46,10 @@ const Uploader: NextPage = () => {
uploader.onProgress((e) => uploader.onProgress((e) =>
setUploadProgress(Math.round((e.uploadedBytes * 100) / e.totalBytes)) setUploadProgress(Math.round((e.uploadedBytes * 100) / e.totalBytes))
) )
uploader.onPlayable(() => {
setPlayable(true)
setReady(true)
})
const video = await uploader.upload() const video = await uploader.upload()
setVideo(video) setVideo(video)
} }
...@@ -179,9 +155,9 @@ const Uploader: NextPage = () => { ...@@ -179,9 +155,9 @@ const Uploader: NextPage = () => {
<div className="status-container"> <div className="status-container">
<Status title="Uploaded" done={uploadProgress >= 100} /> <Status title="Uploaded" done={uploadProgress >= 100} />
<span /> <span />
<Status title="Ingested" done={status.ingested} /> <Status title="Ingested" done={uploadProgress >= 100} />
<span /> <span />
<Status title="Playable" done={status.encoded} /> <Status title="Playable" done={playable} />
</div> </div>
<Card <Card
content="https://ws.api.video/videos/{videoId}/source" content="https://ws.api.video/videos/{videoId}/source"
...@@ -217,5 +193,3 @@ const Uploader: NextPage = () => { ...@@ -217,5 +193,3 @@ const Uploader: NextPage = () => {
</div> </div>
) )
} }
export default Uploader
import type { NextPage } from 'next'
import Head from 'next/head' import Head from 'next/head'
import Image from 'next/image' import Image from 'next/image'
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
import VideosListResponse from '@api.video/nodejs-client/lib/model/VideosListResponse' import VideosListResponse from '@api.video/nodejs-client/lib/model/VideosListResponse'
const Videos: NextPage = () => { export default function Videos() {
const [videosResponse, setVideosResponse] = useState< const [videosResponse, setVideosResponse] = useState<
VideosListResponse | undefined VideosListResponse | undefined
>(undefined) >(undefined)
...@@ -107,5 +106,3 @@ const Videos: NextPage = () => { ...@@ -107,5 +106,3 @@ const Videos: NextPage = () => {
</div> </div>
) )
} }
export default Videos
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment