Skip to content

Commit

Permalink
Refactor LoadingOverlay to functional component
Browse files Browse the repository at this point in the history
  • Loading branch information
ewoerner committed Sep 22, 2024
1 parent 6581e83 commit cbbc3e7
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 145 deletions.
129 changes: 30 additions & 99 deletions src/components/loading-overlay/LoadingOverlay.tsx
Original file line number Diff line number Diff line change
@@ -1,115 +1,46 @@
/* eslint-disable i18next/no-literal-string */
import React, { Component, RefObject } from 'react'
import React, { useEffect, useRef } from 'react'
import { CSSTransition } from 'react-transition-group'

import Spinner from './Spinner'
// import STYLES from './styles'
import './loading-overlay.less'

class LoadingOverlay extends Component<
LoadingOverlayProps,
LoadingOverlayState
> {
private wrapper: RefObject<HTMLDivElement>

constructor(props: LoadingOverlayProps) {
super(props)
this.wrapper = React.createRef()
}

// componentDidMount() {
// if (this.wrapper.current) {
// const wrapperStyle = window.getComputedStyle(this.wrapper.current)
// const overflowCSS = ['overflow', 'overflowX', 'overflowY'].reduce(
// (m, i) => {
// if (wrapperStyle[i] !== 'visible') m[i] = 'hidden'
// return m
// },
// {}
// )
// this.setState({ overflowCSS })
// }
// }

componentDidUpdate() {
const { active } = this.props
if (active && this.wrapper.current) this.wrapper.current.scrollTop = 0
}

/**
* Return an emotion css object for a given element key
* If a custom style was provided via props, run it with
* the base css obj.
*/
// getStyles = (key, providedState) => {
// const base = STYLES[key](providedState, this.props)
// const custom = this.props.styles[key]
// if (!custom) return base
// return typeof custom === 'function' ? custom(base, this.props) : custom
// }

/**
* Convenience cx wrapper to add prefix classes to each of the child
* elements for styling purposes.
*/
// cx = (names, ...args) => {
// const arr = Array.isArray(names) ? names : [names]
// return cx(
// ...arr.map((name) =>
// name ? `${this.props.classNamePrefix}${name}` : ''
// ),
// ...args
// )
// }

render() {
const { children, className, active, fadeSpeed, spinner, text } = this.props

return (
<div
data-testid="wrapper"
ref={this.wrapper}
className={`loading-overlay ${className} ${active && 'active'}`}
export const LoadingOverlay: React.FC<LoadingOverlayProps> = ({
active,
text,
}) => {
const ref = useRef<HTMLDivElement>(null)
useEffect(() => {
if (active && ref.current) ref.current.scrollTop = 0
}, [active, ref])
return (
<div
data-testid="wrapper"
ref={ref}
className={`loading-overlay ${active && 'active'}`}
>
<CSSTransition
in={active}
classNames="_loading-overlay-transition"
timeout={500}
unmountOnExit
>
<CSSTransition
in={active}
classNames="_loading-overlay-transition"
timeout={fadeSpeed}
unmountOnExit
>
<div data-testid="overlay" className="overlay">
<div className="content">
<>
{spinner &&
(typeof spinner === 'boolean' ? <Spinner /> : spinner)}
{text}
</>
</div>
<div data-testid="overlay" className="overlay">
<div className="content">
<>
<Spinner />
{text}
</>
</div>
</CSSTransition>
{children}
</div>
)
}
</div>
</CSSTransition>
</div>
)
}

export interface LoadingOverlayProps {
active: boolean
fadeSpeed: number
className?: string
classNamePrefix?: string
spinner: boolean | React.Component
text: React.Component | string
styles?: CSSStyleDeclaration
children?: React.ReactNode
}

interface LoadingOverlayState {}

// LoadingOverlayWrapper.defaultProps = {
// classNamePrefix: '_loading_overlay_',
// fadeSpeed: 500,
// styles: {},
// }

export default LoadingOverlay
9 changes: 1 addition & 8 deletions src/components/login/private-route/PrivateRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ export const PrivateRoute: React.FC<PrivateRouteProps> = ({
{...rest}
render={(props) => {
if (!initialized) {
return (
<LoadingOverlay
active
spinner
text={t('login.checking')}
fadeSpeed={500}
/>
)
return <LoadingOverlay active text={t('login.checking')} />
}
if (authenticated) {
return <Component {...props} admin={admin} />
Expand Down
7 changes: 1 addition & 6 deletions src/components/recent-banners/RecentBanners.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ export const RecentBanners: FC<RecentBannersProps> = ({

return (
<div className="recent-banners">
<LoadingOverlay
active={loading}
spinner
fadeSpeed={500}
text={t('loadingRecent')}
/>
<LoadingOverlay active={loading} text={t('loadingRecent')} />
<div className="recent-banners-title">
<h1>{titleList}</h1>
</div>
Expand Down
7 changes: 1 addition & 6 deletions src/components/search-mission-list/SearchMissionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ const SearchMissionList: FC<SearchMissionListProps> = ({
<Fragment>
{hasMoreMissions && !initial && (
<Col>
<LoadingOverlay
active
spinner
fadeSpeed={500}
text={t('missions.searching')}
/>
<LoadingOverlay active text={t('missions.searching')} />
</Col>
)}
</Fragment>
Expand Down
7 changes: 1 addition & 6 deletions src/components/verify-account/VerifyAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,7 @@ const VerifyAccount: React.FC<VerifyAccountProps> = ({

return (
<div className="account-linking">
<LoadingOverlay
active={loading}
fadeSpeed={300}
spinner
text={t('account.loading')}
/>
<LoadingOverlay active={loading} text={t('account.loading')} />
<div>
<h3>{t('account.linking.title')}</h3>
{!isAccountLinked(currentUser) && <p>{t('account.linking.none')}</p>}
Expand Down
9 changes: 1 addition & 8 deletions src/pages/banner-info/BannerInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,7 @@ class BannerInfo extends React.Component<BannerInfoProps, BannerInfoState> {
)
}
if (status !== 'error') {
return (
<LoadingOverlay
active
spinner
text={i18n!.t('loading')}
fadeSpeed={500}
/>
)
return <LoadingOverlay active text={i18n!.t('loading')} />
}
return (
<Trans i18nKey="banners.noneWithId">No banners found with that id</Trans>
Expand Down
9 changes: 1 addition & 8 deletions src/pages/browser/Browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,7 @@ class Browser extends React.Component<BrowserProps, BrowserState> {
const { filter, selectedPlaceId, status } = this.state

if (status === 'initial') {
return (
<LoadingOverlay
text={i18n!.t('loading')}
fadeSpeed={500}
active
spinner={true}
/>
)
return <LoadingOverlay text={i18n!.t('loading')} active />
}

let administrativeAreas: Array<Place> | null = null
Expand Down
2 changes: 0 additions & 2 deletions src/pages/create-banner/CreateBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,6 @@ class CreateBanner extends React.Component<
<LoadingOverlay
active={status === 'loading'}
text={i18n!.t('banners.creation.preview.generating')}
spinner
fadeSpeed={500}
/>
<h1>{title}</h1>

Expand Down
2 changes: 0 additions & 2 deletions src/pages/preview-banner/PreviewBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ class PreviewBanner extends React.Component<
<LoadingOverlay
active={status === 'loading'}
text={i18n!.t('banners.review.saving')}
spinner
fadeSpeed={500}
/>
<BannerInfoWithMap
banner={banner}
Expand Down

0 comments on commit cbbc3e7

Please sign in to comment.