Skip to content

Commit 1a34266

Browse files
committed
[IMP] awesome_dashboard: added Chart and made dashboard lazyload.
created service to load rpc call and load rpc data. added interval for rpc call to refresh data every 10 minuets. added Chart to show t-shirt size in chart view. load dashboard as lazy-loading by register that Component to lazy-components.
1 parent ced1c9f commit 1a34266

File tree

13 files changed

+154
-14
lines changed

13 files changed

+154
-14
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,10 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
.eslintignore
132+
.eslintrc.json
133+
jsconfig.json
134+
package.json
135+
package-lock.json
136+
node_modules/

awesome_dashboard/__manifest__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
'web.assets_backend': [
2626
'awesome_dashboard/static/src/**/*',
2727
],
28+
'awesome_dashboard.dashboard': [
29+
'awesome_dashboard/static/src/dashboard/**/*',
30+
],
2831
},
2932
'license': 'AGPL-3'
3033
}
104 KB
Loading

awesome_dashboard/static/src/dashboard.js renamed to awesome_dashboard/static/src/dashboard/dashboard.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
import { Component, onWillStart } from "@odoo/owl";
1+
import { Component, useState } from "@odoo/owl";
22
import { registry } from "@web/core/registry";
33
import { Layout } from "@web/search/layout";
44
import { useService } from "@web/core/utils/hooks";
55
import { DashboardItem } from "./dashboarditem/dashboarditem";
6-
import { rpc } from "@web/core/network/rpc";
6+
import { PieChart } from "./piechart/piechart";
77

88
class AwesomeDashboard extends Component {
99
static template = "awesome_dashboard.AwesomeDashboard";
10-
static components = { Layout, DashboardItem }
10+
static components = { Layout, DashboardItem, PieChart }
1111

1212
setup() {
1313
this.action = useService("action");
14-
onWillStart(async () => {
15-
this.result = await rpc("/awesome_dashboard/statistics");
16-
})
14+
const statisticsService = useService("awesome_dashboard.statistics");
15+
this.statistics = useState(statisticsService.state);
1716
}
1817

1918
openCustomers() {
@@ -42,4 +41,4 @@ class AwesomeDashboard extends Component {
4241
}
4342
}
4443

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

awesome_dashboard/static/src/dashboard.xml renamed to awesome_dashboard/static/src/dashboard/dashboard.xml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,37 @@
1515
<DashboardItem>
1616
<div class="text-center">
1717
Number of new orders this month
18-
<p t-out="this.result.nb_new_orders" class="fs-1 fw-bold text-success"/>
18+
<p t-out="this.statistics.data.nb_new_orders" class="fs-1 fw-bold text-success"/>
1919
</div>
2020
</DashboardItem>
2121
<DashboardItem>
2222
<div class="text-center">
2323
Total amount of new orders this month
24-
<p t-out="this.result.total_amount" class="fs-1 fw-bold text-success"/>
24+
<p t-out="this.statistics.data.total_amount" class="fs-1 fw-bold text-success"/>
2525
</div>
2626
</DashboardItem>
2727
<DashboardItem>
2828
<div class="text-center">
2929
Average amount of t-shirt by order this month
30-
<p t-out="this.result.average_quantity" class="fs-1 fw-bold text-success"/>
30+
<p t-out="this.statistics.data.average_quantity" class="fs-1 fw-bold text-success"/>
3131
</div>
3232
</DashboardItem>
3333
<DashboardItem>
3434
<div class="text-center">
3535
Number of cancelled orders this month
36-
<p t-out="this.result.nb_cancelled_orders" class="fs-1 fw-bold text-success"/>
36+
<p t-out="this.statistics.data.nb_cancelled_orders" class="fs-1 fw-bold text-success"/>
3737
</div>
3838
</DashboardItem>
3939
<DashboardItem>
40+
<div class="text-center">
41+
Shirt order by size
42+
<PieChart items="this.statistics.data.orders_by_size"/>
43+
</div>
44+
</DashboardItem>
45+
<DashboardItem>
4046
<div class="text-center">
4147
Average time for an order to go from ‘new’ to ‘sent’ or ‘cancelled’
42-
<p t-out="this.result.average_time" class="fs-1 fw-bold text-success"/>
48+
<p t-out="this.statistics.data.average_time" class="fs-1 fw-bold text-success"/>
4349
</div>
4450
</DashboardItem>
4551
</div>
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<templates xml:space="preserve">
3+
34
<t t-name="awesome_dashboard.DashboardItem">
4-
<div class="card m-2" t-attf-style="width: {{ 18 * props.size }}rem; border-radius: 16px;">
5+
<div class="card m-2" t-attf-style="width: {{ 18 * props.size }}rem; border-radius: 16px; height: fit-content;">
56
<div class="card-body">
67
<t t-slot="default"/>
78
</div>
89
</div>
910
</t>
11+
1012
</templates>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { Component, onWillStart, onPatched, useRef, onMounted } from "@odoo/owl";
2+
import { loadJS } from "@web/core/assets";
3+
4+
export class PieChart extends Component {
5+
static template = "awesome_dashboard.PieChart";
6+
static props = {
7+
items: {
8+
type: Object,
9+
optional: true,
10+
default: () => ({}),
11+
},
12+
}
13+
14+
setup() {
15+
this.chartRef = useRef("pie-canvas");
16+
this.myPieChart = null;
17+
18+
onWillStart(async () => {
19+
await loadJS("/web/static/lib/Chart/Chart.js");
20+
});
21+
22+
onMounted(() => {
23+
this.renderChart()
24+
});
25+
26+
onPatched(() => {
27+
this.renderChart()
28+
});
29+
}
30+
31+
renderChart() {
32+
if (!this.chartRef.el) {
33+
return;
34+
}
35+
36+
if (this.myPieChart) {
37+
this.myPieChart.destroy();
38+
}
39+
40+
this.pieChartData = {
41+
labels: ['M', 'S', 'XL'],
42+
datasets: [{
43+
label: 'Sales Count',
44+
data: [this.props.items.m, this.props.items.s, this.props.items.xl],
45+
backgroundColor: [
46+
'rgba(255, 99, 132, 0.6)',
47+
'rgba(54, 162, 235, 0.6)',
48+
'rgba(255, 206, 86, 0.6)'
49+
],
50+
borderColor: [
51+
'rgba(255, 99, 132, 1)',
52+
'rgba(54, 162, 235, 1)',
53+
'rgba(255, 206, 86, 1)'
54+
],
55+
borderWidth: 1
56+
}]
57+
};
58+
59+
this.pieChartOptions = {
60+
responsive: true,
61+
maintainAspectRatio: false,
62+
plugins: {
63+
legend: {
64+
position: 'top',
65+
}
66+
}
67+
};
68+
69+
this.myPieChart = new Chart(this.chartRef.el,
70+
{
71+
type: 'pie',
72+
data: this.pieChartData,
73+
options: this.pieChartOptions
74+
});
75+
}
76+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
4+
<t t-name="awesome_dashboard.PieChart">
5+
<div class="card-body">
6+
<canvas t-ref="pie-canvas" class="o_pie_chart_canvas"/>
7+
</div>
8+
</t>
9+
10+
</templates>

0 commit comments

Comments
 (0)