Skip to content

Commit

Permalink
feat(strapi-implementation): add new types
Browse files Browse the repository at this point in the history
  • Loading branch information
nathandebalthasar committed Sep 28, 2023
1 parent 155cb92 commit db331df
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 27 deletions.
4 changes: 4 additions & 0 deletions apps/trackflix/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ amplifytools.xcconfig
.secret-*
**.sample
#amplify-do-not-edit-end

.env
.env.development
.env.production
2 changes: 1 addition & 1 deletion apps/trackflix/src/api/IApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { VideoOnDemand } from '../models'
import { VideoOnDemand } from './api.interface'

export interface IApi {
fetchHighlightedVideos(): Promise<VideoOnDemand[]>
Expand Down
34 changes: 29 additions & 5 deletions apps/trackflix/src/api/StrapiApi.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
import { VideoOnDemand } from '../models'
import { VideoOnDemand } from './api.interface'
import { IApi } from './IApi'
import { StrapiMedia } from './strapi.interface'

export class StrapiApi implements IApi {
private readonly endpoint: string
private readonly baseUrl: string

private readonly token: string

constructor(endpoint: string, token: string) {
this.endpoint = endpoint
constructor({ baseUrl, token }: { baseUrl?: string; token?: string }) {
if (!baseUrl || !token)
throw new Error('StrapiApi: baseUrl and token are required')

this.baseUrl = baseUrl
this.token = token
}

async fetchHighlightedVideos(): Promise<VideoOnDemand[]> {
return []
const response = await fetch(
`${this.baseUrl}/api/vods?filters[highlighted][$eq]=true`,
{
headers: {
Authorization: `Bearer ${this.token}`,
},
}
)

const videos: StrapiMedia[] = await response
.json()
.then((res) => res.data)
return videos.map((video: StrapiMedia) => ({
id: video.id,
title: video.attributes.Name,
description: video.attributes.description,
highlighted: video.attributes.highlighted,
createdAt: video.attributes.createdAt,
updatedAt: video.attributes.updatedAt,
src: video.attributes.media_url,
}))
}
}
14 changes: 14 additions & 0 deletions apps/trackflix/src/api/api.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface Thumbnail {
src: string
}

export interface VideoOnDemand {
id: string
title: string
description: string
highlighted: boolean
thumbnail?: Thumbnail
createdAt: string
updatedAt: string
src: string
}
14 changes: 14 additions & 0 deletions apps/trackflix/src/api/strapi.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface StrapiMedia {
id: string
attributes: {
Name: string
createdAt: string
updatedAt: string
rotation_start?: number
rotation_end?: number
description: string
type?: string
media_url: string
highlighted: boolean
}
}
12 changes: 12 additions & 0 deletions apps/trackflix/src/context/CMSContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IApi } from '../api/IApi'
import { StrapiApi } from '../api/StrapiApi'
import { createContext } from 'react'

export const CMSContext = createContext<{
api: IApi
}>({
api: new StrapiApi({
baseUrl: process.env.REACT_APP_CMS_BASE_URL,
token: process.env.REACT_APP_CMS_TOKEN,
}),
})
11 changes: 8 additions & 3 deletions apps/trackflix/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useState, useEffect } from 'react'
import React, { useState, useEffect, useContext } from 'react'
import Layout from '../shared/components/Layout'
import Landing from '../shared/home/Landing'
import Videos from '../shared/videos'
import defaultTheme, { NavbarTheme, defaultNavbar } from '../shared/theme'
import { CMSContext } from '../context/CMSContext'

const noScrollNavBarTheme: NavbarTheme = {
type: 'noScroll',
Expand All @@ -27,6 +28,8 @@ const HomePage = () => {
navbar: noScrollNavBarTheme,
},
})
const apiContext = useContext(CMSContext)

const handleScroll = () => {
if (window.pageYOffset > 20 && theme.palette.navbar.type !== 'scroll') {
setTheme({
Expand Down Expand Up @@ -61,8 +64,10 @@ const HomePage = () => {

return (
<Layout overrideTheme={theme} removePaddingTop>
<Landing />
<Videos />
<CMSContext.Provider value={apiContext}>
<Landing />
<Videos />
</CMSContext.Provider>
</Layout>
)
}
Expand Down
28 changes: 10 additions & 18 deletions apps/trackflix/src/shared/home/Landing.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useContext, useEffect, useState } from 'react'
import styled from 'styled-components'
import { fetchHighlightedVideos } from '../api'
import ReactPlayer from 'react-player'
Expand All @@ -7,6 +7,8 @@ import intro from '../../videos/introduction-video.mp4'
import PlayLogo from '../../assets/logo/logo-play-without-circle.svg'
import { screenSizes } from '../constants'
import { useWindowDimensions } from '../hooks'
import { CMSContext } from '../../context/CMSContext'
import { VideoOnDemand } from '../../api/api.interface'

const LandingContainer = styled.div`
width: 100%;
Expand Down Expand Up @@ -95,29 +97,19 @@ const StyledPlayLogo = styled(PlayLogo)`

const Landing = () => {
const [source, setSource] = useState<string | null>(null)
const [video, setVideo] = useState(null)
const [video, setVideo] = useState<VideoOnDemand | null>(null)
const { width } = useWindowDimensions()
const { api } = useContext(CMSContext)

useEffect(() => {
const fetchHighlited = async () => {
const highlightedVideos = await fetchHighlightedVideos()
if (
highlightedVideos &&
highlightedVideos.data &&
highlightedVideos.data.listMedia &&
highlightedVideos.data.listMedia.items &&
highlightedVideos.data.listMedia.items.length > 0
) {
const media = highlightedVideos.data.listMedia.items[0]
const highlightedVideos = await api.fetchHighlightedVideos()
if (highlightedVideos && highlightedVideos.length > 0) {
const media = highlightedVideos[0]
setVideo(media)
setSource(media.source)
setSource(media.src)
} else {
// test purpose, to replace later
setSource(
'https://dzmkyt5xmjxxq.cloudfront.net/public/fdcbf258-ee64-466e-aa08-4ac06cf69117/fdcbf258-ee64-466e-aa08-4ac06cf69117.m3u8'
)
//
// setSource(intro)
setSource(intro)
}
}
fetchHighlited()
Expand Down

0 comments on commit db331df

Please sign in to comment.