Skip to content

Commit

Permalink
Merge branch 'main' of github.com:wordbots/wordbots-core into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNisnevich committed Jan 17, 2022
2 parents a4444b7 + 7e21dd6 commit 1f86742
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 32 deletions.
37 changes: 14 additions & 23 deletions src/common/components/cards/RecentCardsCarousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@ interface RecentCardsCarouselState {
width?: number
}

const duplicateCardsUntilCarouselFull = (cards: w.CardInStore[]): w.CardInStore[] => {
if (cards.length < RecentCardsCarousel.MAX_CARDS_TO_SHOW && cards.length > 0) {
while (cards.length < RecentCardsCarousel.MAX_CARDS_TO_SHOW) {
cards = [...cards, ...cards];
}
}
return cards;
};

export default class RecentCardsCarousel extends React.Component<RecentCardsCarouselProps, RecentCardsCarouselState> {
public static MAX_CARDS_TO_SHOW = 8;

public state: RecentCardsCarouselState = {
recentCards: this.props.cardsToShow || [],
recentCards: this.props.cardsToShow ? duplicateCardsUntilCarouselFull(this.props.cardsToShow) : [],
paused: false
};

Expand Down Expand Up @@ -68,20 +77,6 @@ export default class RecentCardsCarousel extends React.Component<RecentCardsCaro
if (recentCards.length > 0) {
return (
<div>
{!this.props.cardsToShow && (
<div
style={{
marginTop: 10,
color: '#999',
fontSize: 20,
fontWeight: 'bold',
textTransform: 'uppercase',
textAlign: 'center'
}}
>
Most recently created cards
</div>
)}
<div
className="recentCardsCarousel"
onMouseOver={this.handleMouseOver}
Expand Down Expand Up @@ -132,15 +127,11 @@ export default class RecentCardsCarousel extends React.Component<RecentCardsCaro
}

private initializeCarousel = async (userId?: string) => {
let recentCards = await mostRecentCards(userId || null, 15);

if (recentCards.length < RecentCardsCarousel.MAX_CARDS_TO_SHOW && recentCards.length > 0) {
while (recentCards.length < RecentCardsCarousel.MAX_CARDS_TO_SHOW) {
recentCards = [...recentCards, ...recentCards];
}
}
const recentCards = await mostRecentCards(userId || null, 15);

this.setState({ recentCards });
this.setState({
recentCards: duplicateCardsUntilCarouselFull(recentCards)
});
}

private handleClickCard = (card: w.CardInStore) => {
Expand Down
16 changes: 15 additions & 1 deletion src/common/containers/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,21 @@ class Home extends React.Component<HomeProps> {
</SplashSection>
</div>

<RecentCardsCarousel history={history} />
<div>
<div
style={{
marginTop: 20,
color: '#999',
fontSize: 20,
fontWeight: 'bold',
textTransform: 'uppercase',
textAlign: 'center'
}}
>
Most recently created cards
</div>
<RecentCardsCarousel history={history} />
</div>

{
!isFlagSet('skipNewHere') &&
Expand Down
69 changes: 62 additions & 7 deletions src/common/containers/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Helmet from 'react-helmet';
import { RouteComponentProps } from 'react-router';
import { withRouter } from 'react-router-dom';

import CardGrid from '../components/cards/CardGrid';
import RecentCardsCarousel from '../components/cards/RecentCardsCarousel';
import Title from '../components/Title';
import MatchmakingInfo from '../components/users/profile/MatchmakingInfo';
Expand All @@ -21,15 +22,18 @@ import {
getNumSetsCreatedCountByUserId,
getGamesByUser,
getUserNamesByIds,
setStatistic
setStatistic,
mostRecentCards
} from '../util/firebase';

type ProfileProps = RouteComponentProps<{ userId: string }> & WithStyles;

interface ProfileState {
cardsDisplayMode: 'recent' | 'all'
userId?: string
userName?: string
games?: w.SavedGame[]
cards?: w.CardInStore[]
playerNames?: Record<string, string>
playerInfo?: {
cardsCreated: number
Expand Down Expand Up @@ -59,7 +63,9 @@ const styles: Record<string, CSSProperties> = {
};

class Profile extends React.Component<ProfileProps, ProfileState> {
public state: ProfileState = {};
public state: ProfileState = {
cardsDisplayMode: 'recent'
};

public async componentDidMount(): Promise<void> {
this.loadProfileData();
Expand All @@ -79,8 +85,8 @@ class Profile extends React.Component<ProfileProps, ProfileState> {
}

public render(): JSX.Element {
const { classes } = this.props;
const { games, playerNames, playerInfo, userId, userName } = this.state;
const { classes, history } = this.props;
const { cardsDisplayMode, games, cards, playerNames, playerInfo, userId, userName } = this.state;

const title = userName ? `${userName}'s Profile` : 'Profile';

Expand Down Expand Up @@ -108,14 +114,59 @@ class Profile extends React.Component<ProfileProps, ProfileState> {
/>
</Paper>
</div>
<div className={classes.recentCardsContainer}>
{userId && <RecentCardsCarousel key={userId} userId={userId} history={this.props.history} />}
</div>
{(userId && cards) && (
<div key={userId} className={classes.recentCardsContainer}>
<div
style={{
margin: 10,
color: '#999',
fontSize: 20,
fontWeight: 'bold',
textTransform: 'uppercase',
textAlign: 'center'
}}
>
{ cardsDisplayMode === 'recent' ? 'Most recently created cards ' : 'All created cards ' }
<a
className="underline"
onClick={this.toggleCardsDisplayMode}
style={{
fontSize: 14,
textTransform: 'none'
}}
>
{ cardsDisplayMode === 'recent' ? '[show all cards]' : '[show most recent cards]' }
</a>
</div>
{
cardsDisplayMode === 'recent'
? <RecentCardsCarousel cardsToShow={cards.slice(0, 15)} history={history} />
: (
<CardGrid
cards={cards || []}
selectedCardIds={[]}
selectable={false}
onCardClick={this.handleClickCard}
/>
)
}
</div>
)}
</div>
</div>
);
}

private toggleCardsDisplayMode = (): void => {
this.setState(({ cardsDisplayMode }) => ({
cardsDisplayMode: cardsDisplayMode === 'all' ? 'recent' : 'all'
}));
}

private handleClickCard = (cardId: w.CardId) => {
this.props.history.push(`/card/${cardId}`);
}

private async loadProfileData(): Promise<void> {
try {
const { userId } = this.props.match.params;
Expand All @@ -129,6 +180,10 @@ class Profile extends React.Component<ProfileProps, ProfileState> {
const games = await getGamesByUser(userId);
this.loadGamesData(games);
this.loadPlayerInfoData(userId, games);

this.setState({
cards: await mostRecentCards(userId)
});
} catch (error) {
// Most likely reason is that userId is undefined or that user doesn't exist.
console.error(error); // eslint-disable-line no-console
Expand Down
2 changes: 1 addition & 1 deletion src/common/util/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export async function getCardById(cardId: string): Promise<w.CardInStore> {
return snapshot.val() as w.CardInStore;
}

export async function mostRecentCards(uid: w.UserId | null, limit: number): Promise<w.CardInStore[]> {
export async function mostRecentCards(uid: w.UserId | null, limit = 9999): Promise<w.CardInStore[]> {
const cards = await getCards(uid);

return flow(
Expand Down

0 comments on commit 1f86742

Please sign in to comment.