-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate a header for Tribes List to React (#1137)
Also partially migrate the tr-boards directive to React
- Loading branch information
mrkvon
authored
Dec 13, 2019
1 parent
bead1a1
commit 87fd73a
Showing
5 changed files
with
129 additions
and
21 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
import { selectPhoto } from '../services/photos.service'; | ||
|
||
/** | ||
* @param {string[]|string} names - array of names or a single name | ||
* | ||
* @returns {string} the provided name or randomly picked name from array | ||
*/ | ||
function selectName(names) { | ||
return Array.isArray(names) ? names[Math.floor(Math.random() * names.length)] : names; | ||
} | ||
|
||
/** | ||
* Partially migrated tr-boards directive | ||
* modules/core/client/directives/tr-boards.client.directive.js | ||
* | ||
* @TODO implement tr-boards-ignore-small directive | ||
* @TODO implement primary, inset, error and maybe other attributes, which are currently board classes | ||
* and which could become attributes <Board primary inset error names="bokeh" /> | ||
*/ | ||
export default function Board({ names='bokeh', children, onDisplayPhoto=() => {}, onHidePhoto=() => {}, className, ...rest }) { | ||
|
||
const [photo, setPhoto] = useState(null); | ||
|
||
useEffect(() => { | ||
// pick a name from provided names | ||
const selectedName = selectName(names); | ||
// select an appropriate photo by name | ||
const photo = selectPhoto(selectedName); | ||
const photoObject = { [selectedName]: photo }; | ||
|
||
// update the state with the selected photo | ||
setPhoto(photo); | ||
|
||
// inform the parent that the photo is displayed | ||
// ...useful e.g. for displaying photo credits elsewere | ||
onDisplayPhoto(photoObject); | ||
|
||
// inform the parent that the photo is not displayed anymore | ||
return () => onHidePhoto(photoObject); | ||
}, []); | ||
|
||
const style = photo ? { backgroundImage: `url("${photo.imageUrl}")` } : null; | ||
return ( | ||
<section style={style} className={classNames('board', className)} {...rest}> | ||
{children} | ||
</section> | ||
); | ||
} | ||
|
||
Board.propTypes = { | ||
names: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.arrayOf(PropTypes.string) | ||
]).isRequired, | ||
className: PropTypes.string, | ||
children: PropTypes.node, | ||
onDisplayPhoto: PropTypes.func, | ||
onHidePhoto: PropTypes.func | ||
}; |
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
43 changes: 43 additions & 0 deletions
43
modules/tribes/client/components/TribesHeader.component.js
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useTranslation } from 'react-i18next'; | ||
import Board from '@/modules/core/client/components/Board'; | ||
|
||
export default function TribesHeader({ isLoggedIn, onDisplayPhoto, onHidePhoto }) { | ||
|
||
const { t } = useTranslation('tribes'); | ||
|
||
return ( | ||
<Board | ||
names="tribes-1" | ||
className="tribes-header" | ||
onDisplayPhoto={onDisplayPhoto} | ||
onHidePhoto={onHidePhoto} | ||
> | ||
<div className="container"> | ||
<div className="row"> | ||
<div className="col-xs-12 text-center"> | ||
<br /><br /> | ||
<h2>{t('Discover Tribes')}</h2> | ||
<br /> | ||
<p className="lead"> | ||
{t('Joining Tribes helps you find likeminded Trustroots members.')} | ||
</p> | ||
{!isLoggedIn && <div> | ||
<hr className="hr-white hr-xs"/> | ||
<a href="/signup" className="btn btn-action btn-default"> | ||
{t('Sign up with Trustroots')} | ||
</a> | ||
</div>} | ||
</div> | ||
</div> | ||
</div> | ||
</Board> | ||
); | ||
} | ||
|
||
TribesHeader.propTypes = { | ||
isLoggedIn: PropTypes.bool.isRequired, | ||
onDisplayPhoto: PropTypes.func.isRequired, | ||
onHidePhoto: PropTypes.func.isRequired | ||
}; |
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