forked from AdaGold/inspiration-board
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathBoard.js
More file actions
108 lines (90 loc) · 2.72 KB
/
Board.js
File metadata and controls
108 lines (90 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';
import './Board.css';
import Card from './Card';
import NewCardForm from './NewCardForm';
// import CARD_DATA from '../data/card-data.json';
class Board extends Component {
constructor(props) {
super(props);
this.state = {
cards: [],
url: this.props.url,
boardName: this.props.boardName,
error: undefined
};
}
componentDidMount( ) {
const allCardsURL = this.state.url + 'boards/' + this.state.boardName + 'cards/';
axios.get(allCardsURL)
.then((response) => {
this.setState({
cards: response.data
})
})
.catch((error)=> {
const errorStr = `Got an error with status ${error.response.status} and message ${error.response.statusText}`
this.setState({
error: errorStr
})
});
}
deleteCard = (id) => {
const deleteURL = `${this.state.url}cards/${id}`
axios.delete(deleteURL)
.then((response) => {
const card = response.data.card;
alert(`Successfully deleted card with id ${card.id} and message ${card.text}.`)
const updatedCards = this.state.cards.filter((card) => {
return card.card.id !== id
})
this.setState({
cards: updatedCards
})
})
.catch((error) => {
const errorStr = `Got an error with status ${error.response.status} and message ${error.response.statusText}`
this.setState({
error: errorStr
})
})
}
addCard = (cardObj) => {
let URL = `${this.state.url}boards/${this.state.boardName}cards`;
axios.post(URL, cardObj)
.then((response) => {
const updatedCards = this.state.cards;
updatedCards.push({card: response.data.card});
console.log(updatedCards, "response", response.data)
this.setState({
cards: updatedCards
})
})
.catch((error) => {
const errorStr = `Got an error with status ${error.response.status} and message ${error.response.statusText}`
this.setState({
error: errorStr
})
})
}
render() {
const cards = this.state.cards.map((card) => {
return (<Card key={`${card.card.id}${card.card.text}${card.card.emoji}`}
text={card.card.text} emoji={card.card.emoji} id={card.card.id} deleteCardCallback={this.deleteCard}/>)
})
const display = (this.state.cards.length !== 0) ? (<section>{cards}</section>)
: (<p>{this.state.error}</p>) ;
return (
<div>
<NewCardForm addCardCallback={this.addCard}/>
{display}
</div>
)
}
}
Board.propTypes = {
url: PropTypes.string.isRequired,
boardName: PropTypes.string.isRequired
};
export default Board;