Skip to content

Commit 4c06dbd

Browse files
committed
[IMP] awesome_dashboard: make dashboard items dynamic and reusable
This commit restructures the dashboard to render items dynamically based on a centralized configuration list. This approach enables easier extension and reuse of dashboard item types, improving maintainability and scalability. Key changes include: Introduction of dashboard_items.js to define a list of item configurations, including type, title, and props. This enables dynamic rendering of dashboard widgets like NumberCard and PieChartCard. The DashboardItem component now acts as a dynamic wrapper that loads and renders the appropriate child component based on its type. This is achieved using Owl's dynamic component syntax and t-component. Separation of each card type into its own file (NumberCard, PieChartCard) to promote single-responsibility design and reuse across modules. All components continue to use Bootstrap utility classes and maintain responsive, clean, and modern styling. This change lays the foundation for a fully extensible dashboard architecture, where new visualizations can be added without modifying the core dashboard view.
1 parent 1cbf9c3 commit 4c06dbd

File tree

20 files changed

+410
-20
lines changed

20 files changed

+410
-20
lines changed

awesome_dashboard/static/src/dashboard.js

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.dashboard-card-wrapper {
2+
transition: transform 0.3s ease, box-shadow 0.3s ease;
3+
}
4+
5+
.dashboard-card {
6+
border-radius: 1rem;
7+
transition: all 0.3s ease-in-out;
8+
cursor: pointer;
9+
}
10+
11+
.dashboard-card:hover {
12+
transform: translateY(-5px);
13+
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.12);
14+
}
15+

awesome_dashboard/static/src/dashboard.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// .o_dashboard_cards {
2+
// display: flex;
3+
// flex-wrap: wrap;
4+
// gap: 1rem;
5+
// }
6+
7+
.dashboard-item {
8+
background: white;
9+
padding: 1.5rem;
10+
border-radius: 0.75rem;
11+
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
12+
min-height: 150px;
13+
display: flex;
14+
flex-direction: column;
15+
justify-content: center;
16+
align-items: center;
17+
18+
h5 {
19+
font-size: 1rem;
20+
margin-bottom: 0.5rem;
21+
text-align: center;
22+
}
23+
24+
p {
25+
font-size: 1.5rem;
26+
font-weight: bold;
27+
color: green;
28+
}
29+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Component } from "@odoo/owl";
2+
3+
export class DashboardItem extends Component {
4+
static template = "awesome_dashboard.DashboardItem";
5+
static props = { size: { type: Number, optional: true, default: 1 } };
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<templates xml:space="preserve">
2+
<t t-name="awesome_dashboard.DashboardItem">
3+
<div class="card p-3 m-1 h-100 shadow-sm dashboard-card animate-hover">
4+
<div class="card-body overflow-auto">
5+
<t t-slot="default"/>
6+
</div>
7+
</div>
8+
</t>
9+
</templates>
10+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Component } from "@odoo/owl";
2+
3+
export class NumberCard extends Component {
4+
static template = "awesome_dashboard.NumberCard";
5+
static props = {
6+
label: String,
7+
value: [Number, String],
8+
};
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<templates xml:space="preserve">
2+
<t t-name="awesome_dashboard.NumberCard">
3+
<h4 class="text-nowrap">
4+
<t t-out="props.label"/>
5+
</h4>
6+
<p class="fs-2 fw-bold">
7+
<t t-out="props.value"/>
8+
</p>
9+
</t>
10+
</templates>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/** @odoo-module **/
2+
3+
import { Component, useRef, onWillStart, onMounted, onWillUpdateProps } from '@odoo/owl';
4+
import { loadJS } from "@web/core/assets";
5+
6+
export class PieChart extends Component {
7+
static template = 'awesome_dashboard.PieChart';
8+
9+
static props = {
10+
data: Object,
11+
};
12+
13+
setup() {
14+
this.canvasRef = useRef('chartCanvas');
15+
this.chart = null;
16+
17+
onWillStart(async () => {
18+
await loadJS("/web/static/lib/Chart/Chart.js");
19+
});
20+
21+
onMounted(() => {
22+
this.renderChart();
23+
});
24+
25+
onWillUpdateProps((nextProps) => {
26+
if (this.chart) {
27+
// Update dataset data with new props
28+
this.chart.data.labels = Object.keys(nextProps.data);
29+
this.chart.data.datasets[0].data = Object.values(nextProps.data);
30+
this.chart.update(); // ✅ Trigger re-render
31+
}
32+
});
33+
}
34+
35+
renderChart() {
36+
const ctx = this.canvasRef.el.getContext('2d');
37+
this.chart = new Chart(ctx, {
38+
type: "pie",
39+
data: {
40+
labels: Object.keys(this.props.data),
41+
datasets: [
42+
{
43+
label: "Orders by T-shirt Size",
44+
data: Object.values(this.props.data),
45+
backgroundColor: [
46+
"#3498db", "#2ecc71", "#f1c40f", "#e67e22", "#9b59b6"
47+
],
48+
borderWidth: 1
49+
}
50+
]
51+
},
52+
options: {
53+
responsive: true,
54+
plugins: {
55+
legend: {
56+
position: "right"
57+
}
58+
}
59+
}
60+
});
61+
}
62+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<t t-name="awesome_dashboard.PieChart" owl="1">
2+
<div class="w-100 h-100">
3+
<canvas t-ref="chartCanvas" style="width: 100%; height: 300px;"></canvas>
4+
</div>
5+
</t>

0 commit comments

Comments
 (0)