Created
January 8, 2019 06:42
-
-
Save lsanwick/d06b51f09fdbb8e6b3cf6ab672884b6f to your computer and use it in GitHub Desktop.
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
import React, { Component, Fragment } from "react"; | |
import { shuffle } from "lodash"; | |
import memoize from "memoize-one"; | |
import CardItem from "./CardItem"; | |
export default class CardDeck extends Component { | |
state = { | |
index: 0 | |
}; | |
shuffle = memoize(array => shuffle(array)); | |
onGoToNext = () => this.setState({ index: this.state.index + 1 }); | |
render() { | |
const shuffledCards = this.shuffle(this.props.cards); | |
const item = shuffledCards[this.state.index]; | |
if (this.state.index >= shuffledCards.length) { | |
return ( | |
<div> | |
<h1>Finished</h1> | |
</div> | |
); | |
} else { | |
return ( | |
<Fragment> | |
<h1>{item.front}</h1> | |
<h1>{item.back}</h1> | |
{shuffledCards.map(card => ( | |
<CardItem key={card.id} card={card} /> | |
))} | |
<p> | |
<button onClick={this.onGoToNext}>Next</button> | |
</p> | |
</Fragment> | |
); | |
} | |
} | |
} |
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
import React, { Component, Fragment } from "react"; | |
import CardDeck from "./CardDeck"; | |
const CARDS_QUERY = gql` | |
query CardsQuery($id: ID!) { | |
... | |
} | |
`; | |
export default class Cards extends Component { | |
render() { | |
let { id } = this.props.match.params; | |
id = parseInt(id); | |
return ( | |
<Query query={CARDS_QUERY} variables={{ id }}> | |
{({ data, error, loading }) => { | |
if (loading) { | |
return <Loading />; | |
} | |
if (error) { | |
return <Error />; | |
} | |
return <CardDeck cards={data.deck.cards} />; | |
}} | |
</Query> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment