Skip to content
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
65 changes: 63 additions & 2 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,71 @@ class Board extends Component {
};
}

componentDidMount() {
axios.get('https://inspiration-board.herokuapp.com/boards/ultra_princess/cards')
.then((res) => {
this.setState({ cards: res.data })
})
.catch((error) => {
alert('hm ur cards didnt load maybe u should refresh lol');
});
}

delCard = (id) => {
const url = 'https://inspiration-board.herokuapp.com/cards/' + id.toString();

axios.delete(url)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use complete words for variable and function names. Your jargon may feel obvious to you, but it will often be less so to your coworkers.

.then(() => {
let index;
let newState = this.state;

newState.cards.forEach((card, i) => {
if (card.card.id === id) {
index = i;
}
});

newState.cards.splice(index, 1);

this.setState(newState);
})
.catch((e) => {
alert('sry could not delete ?? idk tbh..');
console.log(e);
});
}

addCard = (newCard) => {
const url = `https://inspiration-board.herokuapp.com/boards/ultra_princess/cards?text=${newCard.text}&emoji=${newCard.emoji}`;

axios.post(url)
.then((res) => {
let newState = this.state;

Choose a reason for hiding this comment

The 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.

newState.cards.push(res.data);

this.setState(newState);
})
.catch((e) => {
alert('sry could not add card ?? idk tbh..');
console.log(e);
});
}

render() {
const cards = this.state.cards.map((e, i) => {
return (
<Card key={ e.card.id }
id={ e.card.id }
text={ e.card.text }
emoji={ e.card.emoji }
delCB={ this.delCard } />
)
});

return (
<div>
Board
<div className="board">
{ cards }
<NewCardForm addCB={ this.addCard } />
</div>
)
}
Expand Down
6 changes: 5 additions & 1 deletion src/components/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class Card extends Component {
render() {
return (
<div className="card">
Card
<div className="card__content">
<p className="card__content-text">{ this.props.text }</p>
<p className="card__content-emoji">{ this.props.emoji ? emoji.getUnicode(this.props.emoji) : "" }</p>
<button onClick={() => {this.props.delCB(this.props.id)} } >del mee</button>
</div>
</div>
)
}
Expand Down
66 changes: 66 additions & 0 deletions src/components/NewCardForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,69 @@ import emoji from 'emoji-dictionary';
import './NewCardForm.css';

const EMOJI_LIST = ["", "heart_eyes", "beer", "clap", "sparkling_heart", "heart_eyes_cat", "dog"]

class NewCardForm extends Component {
constructor(props) {
super(props);

this.state = {
text: '',
emoji: '',
};
}

resetState = () => {
this.setState({
text: '',
emoji: '',
});
}

onFormChange = (event) => {
const field = event.target.name;
const value = event.target.value;

const updatedState = {};
updatedState[field] = value;
this.setState(updatedState);
}

onSubmit = (event) => {
event.preventDefault();
const { text, emoji } = this.state;
const newCard = { text: text, emoji: emoji };

this.props.addCB(newCard);
}

render() {
return(
<div className="new-card-form">
<h2 className="new-card-form__header">
Add a Card!
</h2>
<form onSubmit={this.onSubmit} name="new-card-form__form" className="new-card-form__form">
<label className="new-card-form__form-label" htmlFor="text">
Text
</label>
<textarea className="new-card-form__form-textarea" name="text" placeholder="text goes here" onChange={this.onFormChange} value={this.state.text} />
<label className="new-card-form__form-label" htmlFor="emoji">
Emoji
</label>
<select className="new-card-form__form-select" name="emoji" onChange={this.onFormChange}>
<option value=""></option>
<option value="heart_eyes">😍</option>
<option value="beer">🍺</option>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you generate these <option> tags dynamically from EMOJI_LIST?

<option value="clap">👏</option>
<option value="sparkling_heart">💖</option>
<option value="heart_eyes_cat">😻</option>
<option value="dog">🐶</option>
</select>
<input className="new-card-form__form-button" type="submit" name="submit" value="add it" />
</form>
</div>
);
}
}

export default NewCardForm;
Empty file removed src/components/NewCardForm.test.js
Empty file.
13 changes: 13 additions & 0 deletions src/components/test/Card.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import Card from '../Card';
import { shallow } from 'enzyme';

describe('Card', () => {
test('that it matches an existing snapshot', () => {
const wrapper = shallow(
<Card delCB={() => {}} />
);

expect(wrapper).toMatchSnapshot();
});
});
13 changes: 13 additions & 0 deletions src/components/test/NewCardForm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import NewCardForm from '../NewCardForm';
import { shallow } from 'enzyme';

describe('NewCardForm', () => {
test('that it matches an existing snapshot', () => {
const wrapper = shallow(
<NewCardForm addCB={() => {}} />
);

expect(wrapper).toMatchSnapshot();
});
});
Loading