Skip to content

Commit c2f8f51

Browse files
committed
[IMP] awesome_dashboard: reactive use
1 parent e0c0418 commit c2f8f51

File tree

7 files changed

+41
-16
lines changed

7 files changed

+41
-16
lines changed

awesome_dashboard/__manifest__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
'assets': {
2525
'web.assets_backend': [
2626
'awesome_dashboard/static/src/**/*',
27+
('remove', 'awesome_dashboard/static/src/dashboard/**/*'),
2728
],
29+
'awesome_dashboard.dashboard': [
30+
'awesome_dashboard/static/src/dashboard/**/*',
31+
],
32+
2833
},
2934
'license': 'AGPL-3'
3035
}

awesome_dashboard/static/src/dashboard.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @odoo-module **/
22

3-
import { Component, onWillStart } from "@odoo/owl";
3+
import { Component, useState } from "@odoo/owl";
44
import { registry } from "@web/core/registry";
55
import { Layout } from "@web/search/layout";
66
import { useService } from "@web/core/utils/hooks";
@@ -13,13 +13,10 @@ class AwesomeDashboard extends Component {
1313

1414
setup(){
1515
this.action = useService("action");
16-
this.statisticsService = useService("awesome_dashboard.statistics");
16+
this.statistics = useState(useService("awesome_dashboard.statistics"));
1717
this.display = {
1818
controlPanel: {},
1919
};
20-
onWillStart(async() => {
21-
this.statistics = await this.statisticsService.getStatistics();
22-
})
2320
}
2421

2522
openCustomerView() {
@@ -39,4 +36,4 @@ class AwesomeDashboard extends Component {
3936
}
4037
}
4138

42-
registry.category("actions").add("awesome_dashboard.dashboard", AwesomeDashboard);
39+
registry.category("lazy_components").add("AwesomeDashboard", AwesomeDashboard);

awesome_dashboard/static/src/dashboard.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<button class="btn btn-primary" t-on-click="openCustomerView">Customers</button>
88
<button class="btn btn-primary" t-on-click="openLeadView">Leads</button>
99
</t>
10-
<div class="d-flex flex-wrap">
10+
<div class="d-flex flex-wrap" t-if="statistics.isReady">
1111
<DashboardItem>
1212
Average amount of t-shirt by order this month
1313
<div class="fs-1 fw-bold text-success text-center">
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { registry } from "@web/core/registry";
2+
import { LazyComponent } from "@web/core/assets";
3+
import { Component } from "@odoo/owl";
4+
5+
class AwesomeDashboardLoader extends Component {
6+
static template = "awesome_dashboard.AwesomeDashboardLoader";
7+
static components = { LazyComponent };
8+
static props = {};
9+
}
10+
registry.category("actions").add("awesome_dashboard.dashboard", AwesomeDashboardLoader);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
<t t-name="awesome_dashboard.AwesomeDashboardLoader">
4+
<LazyComponent bundle="'awesome_dashboard.dashboard'" Component="'AwesomeDashboard'" props="props"/>
5+
</t>
6+
</templates>

awesome_dashboard/static/src/pie_chart.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { loadJS } from "@web/core/assets";
22
import { getColor } from "@web/core/colors/colors";
3-
import { Component, onWillStart, onWillUnmount, onMounted, useRef } from "@odoo/owl";
3+
import { Component, onWillStart, onWillUnmount, onMounted, onWillUpdateProps, useRef } from "@odoo/owl";
44

55
export class PieChart extends Component {
66
static template = "awesome_dashboard.PieChart";
@@ -15,6 +15,10 @@ export class PieChart extends Component {
1515
onMounted(() => {
1616
this.renderChart();
1717
});
18+
onWillUpdateProps(() => {
19+
this.chart.destroy();
20+
this.renderChart();
21+
})
1822
onWillUnmount(() => {
1923
this.chart.destroy();
2024
});
@@ -38,4 +42,3 @@ export class PieChart extends Component {
3842
});
3943
}
4044
}
41-

awesome_dashboard/static/src/statistics_service.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import { rpc } from "@web/core/network/rpc";
22
import { registry } from "@web/core/registry";
3-
import { memoize } from "@web/core/utils/functions";
3+
import { reactive } from "@odoo/owl";
44

55
const statisticsService = {
6-
async: [
7-
"getStatistics",
8-
],
96
start(){
7+
const statistics = reactive({ isReady: false });
108

11-
return{
12-
getStatistics: memoize(() => rpc("/awesome_dashboard/statistics"))
13-
};
9+
async function getData(){
10+
const updates = await rpc("/awesome_dashboard/statistics");
11+
Object.assign(statistics, updates, { isReady: true });
12+
}
13+
14+
setInterval(getData, 1000*60*10);
15+
getData();
16+
17+
return statistics;
1418
},
1519
};
1620

0 commit comments

Comments
 (0)