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
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
npx create-next-app --example with-apivideo-app
npx create-next-app --example with-apivideo
```
```bash
yarn create next-app --example with-apivideo-app
yarn create next-app --example with-apivideo
```
```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)).
......
......@@ -7,7 +7,7 @@ interface ILoaderProps {
}
const Loader: React.FC<ILoaderProps> = ({ done }): JSX.Element =>
done ? (
<Image src="/check.png" width={30} height={30} />
<Image src="/check.png" alt="Loader" width={30} height={30} />
) : (
<div className={styles.spinner} />
)
......
......@@ -5,7 +5,7 @@ const nextConfig = {
styledComponents: true,
},
images: {
domains: ['cdn.api.video'],
domains: ['vod.api.video'],
},
}
......
......@@ -8,10 +8,10 @@
"dependencies": {
"@api.video/nodejs-client": "2.2.5",
"@api.video/react-player": "^1.0.1",
"@api.video/video-uploader": "1.0.4",
"@api.video/video-uploader": "^1.1.3",
"next": "latest",
"react": "18.1.0",
"react-dom": "18.1.0"
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@types/node": "17.0.23",
......
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import React from 'react'
const Home: NextPage = () => (
<div className="global-container">
<Head>
<title>api.video sample app</title>
<meta
name="description"
content="Generated by create next app & created by api.video"
/>
<link rel="icon" href="/favicon.ico" />
</Head>
export default function Home() {
return (
<div className="global-container">
<Head>
<title>api.video sample app</title>
<meta
name="description"
content="Generated by create next app & created by api.video"
/>
<link rel="icon" href="/favicon.ico" />
</Head>
<header>
<span>api.video sample app</span>
</header>
<header>
<span>api.video sample app</span>
</header>
<main>
<div className="texts-container">
<p>
Hey fellow dev! 👋 <br />
In this basic sample app, you will find{' '}
<a href="https://api.video" target="_blank" rel="noopener noreferrer">
api.video
</a>{' '}
features examples such as <a href="/uploader">video uploader</a>,{' '}
<a href="/videos">videos list</a> and player components.
</p>
<p>
api.video provides APIs and clients to handle all your video needs.
<br />
This app is built with the api.video{' '}
<a
href="https://github.com/apivideo/api.video-nodejs-client"
target="_blank"
rel="noopener noreferrer"
>
Node.js client
</a>
,{' '}
<a
href="https://github.com/apivideo/api.video-typescript-uploader"
target="_blank"
rel="noopener noreferrer"
>
Typescript uploader
</a>{' '}
and{' '}
<a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/apivideo/api.video-react-player"
>
React player component
<main>
<div className="texts-container">
<p>
Hey fellow dev! 👋 <br />
In this basic sample app, you will find{' '}
<a
href="https://api.video"
target="_blank"
rel="noopener noreferrer"
>
api.video
</a>{' '}
features examples such as <a href="/uploader">video uploader</a>,{' '}
<a href="/videos">videos list</a> and player components.
</p>
<p>
api.video provides APIs and clients to handle all your video needs.
<br />
This app is built with the api.video{' '}
<a
href="https://github.com/apivideo/api.video-nodejs-client"
target="_blank"
rel="noopener noreferrer"
>
Node.js client
</a>
,{' '}
<a
href="https://github.com/apivideo/api.video-typescript-uploader"
target="_blank"
rel="noopener noreferrer"
>
Typescript uploader
</a>{' '}
and{' '}
<a
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>
.
</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 className="button" href="/videos">
See my videos
</a>
.
</p>
<p>Please, choose a feature to explore.</p>
</div>
</div>
</main>
<div className="status-container">
<a className="button" href="/uploader">
Upload a video
<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>
<a className="button" href="/videos">
See my videos
<span>and</span>
<a href="https://api.video" target="_blank" rel="noopener noreferrer">
api.video
</a>
</div>
</main>
<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
</footer>
</div>
)
}
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import React, { ChangeEvent, useEffect, useRef, useState } from 'react'
......@@ -7,7 +6,7 @@ import { VideoUploader, VideoUploadResponse } from '@api.video/video-uploader'
import Status from '../../components/Status'
import { useRouter } from 'next/router'
const Uploader: NextPage = () => {
export default function Uploader() {
const [uploadToken, setUploadToken] = useState<{ token: string } | undefined>(
undefined
)
......@@ -16,10 +15,7 @@ const Uploader: NextPage = () => {
)
const [video, setVideo] = useState<VideoUploadResponse | undefined>(undefined)
const [ready, setReady] = useState<boolean>(false)
const [status, setStatus] = useState<{ ingested: boolean; encoded: boolean }>(
{ ingested: false, encoded: false }
)
const [interId, setInterId] = useState<number | undefined>(undefined)
const [playable, setPlayable] = useState<boolean>(false)
const inputRef = useRef<HTMLInputElement>(null)
const router = useRouter()
......@@ -28,30 +24,6 @@ const Uploader: NextPage = () => {
.then((res) => res.json())
.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 (
e: ChangeEvent<HTMLInputElement>
......@@ -60,7 +32,7 @@ const Uploader: NextPage = () => {
if (!uploadToken || !uploadToken.token) return
const clearState = (): void => {
setReady(false)
setStatus({ ingested: false, encoded: false })
setPlayable(false)
setVideo(undefined)
setUploadProgress(undefined)
}
......@@ -74,6 +46,10 @@ const Uploader: NextPage = () => {
uploader.onProgress((e) =>
setUploadProgress(Math.round((e.uploadedBytes * 100) / e.totalBytes))
)
uploader.onPlayable(() => {
setPlayable(true)
setReady(true)
})
const video = await uploader.upload()
setVideo(video)
}
......@@ -179,9 +155,9 @@ const Uploader: NextPage = () => {
<div className="status-container">
<Status title="Uploaded" done={uploadProgress >= 100} />
<span />
<Status title="Ingested" done={status.ingested} />
<Status title="Ingested" done={uploadProgress >= 100} />
<span />
<Status title="Playable" done={status.encoded} />
<Status title="Playable" done={playable} />
</div>
<Card
content="https://ws.api.video/videos/{videoId}/source"
......@@ -217,5 +193,3 @@ const Uploader: NextPage = () => {
</div>
)
}
export default Uploader
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import React, { useEffect, useState } from 'react'
import VideosListResponse from '@api.video/nodejs-client/lib/model/VideosListResponse'
const Videos: NextPage = () => {
export default function Videos() {
const [videosResponse, setVideosResponse] = useState<
VideosListResponse | undefined
>(undefined)
......@@ -107,5 +106,3 @@ const Videos: NextPage = () => {
</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