Skip to content

Commit

Permalink
Support tag searching
Browse files Browse the repository at this point in the history
  • Loading branch information
kokororin committed Jun 28, 2018
1 parent 7e1f2c5 commit 8e14523
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 116 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"react-css-modules": "^4.7.1",
"react-document-title": "^2.0.3",
"react-dom": "^16.4.1",
"react-event-listener": "^0.6.1",
"react-ga": "^2.1.2",
"react-image": "^1.0.1",
"react-intl": "^2.4.0",
Expand Down
29 changes: 18 additions & 11 deletions src/actions/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export const types = namespacedTypes('gallery', [
'SET_PAGE',
'SET_FETCH_ERROR',
'SET_FETCH_STATUS',
'SET_TAG',
'SET_WORD',
'CLEAR_SOURCE',
'SET_ERROR_TIMES',
'CLEAR_ERROR_TIMES'
'CLEAR_ERROR_TIMES',
'SET_FROM_ILLUST'
]);

export function setItems(data) {
Expand Down Expand Up @@ -67,10 +68,10 @@ function fetchSource() {
return (dispatch, getState) => {
dispatch(setFetchError(false));
dispatch(setFetchStatus(true));
if (getState().gallery.tag === 'ranking') {
if (getState().gallery.word === 'ranking') {
return cachedFetch(`${config.apiBaseURL}${config.rankingURI}`, {
mode: 'cors',
timeout: 10e3,
timeout: 30e3,
data: {
page: getState().gallery.page
}
Expand All @@ -95,12 +96,11 @@ function fetchSource() {
dispatch(setFetchError(true));
});
}
return cachedFetch(`${config.apiBaseURL}${config.galleryURI}`, {
return cachedFetch(`${config.apiBaseURL}${config.searchURI}`, {
mode: 'cors',
timeout: 10e3,
expiryKey: 'expires_at',
timeout: 30e3,
data: {
tag: getState().gallery.tag,
word: getState().gallery.word,
page: getState().gallery.page
}
})
Expand Down Expand Up @@ -134,11 +134,11 @@ export function fetchSourceIfNeeded() {
};
}

export function setTag(tag) {
export function setWord(word) {
return {
type: types.SET_TAG,
type: types.SET_WORD,
payload: {
tag
word
}
};
}
Expand All @@ -152,3 +152,10 @@ export function clearSource() {
}
};
}

export function setFromIllust(fromIllust) {
return {
type: types.SET_FROM_ILLUST,
payload: fromIllust
};
}
21 changes: 12 additions & 9 deletions src/components/InfiniteScroll.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import EventListener from 'react-event-listener';

import ScrollContext from '@/components/ScrollContext';

Expand All @@ -15,14 +16,6 @@ export default class InfiniteScroll extends React.Component {
super(props);
}

componentDidMount() {
this.scrollingElement.addEventListener('scroll', this.onScroll);
}

componentWillUnmount() {
this.scrollingElement.removeEventListener('scroll', this.onScroll);
}

@autobind
onScroll(event) {
if (this.props.isLoading) {
Expand All @@ -48,6 +41,16 @@ export default class InfiniteScroll extends React.Component {
}

render() {
return this.props.children;
return (
<React.Fragment>
{this.props.children}
{this.scrollingElement && (
<EventListener
target={this.scrollingElement}
onScroll={this.onScroll}
/>
)}
</React.Fragment>
);
}
}
15 changes: 7 additions & 8 deletions src/components/ScrollContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import styles from '@/styles/Base.scss';

import React from 'react';
import { withRouter } from 'react-router-dom';
import EventListener, { withOptions } from 'react-event-listener';

@withRouter
export default class ScrollContext extends React.Component {
Expand All @@ -16,10 +17,6 @@ export default class ScrollContext extends React.Component {
super(props);
}

componentDidMount() {
document.addEventListener('scroll', this.onScroll, true);
}

componentDidUpdate(prevProps) {
if (this.props.location === prevProps.location) {
return;
Expand All @@ -30,10 +27,6 @@ export default class ScrollContext extends React.Component {
}
}

componentWillUnmount() {
document.removeEventListener('scroll', this.onScroll, true);
}

@autobind
onScroll(event) {
if (event.target.className === ScrollContext.scrollingClassName) {
Expand All @@ -56,6 +49,12 @@ export default class ScrollContext extends React.Component {
<div className={styles['scroll-context-inner-container']}>
{this.props.children}
</div>
<EventListener
target={document}
onScroll={withOptions(this.onScroll, {
capture: true
})}
/>
</div>
);
}
Expand Down
46 changes: 46 additions & 0 deletions src/components/SearchInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import styles from '@/styles/Gallery.scss';

import React from 'react';
import PropTypes from 'prop-types';
import CSSModules from 'react-css-modules';
import EventListener from 'react-event-listener';
import SearchIcon from '@material-ui/icons/Search';

@CSSModules(styles, { allowMultiple: true })
export default class SearchInput extends React.Component {
static propTypes = {
onSearch: PropTypes.func
};

static defaultProps = {
onSearch() {}
};

constructor(props) {
super(props);
}

@autobind
onKeyDown(event) {
if (event.keyCode === 13 && document.activeElement === this.inputRef) {
this.inputRef.blur();
this.onSearch();
}
}

onSearch() {
this.props.onSearch(this.inputRef.value);
}

render() {
return (
<div styleName="search-root">
<div styleName="search">
<SearchIcon />
</div>
<input ref={ref => (this.inputRef = ref)} styleName="search-input" />
<EventListener target="window" onKeyDown={this.onKeyDown} />
</div>
);
}
}
1 change: 1 addition & 0 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export default {
apiBaseURL: 'https://api.pixiv.moe/v1',
galleryURI: '/gallery',
rankingURI: '/ranking',
searchURI: '/search',
illustURI: '/illust',
commentsURI: '/illust/comments',
authURI: '/user/auth',
Expand Down
Loading

0 comments on commit 8e14523

Please sign in to comment.