Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mini challenge5 #99

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error",
"react/jsx-filename-extension": "error",
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
"react-hooks/exhaustive-deps": "warn",
"import/no-unresolved": ["off", { "ignore": [".css$"] }],
"import/prefer-default-export": "off",
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

#netlify

.netlify
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
"@testing-library/user-event": "^12.1.3",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-icons": "^4.2.0",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3"
"react-scripts": "3.4.3",
"styled-components": "^5.3.0"
},
"scripts": {
"start": "react-scripts start",
Expand All @@ -34,6 +36,9 @@
"prettier": "^2.1.1",
"pretty-quick": "^3.0.0"
},
"resolutions": {
"styled-components": "^5"
},
"browserslist": {
"production": [
">0.2%",
Expand Down
100 changes: 58 additions & 42 deletions src/components/App/App.component.jsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,73 @@
import React, { useLayoutEffect } from 'react';
import React, { useState } from 'react';
import { BrowserRouter, Switch, Route } from 'react-router-dom';

import { ThemeProvider } from 'styled-components';
import AuthProvider from '../../providers/Auth';
import HomePage from '../../pages/Home';
import FavoritesPage from '../../pages/Favorites';
import LoginPage from '../../pages/Login';
import NotFound from '../../pages/NotFound';
import SecretPage from '../../pages/Secret';
import Private from '../Private';
import Fortune from '../Fortune';
import Layout from '../Layout';
import { random } from '../../utils/fns';

function App() {
useLayoutEffect(() => {
const { body } = document;

function rotateBackground() {
const xPercent = random(100);
const yPercent = random(100);
body.style.setProperty('--bg-position', `${xPercent}% ${yPercent}%`);
}

const intervalId = setInterval(rotateBackground, 3000);
body.addEventListener('click', rotateBackground);

return () => {
clearInterval(intervalId);
body.removeEventListener('click', rotateBackground);
};
}, []);
import { GlobalStyle, themes } from '../../globalStyles';
import VideoPlayer from '../../pages/VideoPlayer';
import ThemeContext from '../Context/ThemeContext';
import VideoContext from '../Context/VideoContext';
import { FavoritesProvider } from '../../providers/Favorites/Favorites.provider';
import FavoriteDetailsPage from '../../pages/FavoriteDetails/FavoriteDetails.page';
import VideoFavoriteContext from '../Context/VideoFavorite';

function App() {
const [theme, setTheme] = useState('light');
const [video, setVideo] = useState({});
const [videoFavorite, setVideoFavorite] = useState({});
return (
<BrowserRouter>
<AuthProvider>
<Layout>
<Switch>
<Route exact path="/">
<HomePage />
</Route>
<Route exact path="/login">
<LoginPage />
</Route>
<Private exact path="/secret">
<SecretPage />
</Private>
<Route path="*">
<NotFound />
</Route>
</Switch>
<Fortune />
</Layout>
</AuthProvider>
</BrowserRouter>
<>
<ThemeProvider theme={themes[theme]}>
<GlobalStyle />
<BrowserRouter>
<AuthProvider>
<FavoritesProvider>
<Layout>
<VideoFavoriteContext.Provider
value={{ videoFavorite, setVideoFavorite }}
>
<VideoContext.Provider value={{ video, setVideo }}>
<ThemeContext.Provider value={{ theme, setTheme }}>
<Switch>
<Route exact path="/">
<HomePage />
</Route>
<Route path="/watch">
<VideoPlayer />
</Route>
<Route exact path="/login">
<LoginPage />
</Route>
<Private exact path="/favorites">
<FavoritesPage />
</Private>
<Private path="/favorites/">
<FavoriteDetailsPage />
</Private>
<Private exact path="/secret">
<SecretPage />
</Private>
<Route path="*">
<NotFound />
</Route>
</Switch>
</ThemeContext.Provider>
</VideoContext.Provider>
</VideoFavoriteContext.Provider>
</Layout>
</FavoritesProvider>
</AuthProvider>
</BrowserRouter>
</ThemeProvider>
</>
);
}

Expand Down
92 changes: 92 additions & 0 deletions src/components/CardVideo/CardVideo.component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import React, { useContext, useEffect, useState } from 'react';
import VideoContext from '../Context/VideoContext';
import VideoFavoriteContext from '../Context/VideoFavorite';
import LinkVideo from '../Link.element';
import {
CardVideoDisplayerContainer,
CardVideoContainer,
CardVideoImage,
CardVideoBottom,
} from './CardVideo.elements';

export function CardVideo({ video, id }) {
const urlVideo = `/watch?v=${id}`;

const { setVideo } = useContext(VideoContext);

const handleClick = () => {
setVideo(video);
};

return (
<>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this empty fragment <> is necessary here

<LinkVideo to={urlVideo} onClick={handleClick}>
<CardVideoContainer>
<CardVideoImage
src={
video.snippet.thumbnails.high.url
? video.snippet.thumbnails.high.url
: video.snippet.thumbnails.default.url
}
/>
<CardVideoBottom
title={video.snippet.title}
description={video.snippet.description}
/>
</CardVideoContainer>
</LinkVideo>
</>
);
}

export function CardVideoFavorite({ video, id }) {
const urlVideo = `favorites/${id}`;

const { setVideoFavorite } = useContext(VideoFavoriteContext);

const handleClick = () => {
setVideoFavorite(id);
};

return (
<>
<LinkVideo to={urlVideo} onClick={handleClick}>
<CardVideoContainer>
<CardVideoImage
src={
video.snippet.thumbnails.high.url
? video.snippet.thumbnails.high.url
: video.snippet.thumbnails.default.url
}
/>
<CardVideoBottom
title={video.snippet.title}
description={video.snippet.description}
/>
</CardVideoContainer>
</LinkVideo>
</>
);
}

function CardVideoDisplayer({ videos }) {
const [listVideos, setListVideo] = useState();

useEffect(() => {
if (videos) {
setListVideo(
videos.map((video) => (
<CardVideo id={video.id.videoId} key={video.etag} video={video} />
))
);
}
}, [videos]);

return (
<>
<CardVideoDisplayerContainer>{listVideos}</CardVideoDisplayerContainer>
</>
);
}

export default CardVideoDisplayer;
81 changes: 81 additions & 0 deletions src/components/CardVideo/CardVideo.elements.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React from 'react';
import styled from 'styled-components';

export const CardVideoDisplayerContainer = styled.div`
display: flex;
flex-wrap: wrap;
position: absolute;
justify-content: center;
padding-bottom: 2rem;

top: ${(props) => props.theme.navbar_height};
left: ${(props) => props.theme.sidemenu_width};

width: calc(100vw - ${(props) => props.theme.sidemenu_width});
`;

export const CardVideoContainer = styled.div`
width: calc((100vw - ${({ theme }) => theme.sidemenu_width}) / 1 - 4rem - 0.01px);
aspect-ratio: 16/14;
margin-top: 2rem;
margin-left: 1rem;
margin-right: 1rem;

@media (min-width: 750px) {
width: calc((100vw - ${({ theme }) => theme.sidemenu_width}) / 2 - 4rem - 0.01px);
}

@media (min-width: 1000px) {
width: calc((100vw - ${({ theme }) => theme.sidemenu_width}) / 3 - 4rem - 0.01px);
}

&:hover {
cursor: pointer;
}
`;

export const CardVideoImage = styled.img`
width: 100%;
aspect-ratio: 16/9;
height: auto;
`;

const CardVideoBottomContainer = styled.div`
display: flex;
flex-direction: column;
aspect-ratio: 16/5;
align-items: center;
justify-content: center;
`;

const CardVideoTitle = styled.h2`
color: ${(props) => props.theme.text_color};
font-size: 1.2rem;
padding: 0.5rem 0.3rem;
text-align: center;
`;

const CardVideoDescription = styled.p`
color: ${(props) => props.theme.text_color};

padding-left: 0.5rem;
padding-right: 0.5rem;
padding-bottom: 0.5rem;
font-size: 1rem;
`;

export function CardVideoBottom({ title, description }) {
function trucateText(text) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function can live outside CardVideoBottom so that it does not get redefined on every render

const maxLength = 50;
return text.length <= maxLength ? text : `${text.substring(0, maxLength)}...`;
}

return (
<>
<CardVideoBottomContainer>
<CardVideoTitle>{title}</CardVideoTitle>
<CardVideoDescription>{trucateText(description)}</CardVideoDescription>
</CardVideoBottomContainer>
</>
);
}
37 changes: 37 additions & 0 deletions src/components/CardVideo/CardVideo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { render, screen } from '@testing-library/react';
import React from 'react';
import CardVideoDisplayer from './CardVideo.component';

describe('navbar', () => {
beforeEach(() => {
const videos = [
{
etag: 'sadlkjsakdljsa',
snippet: {
title: 'Wizeline',
description: 'hlksjdakjskldja asjdaks',
thumbnails: {
high: {
url:
'https://yt3.ggpht.com/ytc/AAUvwnighSReQlmHl_S_vSfvnWBAG5Cw4A0YxtE0tm5OpQ=s800-c-k-c0xffffffff-no-rj-mo',
},
},
},
},
];

render(<CardVideoDisplayer videos={videos} />);
});

test('should contains a title', () => {
const title = screen.queryByText('Wizeline');

expect(title).toBeInTheDocument();
});

test('should contains a img', () => {
const img = screen.queryByText('hlksjdakjskldja asjdaks');

expect(img).toBeInTheDocument();
});
});
1 change: 1 addition & 0 deletions src/components/CardVideo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, CardVideo } from './CardVideo.component';
5 changes: 5 additions & 0 deletions src/components/Context/ThemeContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from 'react';

const ThemeContext = createContext({});

export default ThemeContext;
5 changes: 5 additions & 0 deletions src/components/Context/VideoContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from 'react';

const VideoContext = createContext({});

export default VideoContext;
5 changes: 5 additions & 0 deletions src/components/Context/VideoFavorite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from 'react';

const VideoFavoriteContext = createContext({});

export default VideoFavoriteContext;
8 changes: 8 additions & 0 deletions src/components/Link.element.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import styled from 'styled-components';
import { Link } from 'react-router-dom';

const LinkVideo = styled(Link)`
text-decoration: none;
`;

export default LinkVideo;
Loading