-
Notifications
You must be signed in to change notification settings - Fork 45
Chantelle | Edges | Inspiration Board #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
bbef1a9
278460f
f4258bb
e06f80e
9ff1875
a70f8ef
16eb658
9aeb9ca
49f6fdf
f7985f3
e386db0
5a79a49
0ea0c4f
df45241
c92e5bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,108 @@ | ||
| import React, { Component } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import axios from 'axios'; | ||
|
|
||
| import './Board.css'; | ||
| import Card from './Card'; | ||
| import NewCardForm from './NewCardForm'; | ||
| import CARD_DATA from '../data/card-data.json'; | ||
| // import CARD_DATA from '../data/card-data.json'; | ||
|
|
||
| class Board extends Component { | ||
| constructor() { | ||
| super(); | ||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this.state = { | ||
| cards: [], | ||
| url: this.props.url, | ||
| boardName: this.props.boardName, | ||
| error: undefined | ||
| }; | ||
| } | ||
| componentDidMount( ) { | ||
| const allCardsURL = this.state.url + 'boards/' + this.state.boardName + 'cards/'; | ||
|
|
||
| render() { | ||
| return ( | ||
| <div> | ||
| Board | ||
| </div> | ||
| ) | ||
| axios.get(allCardsURL) | ||
| .then((response) => { | ||
|
|
||
| this.setState({ | ||
| cards: response.data | ||
| }) | ||
| }) | ||
| .catch((error)=> { | ||
| const errorStr = `Got an error with status ${error.response.status} and message ${error.response.statusText}` | ||
|
|
||
| this.setState({ | ||
| error: errorStr | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| deleteCard = (id) => { | ||
|
|
||
| const deleteURL = `${this.state.url}cards/${id}` | ||
| axios.delete(deleteURL) | ||
| .then((response) => { | ||
| const card = response.data.card; | ||
| alert(`Successfully deleted card with id ${card.id} and message ${card.text}.`) | ||
|
|
||
| const updatedCards = this.state.cards.filter((card) => { | ||
| return card.card.id !== id | ||
| }) | ||
|
|
||
| this.setState({ | ||
| cards: updatedCards | ||
| }) | ||
| }) | ||
| .catch((error) => { | ||
| const errorStr = `Got an error with status ${error.response.status} and message ${error.response.statusText}` | ||
| this.setState({ | ||
| error: errorStr | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| } | ||
| addCard = (cardObj) => { | ||
|
|
||
| Board.propTypes = { | ||
| let URL = `${this.state.url}boards/${this.state.boardName}cards`; | ||
|
|
||
| axios.post(URL, cardObj) | ||
| .then((response) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that you've kept all the API interaction logic in one place - the callbacks are a little more complex, but I would say it makes the app as a whole much easier to comprehend. Whether or not you intended it, this is a great example of the container component pattern well-applied. |
||
| const updatedCards = this.state.cards; | ||
| updatedCards.push({card: response.data.card}); | ||
| console.log(updatedCards, "response", response.data) | ||
| this.setState({ | ||
| cards: updatedCards | ||
| }) | ||
| }) | ||
| .catch((error) => { | ||
| const errorStr = `Got an error with status ${error.response.status} and message ${error.response.statusText}` | ||
| this.setState({ | ||
| error: errorStr | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| render() { | ||
|
|
||
| const cards = this.state.cards.map((card) => { | ||
| return (<Card key={`${card.card.id}${card.card.text}${card.card.emoji}`} | ||
| text={card.card.text} emoji={card.card.emoji} id={card.card.id} deleteCardCallback={this.deleteCard}/>) | ||
| }) | ||
|
|
||
| const display = (this.state.cards.length !== 0) ? (<section>{cards}</section>) | ||
| : (<p>{this.state.error}</p>) ; | ||
|
|
||
| return ( | ||
| <div> | ||
| <NewCardForm addCardCallback={this.addCard}/> | ||
| {display} | ||
| </div> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| }; | ||
| Board.propTypes = { | ||
| url: PropTypes.string.isRequired, | ||
| boardName: PropTypes.string.isRequired | ||
| }; | ||
|
|
||
| export default Board; | ||
| export default Board; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,35 @@ | ||
| import React, { Component } from 'react'; | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import emoji from 'emoji-dictionary'; | ||
|
|
||
| import './Card.css'; | ||
|
|
||
| class Card extends Component { | ||
| render() { | ||
| return ( | ||
| <div className="card"> | ||
| Card | ||
| </div> | ||
| ) | ||
|
|
||
| const Card = (props) => { | ||
|
|
||
| const { text, emoji, id, deleteCardCallback} = props; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that you converted this to a functional component! |
||
| const onDelete = () => { | ||
| deleteCardCallback(id); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="card"> | ||
| <ul> | ||
| <li> {text} </li> | ||
| <li> {emoji} </li> | ||
| <li><button onClick={onDelete}>Uninspired...</button></li> | ||
| </ul> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| Card.propTypes = { | ||
|
|
||
| text: PropTypes.string.isRequired, | ||
| emoji: PropTypes.string, | ||
| id: PropTypes.number, | ||
| deleteCardCallback: PropTypes.func | ||
| }; | ||
|
|
||
|
|
||
| export default Card; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import React from 'react'; | ||
| import Card from '../Card'; | ||
| import { shallow } from 'enzyme'; | ||
|
|
||
| describe('Card', () => { | ||
| test('that it matches an existing snapshot', () => { | ||
| // First Mount the Component in the testing DOM | ||
| // Arrange | ||
| const wrapper = shallow( < | ||
| Card text="text" | ||
| emoji="emoji" | ||
| id={0} | ||
| deleteCardCallback={() => {} }/>); | ||
|
|
||
| // Assert that it looks like the last snapshot | ||
| expect(wrapper).toMatchSnapshot(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import React from 'react'; | ||
| import NewCardForm from '../NewCardForm'; | ||
| import { shallow } from 'enzyme'; | ||
|
|
||
| describe('NewCardForm', () => { | ||
| test('that it matches an existing snapshot', () => { | ||
| // First Mount the Component in the testing DOM | ||
| // Arrange | ||
| const wrapper = shallow( | ||
| <NewCardForm addCardCallback={() => {} } /> | ||
| ); | ||
|
|
||
| // Assert that it looks like the last snapshot | ||
| expect(wrapper).toMatchSnapshot(); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should only include things in
stateif they're going to change as your application runs. As far as I can tellurlandboardNamedon't change, which means it would be clearer to leave them inprops