-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(PhotoBoard): Convert to functional component
- Loading branch information
Showing
2 changed files
with
65 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,78 @@ | ||
import styles from '../styles/photoList.styl' | ||
|
||
import React, { Component } from 'react' | ||
import React from 'react' | ||
import { withContentRect } from 'react-measure' | ||
import { translate } from 'cozy-ui/transpiled/react/I18n' | ||
import { useI18n } from 'cozy-ui/transpiled/react/I18n' | ||
|
||
import PhotoList from './PhotoList' | ||
import { EmptyPhotos } from 'components/Error/Empty' | ||
import Loading from './Loading' | ||
import ErrorComponent from 'components/Error/ErrorComponent' | ||
import LoadMoreButton from './LoadMoreButton' | ||
|
||
export class PhotoBoard extends Component { | ||
render() { | ||
const { | ||
t, | ||
f, | ||
lists, | ||
selected, | ||
photosContext, | ||
showSelection, | ||
onPhotoToggle, | ||
onPhotosSelect, | ||
onPhotosUnselect, | ||
fetchStatus, | ||
hasMore, | ||
fetchMore, | ||
measureRef, | ||
contentRect: { | ||
entry: { width } | ||
}, | ||
lastFetch | ||
} = this.props | ||
const isError = fetchStatus === 'failed' | ||
const isFetching = | ||
(fetchStatus === 'pending' || fetchStatus === 'loading') && !lastFetch | ||
const PhotoBoard = ({ | ||
lists, | ||
selected, | ||
photosContext, | ||
showSelection, | ||
onPhotoToggle, | ||
onPhotosSelect, | ||
onPhotosUnselect, | ||
fetchStatus, | ||
hasMore, | ||
fetchMore, | ||
measureRef, | ||
contentRect: { | ||
entry: { width } | ||
}, | ||
lastFetch | ||
}) => { | ||
const { t, f } = useI18n() | ||
|
||
if (isError) { | ||
return <ErrorComponent errorType={`${photosContext}_photos`} /> | ||
} | ||
if (isFetching) { | ||
return <Loading loadingType="photos_fetching" /> | ||
} | ||
if (!isFetching && (lists.length === 0 || lists[0].photos.length === 0)) { | ||
return <EmptyPhotos localeKey={`${photosContext}_photos`} /> | ||
} | ||
const isError = fetchStatus === 'failed' | ||
const isFetching = | ||
(fetchStatus === 'pending' || fetchStatus === 'loading') && !lastFetch | ||
|
||
return ( | ||
<div | ||
className={showSelection ? styles['pho-list-selection'] : ''} | ||
ref={measureRef} | ||
> | ||
{lists.map((photoList, idx) => ( | ||
<PhotoList | ||
key={idx} | ||
title={ | ||
photoList.title || | ||
(photoList.month ? f(photoList.month, 'MMMM YYYY') : '') | ||
} | ||
photos={photoList.photos} | ||
selected={selected} | ||
showSelection={showSelection} | ||
onPhotoToggle={onPhotoToggle} | ||
onPhotosSelect={onPhotosSelect} | ||
onPhotosUnselect={onPhotosUnselect} | ||
containerWidth={width} | ||
/> | ||
))} | ||
{hasMore && ( | ||
<LoadMoreButton | ||
label={t('Board.load_more')} | ||
width={width} | ||
onClick={fetchMore} | ||
/> | ||
)} | ||
</div> | ||
) | ||
if (isError) { | ||
return <ErrorComponent errorType={`${photosContext}_photos`} /> | ||
} | ||
if (isFetching) { | ||
return <Loading loadingType="photos_fetching" /> | ||
} | ||
if (!isFetching && (lists.length === 0 || lists[0].photos.length === 0)) { | ||
return <EmptyPhotos localeKey={`${photosContext}_photos`} /> | ||
} | ||
|
||
return ( | ||
<div | ||
className={showSelection ? styles['pho-list-selection'] : ''} | ||
ref={measureRef} | ||
> | ||
{lists.map((photoList, idx) => ( | ||
<PhotoList | ||
key={idx} | ||
title={ | ||
photoList.title || | ||
(photoList.month ? f(photoList.month, 'MMMM YYYY') : '') | ||
} | ||
photos={photoList.photos} | ||
selected={selected} | ||
showSelection={showSelection} | ||
onPhotoToggle={onPhotoToggle} | ||
onPhotosSelect={onPhotosSelect} | ||
onPhotosUnselect={onPhotosUnselect} | ||
containerWidth={width} | ||
/> | ||
))} | ||
{hasMore && ( | ||
<LoadMoreButton | ||
label={t('Board.load_more')} | ||
width={width} | ||
onClick={fetchMore} | ||
/> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default translate()(withContentRect()(PhotoBoard)) | ||
export default withContentRect()(PhotoBoard) |
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