-
Notifications
You must be signed in to change notification settings - Fork 45
Hayden - Edges (C10) - Inspo Board #44
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
eb956f5
8c856ca
764559b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
| .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; | ||
|
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. |
||
| 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> | ||
| ) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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> | ||
|
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. Could you generate these |
||
| <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; | ||
| 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(); | ||
| }); | ||
| }); |
| 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(); | ||
| }); | ||
| }); |
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 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.