-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
907 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"> | ||
<div class="demobox"> | ||
<div id="root"></div> | ||
</div> | ||
|
||
<script src="../build/commons.js"></script> | ||
<script src="../build/Youtube.js"></script> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
// import { GridLayout } from "@egjs/react-infinitegrid"; | ||
import { GridLayout } from "../../src/index"; | ||
import getNextPage from "./Youtube/api"; | ||
import Header from "./Youtube/Header"; | ||
import Item from "./Youtube/Item"; | ||
import "./Youtube/index.css"; | ||
|
||
function getItems(items, groupKey, start) { | ||
return items.map((props, i) => ( | ||
<Item groupKey={groupKey} key={`item${start + i}`} {...props} /> | ||
)); | ||
} | ||
const Message = ({ message }) => (message ? <div className="nomore">{message}</div> : null); | ||
|
||
class App extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
list: [], | ||
nextPageToken: "", | ||
query: "", | ||
message: "", | ||
}; | ||
} | ||
onSearch = query => { | ||
document.body.scrollTop = 0; | ||
this.setState({query, list: [], nextPageToken: "", message: ""}); | ||
}; | ||
onAppend = e => { | ||
const groupKey = parseFloat(e.groupKey || 0) + 1; | ||
const pageToken = this.state.nextPageToken; | ||
|
||
if (pageToken === -1) { | ||
return; | ||
} | ||
e.startLoading(); | ||
getNextPage(this.state.query, pageToken).then(result => { | ||
const {nextPageToken, items, error} = result; | ||
const list = this.state.list; | ||
const length = items.length; | ||
const message = | ||
error || | ||
(!nextPageToken && | ||
!list.length && | ||
`No results found for ${this.state.query}`); | ||
|
||
this.setState({ | ||
message, | ||
nextPageToken: (length && nextPageToken) || -1, | ||
list: error ? [] : list.concat(getItems(items, groupKey, list.length)) | ||
}); | ||
(message || !length) && e.endLoading(); | ||
}); | ||
}; | ||
onLayoutComplete = e => { | ||
!e.isLayout && e.endLoading(); | ||
}; | ||
render() { | ||
return ( | ||
<div> | ||
<Header onSearch={this.onSearch} /> | ||
<GridLayout | ||
id="grid" | ||
isEqualSize={true} | ||
align="center" | ||
onAppend={this.onAppend} | ||
loading={<div className="loading">LOADING...</div>} | ||
onLayoutComplete={this.onLayoutComplete} | ||
> | ||
{this.state.list} | ||
</GridLayout> | ||
<Message message={this.state.message} /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
ReactDOM.render(<App />, document.getElementById("root")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
|
||
export default class Header extends React.Component { | ||
search = () => { | ||
const query = this.input.value; | ||
|
||
if (!query) { | ||
this.input.focus(); | ||
return; | ||
} | ||
this.input.blur(); | ||
this.props.onSearch(query); | ||
}; | ||
render() { | ||
return ( | ||
<header className="header"> | ||
<div className="search"> | ||
<div className="search-input"> | ||
<input | ||
type="text" | ||
className="query" | ||
placeholder="Search..." | ||
ref={e => { | ||
e && (this.input = e); | ||
}} | ||
onKeyUp={e => { | ||
if (!this.input || e.keyCode !== 13) { | ||
return; | ||
} | ||
this.search(); | ||
}} | ||
/> | ||
</div> | ||
<button className="submit" onClick={this.search} /> | ||
</div> | ||
<div className="menu"> | ||
<a href="https://github.com/naver/egjs-infinitegrid/tree/master/packages/react-infinitegrid" target="_blank">Github</a> | ||
<a href="https://codesandbox.io/s/935xz7zy8r" target="_blank">Code</a> | ||
<a href="https://github.com/naver/egjs-infinitegrid/wiki/react-infinitegrid-API-documentation" target="_blank">API</a> | ||
</div> | ||
</header> | ||
); | ||
} | ||
componentDidMount() { | ||
const header = ReactDOM.findDOMNode(this); | ||
const search = header.querySelector(".search"); | ||
const menu = header.querySelector(".menu"); | ||
const scroll = e => { | ||
const scrollTop = | ||
window.pageYOffset || | ||
document.body.scrollTop || | ||
document.documentElement.scrollTop; | ||
const height = 300 - Math.min(scrollTop, 220); | ||
const scale = (Math.min(130, height) + 70) / 200; | ||
const opacity = Math.max(0, 220 - scrollTop) / 220; | ||
|
||
header.style.height = `${height}px`; | ||
search.style.transform = `scale(${scale})`; | ||
menu.style.opacity = opacity; | ||
menu.style.display = opacity ? "block" : "none"; | ||
}; | ||
|
||
window.addEventListener("scroll", scroll); | ||
scroll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from "react"; | ||
|
||
const Item = ({ | ||
link, | ||
width, | ||
height, | ||
title, | ||
url, | ||
channelLink, | ||
channelTitle, | ||
}) => (<div className="item"> | ||
<a href={link} target="_blank"> | ||
<span className="image-container"> | ||
<img | ||
data-width={width} | ||
data-height={height} | ||
src={url} | ||
alt="thumbnail" | ||
/> | ||
</span> | ||
<span className="title">{title}</span> | ||
</a> | ||
<a href={channelLink} target="_blank"> | ||
<span className="channel">{channelTitle}</span> | ||
</a> | ||
</div>); | ||
|
||
export default Item; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import fetch from "./mock"; | ||
|
||
function getItems(items) { | ||
return items.filter(item => item.id && item.id.videoId).map(item => { | ||
const {id, snippet} = item; | ||
const {title, thumbnails, channelTitle, channelId} = snippet; | ||
const {width, height, url} = thumbnails.medium; | ||
const link = `https://www.youtube.com/watch?v=${id.videoId}`; | ||
const channelLink = `https://www.youtube.com/channel/${channelId}`; | ||
|
||
return { | ||
link, | ||
width, | ||
height, | ||
url, | ||
title, | ||
channelLink, | ||
channelTitle, | ||
}; | ||
}); | ||
} | ||
// https://developers.google.com/youtube/v3/ | ||
// This key is only allowed on naver.github.io, 935xz7zy8r.codesandbox.io/ | ||
// use mock.js | ||
const key = ""; | ||
|
||
export default function getNextPage(query, pageToken) { | ||
const urls = [ | ||
"https://www.googleapis.com/youtube/v3/search?part=snippet", | ||
`q=${query}`, | ||
`key=${key}`, | ||
`maxResults=30`, | ||
]; | ||
|
||
pageToken && urls.push(`pageToken=${pageToken}`); | ||
|
||
return fetch(urls.join("&")) | ||
.then(e => e.json()) | ||
.then(e => (e.error ? Promise.reject({error: e.error.message}) : e)) | ||
.then(({nextPageToken, items}) => ({ | ||
nextPageToken, | ||
items: getItems(items), | ||
})) | ||
.catch(e => ({error: e.error || "ERR", nextPageToken: -1, items: []})); | ||
} |
Oops, something went wrong.