From d48cb5aae4b0139bb9a1a2c8d50d8fb270ce0504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Fri, 22 Mar 2019 09:52:48 +0100 Subject: [PATCH] imp: work on store, refactor, improve todoapp --- examples/todoapp/components/TodoApp.js | 20 +++++---- examples/todoapp/main.js | 2 +- examples/todoapp/templates.xml | 6 +-- src/index.ts | 4 +- src/store.ts | 43 ++++++++++++++++--- src/utils.ts | 13 ++++++ tests/__snapshots__/store.test.ts.snap | 15 +++++++ tests/store.test.ts | 57 +++++++++++++++++++++++++- tests/utils.test.ts | 14 ++++++- 9 files changed, 150 insertions(+), 24 deletions(-) create mode 100644 tests/__snapshots__/store.test.ts.snap diff --git a/examples/todoapp/components/TodoApp.js b/examples/todoapp/components/TodoApp.js index 2d565b87..ab3aca07 100644 --- a/examples/todoapp/components/TodoApp.js +++ b/examples/todoapp/components/TodoApp.js @@ -1,20 +1,20 @@ import { TodoItem } from "./TodoItem.js"; -const { StoreMixin, Component } = odoo.core; +const { connect, Component } = odoo.core; const ENTER_KEY = 13; -export class TodoApp extends StoreMixin(Component) { +function mapStateToProps(state) { + return { todos: state.todos }; +} + +class TodoApp extends Component { template = "todoapp"; widgets = { TodoItem }; state = { filter: "all" }; - get todos() { - return this.env.store.state.todos; - } - get visibleTodos() { - let todos = this.todos; + let todos = this.props.todos; if (this.state.filter === "active") { todos = todos.filter(t => !t.completed); } @@ -25,11 +25,11 @@ export class TodoApp extends StoreMixin(Component) { } get allChecked() { - return this.todos.every(todo => todo.completed); + return this.props.todos.every(todo => todo.completed); } get remaining() { - return this.todos.filter(todo => !todo.completed).length; + return this.props.todos.filter(todo => !todo.completed).length; } get remainingText() { @@ -54,3 +54,5 @@ export class TodoApp extends StoreMixin(Component) { this.env.store.dispatch("toggleAll", !this.allChecked); } } + +export default connect(mapStateToProps)(TodoApp); diff --git a/examples/todoapp/main.js b/examples/todoapp/main.js index 0e77fe0d..342511b7 100644 --- a/examples/todoapp/main.js +++ b/examples/todoapp/main.js @@ -1,4 +1,4 @@ -import { TodoApp } from "./components/TodoApp.js"; +import TodoApp from "./components/TodoApp.js"; import { makeStore } from "./store.js"; async function makeEnv() { diff --git a/examples/todoapp/templates.xml b/examples/todoapp/templates.xml index dd444b50..a2290c86 100644 --- a/examples/todoapp/templates.xml +++ b/examples/todoapp/templates.xml @@ -8,7 +8,7 @@ -
+
    @@ -18,7 +18,7 @@
-