Created
September 30, 2018 18:35
-
-
Save alvinmatias69/014e109d525bafd89184af6e873766ef to your computer and use it in GitHub Desktop.
A class composition for decomposing data from react component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class Conversation { | |
constructor() { | |
this.conversations = []; | |
} | |
process(conversations) { | |
this.conversations = conversations.map(item => ({ | |
...item, | |
date: parseDate(item.date), | |
selected: false, | |
})); | |
} | |
select(id) { | |
this.conversations = this.conversations.map(item => ({ | |
...item, | |
selected: item.id === id ? !item.selected : item.selected, | |
})); | |
} | |
removeSelected() { | |
this.conversations = this.conversations.map(item => ({ | |
...item, | |
selected: false, | |
})); | |
} | |
getSelectedId() { | |
return this.conversations.reduce((acc, cur) => { | |
if (cur.selected) { | |
acc.push(cur.id); | |
} | |
return acc; | |
}, []); | |
} | |
deleteSelected() { | |
this.conversations = this.conversations.filter(item => !item.selected); | |
} | |
countSelected() { | |
return this.conversations.reduce((acc, cur) => { | |
if (cur.selected) { | |
acc += 1; | |
} | |
return acc; | |
}, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment