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

Sockets - Riyo #22

Open
wants to merge 4 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
9 changes: 9 additions & 0 deletions package-lock.json

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"devDependencies": {
"enzyme": "^3.10.0",
"enzyme-adapter-react-16": "^1.14.0",
"enzyme-to-json": "^3.3.5",
"gh-pages": "^2.0.1"
},
"jest": {
"snapshotSerializers": ["enzyme-to-json/serializer"]
}
}
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class App extends Component {
</header>
<Board
url="https://inspiration-board.herokuapp.com/boards/"
boardName={`Ada-Lovelace`}
boardName={`riyo`}
/>
</section>
);
Expand Down
86 changes: 81 additions & 5 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,104 @@ import './Board.css';
import Card from './Card';
import NewCardForm from './NewCardForm';
import CARD_DATA from '../data/card-data.json';
import { pseudoRandomBytes } from 'crypto';

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

this.state = {
cards: [],
errorMessages: ""
};
}

componentDidMount() {
const fullUrl = this.props.url + this.props.boardName + "/cards"
axios.get(fullUrl)
.then((response) => {
const cards = response.data.map((card) => {
const newCard = {
text: card.card.text,
emoji: card.card.emoji,
id: card.card.id
}
return newCard;
})

this.setState({ cards });

})
.catch((error) => {
this.setState({ errorMessages: error.message });
});
}


addCardCallback = (cardInfo) => {
const fullUrl = this.props.url + this.props.boardName + "/cards"
axios.post(fullUrl, cardInfo)
.then((response) => {
const card = response.data["card"]
let updatedData = this.state.cards;
updatedData.push({
id: card.id,
text: cardInfo.text,
emoji: cardInfo.emoji
});
this.setState({ cards: updatedData })
})
.catch((error) => {
this.setState({ errorMessages: error.message });
});
}

deleteCardCallback = (id) => {
const updatedCards = this.state.cards.filter((card) => card.id !== id)

this.setState({
cards: updatedCards
});

const fullUrl = `https://inspiration-board.herokuapp.com/cards/${id}`
axios.delete(fullUrl)
.then((response) => {

})
.catch((error) => {
this.setState({ errorMessages: error.message });
});
}




render() {
const showCards = this.state.cards.map((card, index) => {
return (<Card
key={index}
text={card.text}
emoji={card.emoji}
id={card.id}
deleteCardCallback={this.deleteCardCallback}
/>)
})

const errors = <div className="validation-errors-display">Error: {this.state.errorMessages}</div>
return (
<div>
Board
<div className="board">
{this.state.errorMessages ? errors : ""}
<NewCardForm addCardCallback={this.addCardCallback} />
{showCards}
</div>
)
}

};
}

Board.propTypes = {

Board.propTypes = {
url: PropTypes.string.isRequired,
boardName: PropTypes.string.isRequired,
};

export default Board;
Empty file removed src/components/Board.test.js
Empty file.
22 changes: 20 additions & 2 deletions src/components/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,38 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types';
import emoji from 'emoji-dictionary';


import './Card.css';

class Card extends Component {


render() {
const isEmoji = (this.props.emoji) ? this.props.emoji : ""
const id = this.props.id
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(isEmoji)}
</div>
<div>
<button onClick={() => this.props.deleteCardCallback(id)} className="card__delete"> Delete </button>
</div>
</div>
</div>
)
}
}

Card.propTypes = {

text: PropTypes.string,
emoji: PropTypes.string,
id: PropTypes.number,
deleteCardCallback: PropTypes.func
};

export default Card;
75 changes: 75 additions & 0 deletions src/components/NewCardForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,79 @@ import PropTypes from 'prop-types';
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: "",
}
}

onInputChange = (event) => {
const updatedState = {};

const field = event.target.name;
const value = event.target.value;

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


clearForm = () => {
this.setState({
text: "",
emoji: "",
});
}

onFormSubmit = (event) => {
event.preventDefault();

const newCard = {
text: this.state.text,
emoji: this.state.emoji,
}

this.props.addCardCallback(newCard)
this.clearForm()
}


emojiOptions = (emojis) => {
const select = emojis.map((emojiString) => {
return (
<option value={emojiString}>{emoji.getUnicode(emojiString)}</option>
)
})
return select
}

render() {
return (
<div className="new-card-form">
<div className="new-card-form__header">
Write a Inspirational Card!
</div>
<form className="new-card-form__form" onSubmit={this.onFormSubmit}>
<div>
<textarea name="text" value={this.state.text} type="text" onChange={this.onInputChange} className="new-card-form__form-textarea" />
</div>
<div>
<select className="new-card-form__form-select" onChange={this.onInputChange} name="emoji" value={this.state.emoji} >
{this.emojiOptions(EMOJI_LIST)}
</select>
</div>
<div>
<input type="submit" value="Submit Card" className="new-card-form__form-button" />
</div>
</form>
</div>
)
}
}
export default NewCardForm;
Empty file removed src/components/NewCardForm.test.js
Empty file.
12 changes: 12 additions & 0 deletions src/components/test/Board.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import Board from '../Board';
import { shallow } from 'enzyme';

describe('Board', () => {
test('that it matches an existing snapshot', () => {

const wrapper = shallow( <Board url={() => {} } boardName={() => {} } />);

expect(wrapper).toMatchSnapshot();
});
});
11 changes: 11 additions & 0 deletions src/components/test/NewCardForm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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 addCardCallback={() => {} } />);

expect(wrapper).toMatchSnapshot();
});
});
11 changes: 11 additions & 0 deletions src/components/test/__snapshots__/Board.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Board that it matches an existing snapshot 1`] = `
<div
className="board"
>
<NewCardForm
addCardCallback={[Function]}
/>
</div>
`;
76 changes: 76 additions & 0 deletions src/components/test/__snapshots__/NewCardForm.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`NewCardForm that it matches an existing snapshot 1`] = `
<div
className="new-card-form"
>
<div
className="new-card-form__header"
>
Write a Inspirational Card!
</div>
<form
className="new-card-form__form"
onSubmit={[Function]}
>
<div>
<textarea
className="new-card-form__form-textarea"
name="text"
onChange={[Function]}
type="text"
value=""
/>
</div>
<div>
<select
className="new-card-form__form-select"
name="emoji"
onChange={[Function]}
value=""
>
<option
value=""
/>
<option
value="heart_eyes"
>
😍
</option>
<option
value="beer"
>
🍺
</option>
<option
value="clap"
>
👏
</option>
<option
value="sparkling_heart"
>
💖
</option>
<option
value="heart_eyes_cat"
>
😻
</option>
<option
value="dog"
>
🐶
</option>
</select>
</div>
<div>
<input
className="new-card-form__form-button"
type="submit"
value="Submit Card"
/>
</div>
</form>
</div>
`;
2 changes: 1 addition & 1 deletion src/data/card-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
{
"text": "",
"Emoji": "heart_eyes"
"emoji": "heart_eyes"
},
{
"text": "REST is part of work"
Expand Down