Skip to content

Instantly share code, notes, and snippets.

@alvinmatias69
Created September 30, 2018 18:35
Show Gist options
  • Save alvinmatias69/014e109d525bafd89184af6e873766ef to your computer and use it in GitHub Desktop.
Save alvinmatias69/014e109d525bafd89184af6e873766ef to your computer and use it in GitHub Desktop.
A class composition for decomposing data from react component
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