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
124 lines (98 loc) · 2.45 KB
/
Board.js
File metadata and controls
124 lines (98 loc) · 2.45 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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: [],
};
}
componentDidMount() {
const url = `${this.props.url}/boards/katrina/cards`;
axios.get(url)
.then((response) => {
this.setState({
cards: response.data,
});
})
.catch((error) => {
const errors = error.response.data.errors.text;
this.setState({
error: errors,
});
});
};
deleteCard = (id) => {
const url = `${this.props.url}/cards/${id}`;
axios.delete(url)
.then(() => {
const updatedCardsList = this.state.cards;
updatedCardsList.forEach((card, i) => {
if (id === card.card.id) {
updatedCardsList.splice(i, 1);
};
});
this.setState({
cards: updatedCardsList
});
})
.catch((error) => {
const errors = error.response.data.errors.text;
this.setState({
error: errors,
});
});
};
addCard = (cardData) => {
const url = `${this.props.url}/boards/katrina/cards`;
axios.post(url, cardData)
.then((response) => {
const updatedCardsList = [...this.state.cards, response.data]
this.setState({
cards: updatedCardsList,
});
})
.catch((error) => {
const errors = error.response.data.errors.text;
this.setState({
error: errors,
});
});
};
render() {
const cardList = this.state.cards.map((card, i) => {
const cardData = card.card;
return (
<Card
key={i}
{...cardData}
onDeleteClickCallback={this.deleteCard}
/>
)
});
const errorMessages = this.state.error ? this.state.error.map((message, i) => {
return (<h3><li key={i}>{message}</li></h3>) }) : '';
return (
<section>
<header className="validation-errors-display">
<ul className="validation-errors-display__list">
{errorMessages}
</ul>
</header>
<div className="board">
{cardList}
</div>
<NewCardForm addCardCallback={this.addCard} />
</section>
)
}
}
Board.propTypes = {
cards: PropTypes.array,
};
export default Board;