Skip to content

Commit

Permalink
refactor(PhotoBoard): Convert to functional component
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Aug 10, 2023
1 parent 3f34766 commit ce14aac
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 68 deletions.
129 changes: 63 additions & 66 deletions src/photos/components/PhotoBoard.jsx
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)
4 changes: 2 additions & 2 deletions src/photos/targets/public/__snapshots__/App.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exports[`Public view should render children when they are present 1`] = `
/>
</div>
</div>
<withI18n(WithContentRect)
<WithContentRect
fetchMore={[MockFunction]}
hasMore={false}
lists={
Expand Down Expand Up @@ -97,7 +97,7 @@ exports[`Public view should render the album 1`] = `
/>
</div>
</div>
<withI18n(WithContentRect)
<WithContentRect
fetchMore={[MockFunction]}
hasMore={false}
lists={
Expand Down

0 comments on commit ce14aac

Please sign in to comment.