-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: use internal-apis for subscriptions and add page loader
- Loading branch information
Sean Yesmunt
committed
May 7, 2018
1 parent
f6afb2e
commit 1fe95ce
Showing
15 changed files
with
415 additions
and
195 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,98 @@ | ||
// @flow | ||
import * as React from 'react'; | ||
import classnames from 'classnames'; | ||
import Spinner from 'component/common/spinner'; | ||
import { isShowingChildren } from 'util/dom'; | ||
|
||
// time in ms to wait to show loading spinner | ||
const LOADER_TIMEOUT = 1500; | ||
|
||
type Props = { | ||
children: React.Node, | ||
children: React.Node | Array<React.Node>, | ||
pageTitle: ?string, | ||
noPadding: ?boolean, | ||
extraPadding: ?boolean, | ||
notContained: ?boolean, // No max-width, but keep the padding | ||
loading: ?boolean, | ||
}; | ||
|
||
const Page = (props: Props) => { | ||
const { pageTitle, children, noPadding, extraPadding, notContained } = props; | ||
return ( | ||
<main | ||
className={classnames('main', { | ||
'main--contained': !notContained && !noPadding && !extraPadding, | ||
'main--no-padding': noPadding, | ||
'main--extra-padding': extraPadding, | ||
})} | ||
> | ||
{pageTitle && ( | ||
<div className="page__header"> | ||
{pageTitle && <h1 className="page__title">{pageTitle}</h1>} | ||
</div> | ||
)} | ||
{children} | ||
</main> | ||
); | ||
type State = { | ||
showLoader: ?boolean, | ||
}; | ||
|
||
class Page extends React.PureComponent<Props, State> { | ||
static getDerivedStateFromProps(nextProps: Props, prevState: State) { | ||
const { children } = nextProps; | ||
const { showLoader } = prevState; | ||
|
||
// If we aren't showing the loader, don't bother updating | ||
if (!showLoader) { | ||
return null; | ||
} | ||
|
||
if (isShowingChildren(children)) { | ||
return { | ||
showLoader: false, | ||
}; | ||
} | ||
return null; | ||
} | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.state = { | ||
showLoader: false, | ||
}; | ||
|
||
this.loaderTimeout = null; | ||
} | ||
|
||
componentDidMount() { | ||
const { children } = this.props; | ||
|
||
if (!isShowingChildren(children)) | ||
this.loaderTimeout = setTimeout(() => { | ||
this.setState({ showLoader: true }); | ||
}, LOADER_TIMEOUT); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.loaderTimeout = null; | ||
} | ||
|
||
loaderTimeout: ?number; | ||
|
||
render() { | ||
const { pageTitle, children, noPadding, extraPadding, notContained, loading } = this.props; | ||
const { showLoader } = this.state; | ||
|
||
// We don't want to show the loading spinner right away if it will only flash on the | ||
// screen for a short time, wait until we know it will be loading for a bit before showing it | ||
const shouldShowLoader = !isShowingChildren(children) && showLoader; | ||
|
||
return ( | ||
<main | ||
className={classnames('main', { | ||
'main--contained': !notContained && !noPadding && !extraPadding, | ||
'main--no-padding': noPadding, | ||
'main--extra-padding': extraPadding, | ||
})} | ||
> | ||
{pageTitle && ( | ||
<div className="page__header"> | ||
{pageTitle && <h1 className="page__title">{pageTitle}</h1>} | ||
</div> | ||
)} | ||
{!loading && children} | ||
{shouldShowLoader && ( | ||
<div className="page__empty"> | ||
<Spinner /> | ||
</div> | ||
)} | ||
</main> | ||
); | ||
} | ||
} | ||
|
||
export default Page; |
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 |
---|---|---|
@@ -1,27 +1,25 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { | ||
selectSubscriptionsFromClaims, | ||
selectSubscriptionClaims, | ||
selectSubscriptions, | ||
selectHasFetchedSubscriptions, | ||
selectSubscriptionsBeingFetched, | ||
selectIsFetchingSubscriptions, | ||
selectNotifications, | ||
} from 'redux/selectors/subscriptions'; | ||
import { doFetchClaimsByChannel } from 'redux/actions/content'; | ||
import { | ||
setHasFetchedSubscriptions, | ||
setSubscriptionNotifications, | ||
} from 'redux/actions/subscriptions'; | ||
import { setSubscriptionNotifications, doFetchMySubscriptions } from 'redux/actions/subscriptions'; | ||
import SubscriptionsPage from './view'; | ||
|
||
const select = state => ({ | ||
hasFetchedSubscriptions: state.subscriptions.hasFetchedSubscriptions, | ||
savedSubscriptions: selectSubscriptions(state), | ||
subscriptions: selectSubscriptionsFromClaims(state), | ||
isFetchingSubscriptions: selectIsFetchingSubscriptions(state), | ||
subscriptionsBeingFetched: selectSubscriptionsBeingFetched(state), | ||
subscriptions: selectSubscriptions(state), | ||
subscriptionClaims: selectSubscriptionClaims(state), | ||
notifications: selectNotifications(state), | ||
}); | ||
|
||
export default connect(select, { | ||
doFetchClaimsByChannel, | ||
setHasFetchedSubscriptions, | ||
setSubscriptionNotifications, | ||
doFetchMySubscriptions, | ||
})(SubscriptionsPage); |
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
Oops, something went wrong.