Skip to content

Commit

Permalink
Migrate list of tribes to React (#1144)
Browse files Browse the repository at this point in the history
+ introduce styled-components (https://www.styled-components.com/)
  • Loading branch information
mrkvon committed Jan 5, 2020
1 parent 612ba30 commit cb50541
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 73 deletions.
2 changes: 1 addition & 1 deletion modules/tribes/client/components/JoinButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function JoinButton({ tribe, user, onUpdated, ...rest }) {

const [isUpdating, setIsUpdating] = useState(false);

const isMemberInitial = user && user.memberIds && user.memberIds.indexOf(tribe._id) > -1;
const isMemberInitial = (user?.memberIds || []).includes(tribe._id);
const [isMember, setIsMember] = useState(isMemberInitial);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { useTranslation } from 'react-i18next';
import styled from 'styled-components';

import JoinButton from './JoinButton';

import getTribeBackgroundStyle from './helpers/getTribeBackgroundStyle';

/**
* @TODO maybe rename to Tribe
*/
export default function TribeItem({ tribe, user, onMembershipUpdated }) {
const Container = styled.div.attrs({
className: 'panel tribe tribe-image',
})`
// the following styles should have high specificity
// https://www.styled-components.com/docs/faqs#how-can-i-override-styles-with-higher-specificity
&&& {
position: relative;
${({ tribe }) => getTribeBackgroundStyle(tribe, { isProgressive: true, dimensions: '742x496' })}
}
`;

export default function Tribe({ tribe, user, onMembershipUpdated }) {
const { t } = useTranslation('tribes');

const countInfo = (tribe.count === 0)
? t('No members yet')
: t('{{count, number}} members', { count: tribe.count });

return <div
className="panel tribe tribe-image"
style={getTribeBackgroundStyle(tribe, { isProgressive: true, dimensions: '742x496' })}
>
return <Container tribe={tribe}>
<a href={`/tribes/${tribe.slug}`} className="tribe-link">
{tribe.new && <span className="tribe-new" aria-hidden={true}>
<span className="label label-primary">
Expand All @@ -40,10 +46,10 @@ export default function TribeItem({ tribe, user, onMembershipUpdated }) {
onUpdated={onMembershipUpdated}
/>}
</div>
</div>;
</Container>;
}

TribeItem.propTypes = {
Tribe.propTypes = {
tribe: PropTypes.object.isRequired,
user: PropTypes.object,
onMembershipUpdated: PropTypes.func.isRequired,
Expand Down
55 changes: 55 additions & 0 deletions modules/tribes/client/components/TribesList.component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';
import PropTypes from 'prop-types';

import styled from 'styled-components';

import Tribe from './Tribe';
import SuggestTribe from './SuggestTribe';

const List = styled.ul.attrs({ className: 'list-unstyled tribes-grid' })`
margin: -5px -5px 15px -5px;
`;

const Item = styled.li`
padding: 5px;
display: inline-block;
vertical-align: bottom;

// on small screens there is just one column of tribes
width: 100%;

@media (min-width: 616px) {
// two columns of tribes
width: 50%;
}

@media (min-width: 992px) {
// three columns of tribes
width: 33.3333%;
}
`;

export default function TribesList({ tribes, user, onMembershipUpdated }) {
return (
<List>
{tribes.map(tribe => (
<Item key={tribe._id}>
<Tribe
tribe={tribe}
user={user}
onMembershipUpdated={onMembershipUpdated}
/>
</Item>
))}
<Item>
<SuggestTribe />
</Item>
</List>
);
}

TribesList.propTypes = {
tribes: PropTypes.array.isRequired,
user: PropTypes.object,
onMembershipUpdated: PropTypes.func.isRequired,
};
23 changes: 6 additions & 17 deletions modules/tribes/client/views/tribes-list.client.view.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,12 @@

<div class="row">
<div class="col-xs-12">

<ul class="list-unstyled tribes-grid"
angular-grid="tribesList.tribes"
ag-grid-width="288"
ag-gutter-size="10"
ag-refresh-on-img-load="false">
<li ng-repeat="tribe in tribesList.tribes track by tribe._id">
<tribe-item
tribe="tribe"
user="app.user"
onMembershipUpdated="tribesList.broadcastChange"
></tribe-item>
</li>
<li>
<suggest-tribe></suggest-tribe>
</li>
</ul>
<tribes-list
tribes="tribesList.tribes"
user="app.user"
onMembershipUpdated="tribesList.broadcastChange"
></tribes-list>
</div>
</div>

<tribes-join-trustroots ng-if="!app.user">
Expand Down
Loading

0 comments on commit cb50541

Please sign in to comment.