-
Notifications
You must be signed in to change notification settings - Fork 43
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
Sam Berk -- Octos #26
base: master
Are you sure you want to change the base?
Changes from all commits
d7f796b
d64e51b
fc0c895
44cc23b
eb211b9
2da0790
d03cde8
6c1266e
ea9e54e
ef274df
58adc58
458f34d
759e69b
679800a
5bab210
ffbb398
8ff8c39
4aca7c3
16b2b1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,24 +10,110 @@ import CARD_DATA from '../data/card-data.json'; | |
class Board extends Component { | ||
constructor() { | ||
super(); | ||
|
||
this.state = { | ||
cards: [], | ||
boardName: null | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.props.updateStatusCallback(`Loading board: ${this.props.boardName}`, 'success'); | ||
axios.get(`${this.props.url}${this.props.boardName}/cards`) | ||
.then((response) => { | ||
this.props.updateStatusCallback(`Board "${this.props.boardName}" successfully loaded` , 'success'); | ||
this.setState({ | ||
cards: response.data, | ||
boardName: this.props.boardName | ||
}); | ||
}) | ||
|
||
.catch((error) => { | ||
this.props.updateStatusCallback(error.message, 'error'); | ||
}); | ||
} | ||
|
||
componentDidUpdate(prevProps, prevState) { | ||
// debugger | ||
if(prevProps.boardName !== this.props.boardName) { | ||
this.props.updateStatusCallback(`Loading board: ${this.props.boardName}`, 'success'); | ||
axios.get(`${this.props.url}${this.props.boardName}/cards`) | ||
.then((response) => { | ||
this.props.updateStatusCallback(`Board "${this.props.boardName}" successfully loaded` , 'success'); | ||
this.setState({ | ||
cards: response.data, | ||
boardName: this.props.boardName | ||
}); | ||
}) | ||
|
||
.catch((error) => { | ||
this.props.updateStatusCallback(error.message, 'error'); | ||
}); | ||
} | ||
} | ||
|
||
addCard = (newCard) => { | ||
axios.post(`${this.props.url}${this.props.boardName}/cards`, newCard) | ||
.then((response) => { | ||
this.props.updateStatusCallback('New card successfully added', 'success'); | ||
|
||
let updatedCards = this.state.cards; | ||
updatedCards.push({ | ||
card: response.data.card | ||
}); | ||
|
||
this.setState({ | ||
cards: updatedCards | ||
}); | ||
}) | ||
.catch((error) => { | ||
this.props.updateStatusCallback('Card could not be added', 'error'); | ||
}); | ||
} | ||
|
||
deleteCard = (id) => { | ||
axios.delete(`${this.props.url}${this.props.boardName}/cards/${id}`) | ||
.then(() => { | ||
let updatedCards = this.state.cards; | ||
updatedCards.splice(id, 1); | ||
this.setState({ | ||
cards: updatedCards | ||
}); | ||
window.location.reload(); | ||
}) | ||
.catch((error) => { | ||
this.props.updateStatusCallback(`Card could not be deleted: ${error.message}`, 'error'); | ||
}); | ||
} | ||
|
||
render() { | ||
const cards = this.state.cards.map((card, index) => { | ||
return ( | ||
<div> | ||
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. Since this function is returning a collection of Cards, you need to include a key attribute. Something like: <div key={index}> |
||
<Card | ||
key={index} | ||
id={card.card.id} | ||
text={card.card.text} | ||
emoji={card.card.emoji} | ||
deleteCardCallback={this.deleteCard} | ||
/> | ||
</div> | ||
); | ||
}); | ||
|
||
return ( | ||
<div> | ||
Board | ||
<div className='board'> | ||
{ cards } | ||
<NewCardForm addCardCallback={this.addCard}/> | ||
</div> | ||
) | ||
} | ||
|
||
} | ||
|
||
Board.propTypes = { | ||
|
||
url: PropTypes.string.isRequired, | ||
boardName: PropTypes.string.isRequired, | ||
updateStatusCallback: PropTypes.func.isRequired | ||
}; | ||
|
||
export default Board; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { shallow } from 'enzyme'; | ||
import Board from './Board'; | ||
|
||
describe('Board', () => { | ||
test('it will match the last snapshot', () => { | ||
const board = shallow( | ||
<Board url="" boardName="" updateStatusCallback={()=>{}}/> | ||
); | ||
|
||
expect(board).toMatchSnapshot(); | ||
|
||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,17 +5,37 @@ import emoji from 'emoji-dictionary'; | |
import './Card.css'; | ||
|
||
class Card extends Component { | ||
|
||
deleteCard = () => { | ||
this.props.deleteCardCallback(this.props.id); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div className="card"> | ||
Card | ||
<div className="card__content"> | ||
<div className="card__content-text"> | ||
{this.props.text} | ||
</div> | ||
<div className="card__content-emoji"> | ||
{emoji.getUnicode(`${this.props.emoji}`)} | ||
</div> | ||
<div> | ||
<button className="card__delete" onClick={() => { if (window.confirm('Are you sure you wish to delete this item?')) this.deleteCard() } } > | ||
Delete | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
Card.propTypes = { | ||
|
||
id: PropTypes.number.isRequired, | ||
text: PropTypes.string.isRequired, | ||
emoji: PropTypes.string.isRequired, | ||
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. It's either the emoji is required or the text, but not necessarily both! |
||
deleteCardCallback: PropTypes.func.isRequired | ||
}; | ||
|
||
export default Card; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { shallow } from 'enzyme'; | ||
import Card from './Card'; | ||
|
||
describe('Card', () => { | ||
test('it will match the last snapshot', () => { | ||
const card = shallow( | ||
<Card id={0} text="" emoji="" deleteCardCallback={()=>{}}/> | ||
); | ||
|
||
expect(card).toMatchSnapshot(); | ||
|
||
}); | ||
|
||
test('it invokes a callback when the card is deleted', () => { | ||
const callback = jest.fn(); | ||
const card = shallow( | ||
<Card id={0} text="" emoji="" deleteCardCallback={callback}/> | ||
); | ||
|
||
card.find('button').simulate('click'); | ||
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. This test is failing because you have a confirmation dialog as well. You need to research how to mock that. |
||
global.confirm = () => true //This stubs the window.confirm function | ||
|
||
expect(callback).toHaveBeenCalled(); | ||
|
||
}); | ||
}); |
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're causing the entire page to reload which defeats the entire purpose of doing an SPA.