initial push, added main app wrapper component, a youtube service o fetch the youtube data, key in .env not pushed, and pagination
parent
2928185386
commit
d3634f3e35
|
@ -0,0 +1,28 @@
|
|||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
|
||||
# environment
|
||||
|
||||
.env
|
|
@ -0,0 +1,14 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Youtube API</title>
|
||||
<link rel="stylesheet" href="./src/index.css" />
|
||||
<script type="module" src="/src/MainApp.js"></script>
|
||||
</head>
|
||||
<body class="">
|
||||
<main-app />
|
||||
</body>
|
||||
</html>
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"name": "litcode",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@twind/core": "^1.1.3",
|
||||
"axios": "^1.7.7",
|
||||
"dotenv": "^16.4.5",
|
||||
"lit": "^3.2.0",
|
||||
"twind": "^0.16.19"
|
||||
},
|
||||
"devDependencies": {
|
||||
"autoprefixer": "^10.4.20",
|
||||
"postcss": "^8.4.47",
|
||||
"tailwindcss": "^3.4.13",
|
||||
"vite": "^5.4.8"
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
After Width: | Height: | Size: 1.5 KiB |
|
@ -0,0 +1,115 @@
|
|||
import { LitElement, css, html } from 'lit'
|
||||
import { YoutubeDataGrid } from './components/YoutubeDataGrid'
|
||||
|
||||
|
||||
export class MainApp extends LitElement {
|
||||
static get properties() {
|
||||
return {
|
||||
/**
|
||||
* Copy for the read the docs hint.
|
||||
*/
|
||||
docsHint: { type: String },
|
||||
|
||||
/**
|
||||
* The number of times the button has been clicked.
|
||||
*/
|
||||
count: { type: Number },
|
||||
}
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.docsHint = ''
|
||||
this.count = 0
|
||||
}
|
||||
|
||||
render() {
|
||||
return html`
|
||||
<style>
|
||||
:host {
|
||||
display: block;
|
||||
max-width: 1280px;
|
||||
margin: 0 auto;
|
||||
padding: 1rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.logo {
|
||||
height: 3rem;
|
||||
padding: 0.5rem 1rem;
|
||||
filter: drop-shadow(0 0 2em #646cffaa);
|
||||
transition: filter 0.3s;
|
||||
}
|
||||
|
||||
.logo:hover {
|
||||
filter: drop-shadow(0 0 2em #535bffaa);
|
||||
}
|
||||
|
||||
.card {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.read-the-docs {
|
||||
color: #888;
|
||||
}
|
||||
|
||||
a {
|
||||
font-weight: bold;
|
||||
color: #646cff;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
color: #535bff;
|
||||
}
|
||||
|
||||
::slotted(h1) {
|
||||
font-size: 2rem;
|
||||
line-height: 1.2;
|
||||
}
|
||||
|
||||
button {
|
||||
border-radius: 0.5rem;
|
||||
border: none;
|
||||
padding: 0.6rem 1.2rem;
|
||||
font-size: 1rem;
|
||||
font-weight: bold;
|
||||
background-color: #1a1a1a;
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.25s;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background-color: #535bff;
|
||||
}
|
||||
|
||||
button:focus,
|
||||
button:focus-visible {
|
||||
outline: 4px auto #646cff;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: light) {
|
||||
a:hover {
|
||||
color: #747bff;
|
||||
}
|
||||
|
||||
button {
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
<div class="container mx-0 flex flex-col gap-4 p-4">
|
||||
<h1 class="text-xl font-bold">YouTube API </h1>
|
||||
</div>
|
||||
<youtube-data-grid />
|
||||
<slot></slot>
|
||||
|
||||
`
|
||||
}
|
||||
|
||||
_onClick() {
|
||||
this.count++
|
||||
}
|
||||
}
|
||||
|
||||
window.customElements.define('main-app', MainApp)
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="25.6" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 320"><path fill="#00E8FF" d="m64 192l25.926-44.727l38.233-19.114l63.974 63.974l10.833 61.754L192 320l-64-64l-38.074-25.615z"></path><path fill="#283198" d="M128 256V128l64-64v128l-64 64ZM0 256l64 64l9.202-60.602L64 192l-37.542 23.71L0 256Z"></path><path fill="#324FFF" d="M64 192V64l64-64v128l-64 64Zm128 128V192l64-64v128l-64 64ZM0 256V128l64 64l-64 64Z"></path><path fill="#0FF" d="M64 320V192l64 64z"></path></svg>
|
After Width: | Height: | Size: 639 B |
|
@ -0,0 +1,138 @@
|
|||
import { LitElement, css, html } from 'lit';
|
||||
import YoutubeService from '../utils/YoutubeService.js';
|
||||
import PaginationComponent from './ui/PaginationComponent.js';
|
||||
|
||||
export class YoutubeDataGrid extends LitElement {
|
||||
static get properties() {
|
||||
return {
|
||||
keyword: { type: String },
|
||||
sortBy: { type: String },
|
||||
videos: { type: Array },
|
||||
totalResults: { type: Number },
|
||||
currentPage: { type: Number },
|
||||
};
|
||||
}
|
||||
|
||||
static get styles() {
|
||||
return css`
|
||||
:host {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 1rem;
|
||||
max-width: 1280px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
input,
|
||||
select,
|
||||
button {
|
||||
margin: 0.5rem;
|
||||
}
|
||||
|
||||
.video-card {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
padding: 1rem;
|
||||
margin: 1rem;
|
||||
background-color: #f7f7f7;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.video-card img {
|
||||
width: 128px;
|
||||
margin-right: 1rem;
|
||||
}
|
||||
|
||||
.video-card h3 {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.video-card p {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.keyword = '';
|
||||
this.sortBy = 'relevance';
|
||||
this.videos = [];
|
||||
this.totalResults = 0;
|
||||
this.currentPage = 1;
|
||||
}
|
||||
|
||||
render() {
|
||||
return html`
|
||||
<div>
|
||||
<label for="search">Search:</label>
|
||||
<input type="text" id="search" @input=${this.handleInput} />
|
||||
<select @change=${this.handleSelect}>
|
||||
<option value="relevance">Relevance</option>
|
||||
<option value="date">Date</option>
|
||||
<option value="rating">Rating</option>
|
||||
</select>
|
||||
<button @click=${this.searchVideos}>Search</button>
|
||||
</div>
|
||||
<div>
|
||||
Total results: ${this.totalResults}
|
||||
</div>
|
||||
<div>
|
||||
${this.videos.map(
|
||||
(video) => html`
|
||||
<div class="video-card">
|
||||
<a href="https://www.youtube.com/watch?v=${video.videoId}" target="_blank">
|
||||
<img src=${video.thumbnail} alt=${video.title} />
|
||||
</a>
|
||||
<div>
|
||||
<h3><a href="https://www.youtube.com/watch?v=${video.videoId}" target="_blank">${video.title}</a></h3>
|
||||
<p>${this.sanitizeHtml(video.description)}</p>
|
||||
</div>
|
||||
<div>Comments: ${video.commentCount}</div>
|
||||
</div>
|
||||
`
|
||||
)}
|
||||
</div>
|
||||
<pagination-component
|
||||
.totalCount=${this.totalResults}
|
||||
.hitsPerPage=${10}
|
||||
.onPageChange=${this.handlePageChange}
|
||||
.currentPage=${this.currentPage}
|
||||
></pagination-component>
|
||||
`;
|
||||
}
|
||||
|
||||
sanitizeHtml(html) {
|
||||
const doc = document.createElement('div');
|
||||
doc.innerHTML = html;
|
||||
return doc.textContent || doc.innerText;
|
||||
}
|
||||
|
||||
handleInput(event) {
|
||||
this.keyword = event.target.value;
|
||||
}
|
||||
|
||||
handleSelect(event) {
|
||||
this.sortBy = event.target.value;
|
||||
}
|
||||
|
||||
async searchVideos() {
|
||||
const { videos, totalResults } = await YoutubeService.searchVideos(
|
||||
this.keyword,
|
||||
this.sortBy,
|
||||
10,
|
||||
(this.currentPage - 1) * 10 + 1
|
||||
);
|
||||
this.videos = videos;
|
||||
this.totalResults = totalResults;
|
||||
}
|
||||
|
||||
handlePageChange = (page) => {
|
||||
this.currentPage = page;
|
||||
this.searchVideos();
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define('youtube-data-grid', YoutubeDataGrid);
|
|
@ -0,0 +1,66 @@
|
|||
import { LitElement, html } from 'lit';
|
||||
|
||||
export default class PaginationComponent extends LitElement {
|
||||
static get properties() {
|
||||
return {
|
||||
totalCount: { type: Number },
|
||||
hitsPerPage: { type: Number },
|
||||
onPageChange: { type: Function },
|
||||
currentPage: { type: Number },
|
||||
};
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.totalCount = 0;
|
||||
this.hitsPerPage = 10;
|
||||
this.onPageChange = () => {};
|
||||
this.currentPage = 1;
|
||||
}
|
||||
|
||||
render() {
|
||||
const totalPages = Math.ceil(this.totalCount / this.hitsPerPage);
|
||||
const pages = [];
|
||||
if (totalPages <= 10) {
|
||||
for (let i = 0; i < totalPages; i++) {
|
||||
pages.push(i + 1);
|
||||
}
|
||||
} else {
|
||||
if (this.currentPage <= 5) {
|
||||
for (let i = 0; i < 10; i++) {
|
||||
pages.push(i + 1);
|
||||
}
|
||||
} else if (this.currentPage + 5 > totalPages) {
|
||||
for (let i = totalPages - 10 + 1; i <= totalPages; i++) {
|
||||
pages.push(i);
|
||||
}
|
||||
} else {
|
||||
pages.push(1);
|
||||
pages.push(2);
|
||||
pages.push('...');
|
||||
for (let i = this.currentPage - 2; i <= this.currentPage + 2; i++) {
|
||||
pages.push(i);
|
||||
}
|
||||
pages.push('...');
|
||||
pages.push(totalPages - 1);
|
||||
pages.push(totalPages);
|
||||
}
|
||||
}
|
||||
return html`
|
||||
<div>
|
||||
${pages.map((page) =>
|
||||
page === '...' ? html`<span>...</span>` : html`
|
||||
<button
|
||||
@click=${() => this.onPageChange(page)}
|
||||
?active=${this.currentPage === page}
|
||||
>
|
||||
${page}
|
||||
</button>
|
||||
`
|
||||
)}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define('pagination-component', PaginationComponent);
|
|
@ -0,0 +1,6 @@
|
|||
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap');
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: 'Inter', sans-serif;
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
import axios from 'axios';
|
||||
class YoutubeService {
|
||||
static async searchVideos(keyword, sortBy = 'relevance', maxResults = 10) {
|
||||
const apiKey =import.meta.env.VITE_YOUTUBE_API_KEY;
|
||||
const url = `https://www.googleapis.com/youtube/v3/search?part=snippet,id&order=${sortBy}&maxResults=${maxResults}&q=${keyword}&key=${apiKey}`;
|
||||
|
||||
try {
|
||||
const response = await axios.get(url);
|
||||
|
||||
const videos = response.data.items.map((item) => ({
|
||||
title: item.snippet.title,
|
||||
thumbnail: item.snippet.thumbnails.default.url,
|
||||
description: item.snippet.description,
|
||||
videoId: item.id.videoId,
|
||||
}));
|
||||
|
||||
const videoIds = videos.map((video) => video.videoId).join(',');
|
||||
const videoStatisticsUrl = `https://www.googleapis.com/youtube/v3/videos?part=statistics&id=${videoIds}&key=${apiKey}`;
|
||||
const videoStatisticsResponse = await axios.get(videoStatisticsUrl);
|
||||
|
||||
const videoStatistics = videoStatisticsResponse.data.items;
|
||||
|
||||
const videosWithStatistics = videos.map((video) => {
|
||||
const videoStatistic = videoStatistics.find((statistic) => statistic.id === video.videoId);
|
||||
|
||||
return {
|
||||
...video,
|
||||
commentCount: videoStatistic.statistics.commentCount,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
videos: videosWithStatistics,
|
||||
totalResults: response.data.pageInfo.totalResults,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default YoutubeService;
|
Loading…
Reference in New Issue