I am looking for some suggestions on how to make my code cleaner and simpler. Or put another way: more elegant and idiomatic. Any other comments on quality or anything would be appreciated.
class App extends Component {
constructor(props){
super(props);
this.state = {
colIds : ["winnie", "bob", "thomas", "george"],
cols : [
{name:"Winnnie", headerColor: "#8E6E95", id : "winnie" },
{name:"Bob", headerColor: "#39A59C", id :"bob"},
{name:"Thomas", headerColor: "#344759", id: "thomas"},
{name:"George", headerColor: "#E8741E", id: "george"}
],
cards : [
{colid: "winnie", task: "buy eggs"},
{colid: "bob", task: "clean basement"},
{colid: "thomas", task: "pack"},
{colid: "george", task: "pay bills"},
]
}
this.addCard = this.addCard.bind(this);
this.moveCardRight = this.moveCardRight.bind(this);
this.moveCardLeft = this.moveCardLeft.bind(this);
}
getColCards(colid){
let cards = this.state.cards.filter( c => c.colid == colid);
return cards;
}
addCard(colid){
let cardTask = window.prompt("Whats the task you want to add");
let currentCards = this.state.cards.slice();
let newCard = {colid: colid, task: cardTask};
currentCards.push(newCard);
this.setState({cards: currentCards});
}
moveCardRight(card){
let currentCardCol = card.colid;
let nextIndex = this.state.colIds.indexOf(currentCardCol) + 1;
if(nextIndex > this.state.colIds.length - 1 ){
return null;
}
let currentCards = this.state.cards.slice();
for(let i = 0; i < currentCards.length; i++){
if(currentCards[i].task === card.task){
currentCards[i] = {
...currentCards[i],
colid : this.state.colIds[nextIndex]
}
}
}
this.setState({cards: currentCards});
}
moveCardLeft(card){
let currentCardCol = card.colid;
let nextIndex = this.state.colIds.indexOf(currentCardCol) - 1;
if(nextIndex < 0 ){
return null;
}
let currentCards = this.state.cards.slice();
for(let i = 0; i < currentCards.length; i++){
if(currentCards[i].task === card.task){
currentCards[i] = {
...currentCards[i],
colid : this.state.colIds[nextIndex]
}
}
}
this.setState({cards: currentCards});
}
render() {
let cols =[
{name:"Winnnie", headerColor: "#8E6E95"},
{name:"Bob", headerColor: "#39A59C"},
{name:"Thomas", headerColor: "#344759"},
{name:"George", headerColor: "#E8741E"}
];
let colCards = this.state.cols.map(c => {
let cards = this.getColCards(c.id).map(c => {
return <div>
<span onClick = {() => this.moveCardLeft(c)}> {"< "} </span>
{c.task}
<span onClick = {() => this.moveCardRight(c)}> {" >"} </span>
</div>
})
return <CardCol name={c.name} headerColor={c.headerColor} addCard={this.addCard} id={c.id}>
{cards}
</CardCol>
})
return (
<div className="App">
{colCards}
</div>
);
}
}
CardColcomponent? And are themoveCardLeft()andmoveCardRight()functions supposed to move the cards left and right? if so, does that currently work? \$\endgroup\$