Skip to content
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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"axios": "^0.18.0",
"emoji-dictionary": "^1.0.9",
"react": "^16.4.0",
"react-confirm-alert": "^2.0.2",
"react-dom": "^16.4.0",
"react-dropdown": "^1.5.0",
"react-scripts": "1.1.4"
},
"scripts": {
Expand All @@ -20,7 +22,13 @@
"devDependencies": {
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"enzyme-to-json": "^3.3.4",
"gh-pages": "^1.2.0"
},
"jest": {
"snapshotSerializers": [
"enzyme-to-json/serializer"
]
},
"homepage": "http://adagold.github.io/inspiration-board"
}
42 changes: 39 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
import React, { Component } from 'react';
import './App.css';
import Board from './components/Board';
import Status from './components/Status';
import Dropdown from './components/Dropdown';

class App extends Component {
constructor() {
super();

this.state = {
status: {
message: 'loaded the page',
type: 'success'
},
currentBoard: 'sam'
}
}

updateBoard = (name) => {
this.setState({
currentBoard: name
});
}

updateStatus = (message, type) => {
this.setState({
status: {
message: message,
type: type
},
})
}

render() {

return (
<section>
<header className="header">
<h1 className="header__h1"><span className="header__text">Inspiration Board</span></h1>
</header>
<Dropdown updateBoardCallback={ this.updateBoard } />
<Status
message={ this.state.status.message }
type={ this.state.status.type }
/>
<Board
url="https://inspiration-board.herokuapp.com/boards/"
boardName={`Ada-Lovelace`}
/>
url="https://inspiration-board.herokuapp.com/boards/"
boardName={this.state.currentBoard}
updateStatusCallback={this.updateStatus}
/>
</section>
);
}
Expand Down
94 changes: 90 additions & 4 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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.

})
.catch((error) => {
this.props.updateStatusCallback(`Card could not be deleted: ${error.message}`, 'error');
});
}

render() {
const cards = this.state.cards.map((card, index) => {
return (
<div>

Choose a reason for hiding this comment

The 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;
15 changes: 15 additions & 0 deletions src/components/Board.test.js
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();

});
});
24 changes: 22 additions & 2 deletions src/components/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Choose a reason for hiding this comment

The 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;
28 changes: 28 additions & 0 deletions src/components/Card.test.js
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');

Choose a reason for hiding this comment

The 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();

});
});
Loading