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

Tidy up home component #571

Merged
merged 6 commits into from
Apr 21, 2019
Merged
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
21 changes: 15 additions & 6 deletions app/components/header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class Header extends Component<Props, State> {
props: Props;

state: State = {
collapsed: true,
searchQuery: ''
};

Expand All @@ -51,24 +52,32 @@ export default class Header extends Component<Props, State> {
}
};

setCollapse = () => {
this.setState(prevState => ({
collapsed: !prevState.collapsed
}));
};

render(): Node {
const { activeMode, setActiveMode } = this.props;
const { searchQuery } = this.state;
const { collapsed, searchQuery } = this.state;

return (
<Navbar className="navbar navbar-expand-lg navbar-dark bg-dark col-sm-12 col-md-12">
<Navbar
className="navbar navbar-expand-lg navbar-dark bg-dark col-sm-12 col-md-12"
sticky="top"
>
<NavbarToggler
className="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-expanded={!collapsed}
aria-label="Toggle navigation"
onClick={this.setCollapse}
/>
<Collapse
className="collapse navbar-collapse"
id="navbarSupportedContent"
isOpen={!collapsed}
>
<Nav className="navbar-nav mr-auto">
<NavItem
Expand Down
78 changes: 30 additions & 48 deletions app/components/home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ export type itemType = {
};

type Props = {
actions: {
setActiveMode: (
mode: string,
activeModeOptions?: activeModeOptionsType
) => void,
paginate: (
activeMode: string,
activeModeOptions?: activeModeOptionsType
) => void,
clearAllItems: () => void,
setLoading: (isLoading: boolean) => void
},
setActiveMode: (
mode: string,
activeModeOptions?: activeModeOptionsType
) => void,
paginate: (
activeMode: string,
activeModeOptions?: activeModeOptionsType
) => void,
clearAllItems: () => void,
setLoading: (isLoading: boolean) => void,
activeMode: string,
activeModeOptions: activeModeOptionsType,
modes: {
Expand Down Expand Up @@ -69,21 +67,10 @@ export default class Home extends Component<Props, State> {

butter: Butter;

didMount: boolean;

// onChange: () => void;

constructor(props: Props) {
super(props);
this.butter = new Butter();

this.onChange = async (isVisible: boolean) => {
const { isLoading, activeMode, activeModeOptions } = this.props;
if (isVisible && !isLoading) {
await this.paginate(activeMode, activeModeOptions);
}
};

// Temporary hack to preserve scroll position
if (!global.pct) {
global.pct = {
Expand All @@ -107,9 +94,9 @@ export default class Home extends Component<Props, State> {
queryType: string,
activeModeOptions: activeModeOptionsType = {}
) {
const { actions, modes } = this.props;
const { modes, paginate, setLoading } = this.props;

actions.setLoading(true);
setLoading(true);

// HACK: This is a temporary solution.
// Waiting on: https://github.com/yannickcr/eslint-plugin-react/issues/818
Expand All @@ -130,16 +117,16 @@ export default class Home extends Component<Props, State> {
}
})();

actions.paginate(items);
actions.setLoading(false);
paginate(items);
setLoading(false);

return items;
}

/**
* If bottom of component is 2000px from viewport, query
*/
initInfinitePagination() {
initInfinitePagination = () => {
const {
infinitePagination,
activeMode,
Expand All @@ -155,18 +142,12 @@ export default class Home extends Component<Props, State> {
this.paginate(activeMode, activeModeOptions);
}
}
}

setUserMeta(type: 'favorites' | 'watchList', item) {
this.setState({
[type]: this.butter[type]('set', item)
});
}
};

async componentDidMount() {
const { activeMode } = this.props;
this.didMount = true;
document.addEventListener('scroll', this.initInfinitePagination.bind(this));

document.addEventListener('scroll', this.initInfinitePagination);
window.scrollTo(0, global.pct[`${activeMode}ScrollTop`]);

const [favorites, watchList, recentlyWatched] = await Promise.all([
Expand All @@ -182,10 +163,8 @@ export default class Home extends Component<Props, State> {
});
}

componentWillMount() {}

componentWillReceiveProps(nextProps: Props) {
const { activeMode, activeModeOptions, actions } = this.props;
const { activeMode, activeModeOptions, clearAllItems } = this.props;
global.pct[`${activeMode}ScrollTop`] = document.body.scrollTop;

if (activeMode !== nextProps.activeMode) {
Expand All @@ -197,7 +176,7 @@ export default class Home extends Component<Props, State> {
JSON.stringify(activeModeOptions)
) {
if (nextProps.activeMode === 'search') {
actions.clearAllItems();
clearAllItems();
}

this.paginate(nextProps.activeMode, nextProps.activeModeOptions);
Expand All @@ -221,15 +200,18 @@ export default class Home extends Component<Props, State> {

global.pct[`${activeMode}ScrollTop`] = document.body.scrollTop;

this.didMount = false;
document.removeEventListener(
'scroll',
this.initInfinitePagination.bind(this)
);
document.removeEventListener('scroll', this.initInfinitePagination);
}

onChange = async (isVisible: boolean) => {
const { isLoading, activeMode, activeModeOptions } = this.props;
if (isVisible && !isLoading) {
await this.paginate(activeMode, activeModeOptions);
}
};

render() {
const { activeMode, actions, items, isLoading } = this.props;
const { activeMode, items, isLoading, setActiveMode } = this.props;
const { favorites, watchList, recentlyWatched } = this.state;

const home = (
Expand All @@ -254,7 +236,7 @@ export default class Home extends Component<Props, State> {

return (
<Row>
<Header activeMode={activeMode} setActiveMode={actions.setActiveMode} />
<Header activeMode={activeMode} setActiveMode={setActiveMode} />
<Col sm="12">
{activeMode === 'home' ? (
home
Expand Down
15 changes: 12 additions & 3 deletions app/containers/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
* @flow
* @TODO: Use waitForImages plugin to load background images and fade in on load
*/
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as HomeActions from '../actions/homePageActions';
import {
clearAllItems,
paginate,
setActiveMode,
setLoading
} from '../actions/homePageActions';
import Home from '../components/home/Home';

function mapStateToProps(state) {
Expand All @@ -21,7 +25,12 @@ function mapStateToProps(state) {

function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(HomeActions, dispatch)
clearAllItems: () => dispatch(clearAllItems()),
paginate: (activeMode, activeModeOptions) =>
dispatch(paginate(activeMode, activeModeOptions)),
setActiveMode: (mode, activeModeOptions) =>
dispatch(setActiveMode(mode, activeModeOptions)),
setLoading: isLoading => dispatch(setLoading(isLoading))
};
}

Expand Down
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "PopcornTime",
"description": "An experimental PopcornTime client",
"homepage": "https://github.com/amilajack/popcorn-time-desktop",
"version": "1.3.2",
"version": "1.3.3",
"main": "./main.prod.js",
"author": {
"name": "Amila Welihinda <[email protected]>",
Expand Down
15 changes: 0 additions & 15 deletions app/styles/utilities/container.scss
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
::-webkit-scrollbar-corner {
background-color: transparent;
}

.row-center {
display: flex;
align-items: center;
}

::-webkit-scrollbar {
z-index: 0;
width: 8px;
height: 8px;
}

::-webkit-scrollbar-thumb {
-webkit-border-radius: 2px;
background: rgba(150, 150, 150, 0.33);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "popcorn-time-desktop",
"version": "1.3.2",
"version": "1.3.3",
"description": "An experimental Popcorn Time client",
"scripts": {
"bench-api": "cross-env NODE_ENV=test BABEL_DISABLE_CACHE=1 jest test/**/*.benchmark.js",
Expand Down