Skip to content

Instantly share code, notes, and snippets.

@agoldis
Last active April 11, 2018 03:50
Show Gist options
  • Save agoldis/f41930c13682036e2287e9be0df7b257 to your computer and use it in GitHub Desktop.
Save agoldis/f41930c13682036e2287e9be0df7b257 to your computer and use it in GitHub Desktop.
redux blog
const initialState = {
users: [{ id: "user01", name: "John" }, { id: "user02", name: "Bob" }],
tasks: [
{ id: "task01", title: "Wash car", assignee: "user01" },
{ id: "task02", title: "Watch tutorial", assignee: "user01" },
{ id: "task03", title: "Do Homework", assignee: "user02" }
],
profiles: [
{ id: "profile01", user: "user01", picture: "picture01URL" },
{ id: "profile02", user: "user02", picture: "picture02URL" }
]
};
Object.defineProperty(initialState, "userTasks", {
get: function userTasks() {
return this.users.map(user =>
Object.assign({}, user, {
tasks: this.tasks.filter(task => task.assignee === user.id)
})
);
},
enumerable: false
});
Object.defineProperty(initialState, "userTasksCount", {
get: function userTasksCount() {
return this.userTasks.map(user =>
Object.assign({}, user, { tasksCount: user.tasks.count })
);
},
enumerable: false
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment