Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions awesome_dashboard/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
# -*- coding: utf-8 -*-
{
'name': "Awesome Dashboard",

'summary': """
"name": "Awesome Dashboard",
"summary": """
Starting module for "Discover the JS framework, chapter 2: Build a dashboard"
""",

'description': """
"description": """
Starting module for "Discover the JS framework, chapter 2: Build a dashboard"
""",

'author': "Odoo",
'website': "https://www.odoo.com/",
'category': 'Tutorials',
'version': '0.1',
'application': True,
'installable': True,
'depends': ['base', 'web', 'mail', 'crm'],

'data': [
'views/views.xml',
"author": "Odoo",
"website": "https://www.odoo.com/",
"category": "Tutorials",
"version": "0.1",
"application": True,
"installable": True,
"depends": ["base", "web", "mail", "crm"],
"data": [
"views/views.xml",
],
'assets': {
'web.assets_backend': [
'awesome_dashboard/static/src/**/*',
"assets": {
"web.assets_backend": [
"awesome_dashboard/static/src/dashboard_action.js",
],
"awesome_dashboard.dashboard": [
"awesome_dashboard/static/src/dashboard/**/*",
],
},
'license': 'AGPL-3'
"license": "AGPL-3",
}
8 changes: 0 additions & 8 deletions awesome_dashboard/static/src/dashboard.js

This file was deleted.

8 changes: 0 additions & 8 deletions awesome_dashboard/static/src/dashboard.xml

This file was deleted.

52 changes: 52 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Component, useState } from "@odoo/owl";
import { _t } from "@web/core/l10n/translation"
import { Layout } from "@web/search/layout"
import { registry } from "@web/core/registry";
import { useService } from "@web/core/utils/hooks";
import { DashboardItem } from "./dashboard_item";
import { PieChart } from "./pie_chart";
import { DashboardConfigDialog } from "./dashboard_config_dialog";

class AwesomeDashboard extends Component {
static template = "awesome_dashboard.AwesomeDashboard";

static components = { Layout, DashboardItem, PieChart, DashboardConfigDialog };

setup() {
this.action = useService("action");
this.statisticsService = useService("awesome_dashboard.statistics")
this.dialog = useService("dialog");
this.statistics = useState(this.statisticsService);
this.configState = useState({
disabledItems: JSON.parse(localStorage.getItem("disabled_dashboard_items") || "[]")
});
}

get items() {
const allItems = registry.category("awesome_dashboard").getAll();
return allItems.filter(item => !this.configState.disabledItems.includes(item.id));
}

openConfiguration() {
this.dialog.add(DashboardConfigDialog, {
onConfigSaved: () => {
this.configState.disabledItems = JSON.parse(localStorage.getItem("disabled_dashboard_items") || "[]");
}
});
}

openCustomers() {
this.action.doAction("base.action_partner_form");
}

openLeads() {
this.action.doAction({
type: "ir.actions.act_window",
name: _t("Leads"),
res_model: "crm.lead",
views: [[false, "list"], [false, "form"]],
});
}
}

registry.category("lazy_components").add("AwesomeDashboard", AwesomeDashboard);
9 changes: 9 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.o_dashboard {
background-color: #f0f2f5;
display: flex;
flex-direction: column;
}
.o_dashboard .display-1 {
color: var(--primary);
font-weight: 500;
}
24 changes: 24 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="awesome_dashboard.AwesomeDashboard">
<Layout display="{controlPanel: {} }" className="'o_dashboard h-100'">
<t t-set-slot="control-panel-always-buttons">
<button class="btn btn-primary" t-on-click="openCustomers">Customers</button>
<button class="btn btn-primary" t-on-click="openLeads">Leads</button>
</t>
<t t-set-slot="control-panel-additional-actions">
<button class="d-print-none btn lh-sm p-0 border-0" t-on-click="openConfiguration">
<i class="fa fa-fw fa-cog"/>
</button>
</t>
<div class="d-flex flex-wrap">
<t t-foreach="items" t-as="item" t-key="item.id">
<DashboardItem size="item.size || 1">
<t t-set="itemProp" t-value="item.props ? item.props(statistics) : {'data': statistics}"/>
<t t-component="item.Component" t-props="itemProp" />
</DashboardItem>
</t>
</div>
</Layout>
</t>
</templates>
32 changes: 32 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_config_dialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/** @odoo-module **/
import { Component, useState } from "@odoo/owl";
import { Dialog } from "@web/core/dialog/dialog";
import { registry } from "@web/core/registry";

export class DashboardConfigDialog extends Component {
static components = { Dialog };
static template = "awesome_dashboard.DashboardConfigDialog";

setup() {
this.allItems = registry.category("awesome_dashboard").getAll();

const disabledItems = JSON.parse(localStorage.getItem("disabled_dashboard_items") || "[]");

const initialStatus = {};
for (const item of this.allItems) {
initialStatus[item.id] = !disabledItems.includes(item.id);
}

this.state = useState(initialStatus);
}

onApply() {
const disabledIds = Object.keys(this.state).filter(id => !this.state[id]);

localStorage.setItem("disabled_dashboard_items", JSON.stringify(disabledIds));

this.props.onConfigSaved();

this.props.close();
}
}
23 changes: 23 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_config_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="awesome_dashboard.DashboardConfigDialog">
<Dialog title="'Dashboard items configuration'">
<div class="p-3">
<p class="text-muted">Which cards do you wish to see?</p>
<div t-foreach="allItems" t-as="item" t-key="item.id" class="form-check my-2">
<input
class="form-check-input"
type="checkbox"
t-model="state[item.id]"
t-att-id="'check_' + item.id"
/>
<label class="form-check-label" t-att-for="'check_' + item.id" t-out="item.description"/>
</div>
</div>
<t t-set-slot="footer">
<button class="btn btn-primary" t-on-click="onApply">Apply</button>
<button class="btn btn-secondary" t-on-click="props.close">Cancel</button>
</t>
</Dialog>
</t>
</templates>
13 changes: 13 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Component } from "@odoo/owl";

export class DashboardItem extends Component {
static template = "awesome_dashboard.DashboardItem";

static props = {
size: { type: Number, optional: true },
slots: { type: Object, optional: true },
}
static defaultProps = {
size: 1,
}
}
12 changes: 12 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="awesome_dashboard.DashboardItem">
<div class="p-2 col-12" t-attf-class="col-lg-#{ (props.size || 1) * 3 }">
<div class="o_dashboard_item card shadow-sm h-100">
<div class="card-body">
<t t-slot="default"/>
</div>
</div>
</div>
</t>
</templates>
67 changes: 67 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { registry } from "@web/core/registry";
import { _t } from "@web/core/l10n/translation";
import { NumberCard } from "./number_card";
import { PieChartCard } from "./pie_chart_card";

const dashboardRegistry = registry.category("awesome_dashboard");

dashboardRegistry.add("nb_new_orders", {
id: "nb_new_orders",
description: _t("Number of new orders this month"),
Component: NumberCard,
props: (data) => ({
title: _t("Number of new orders this month"),
value: data.nb_new_orders,
}),
});

dashboardRegistry.add("total_amount", {
id: "total_amount",
description: _t("Total amount of new orders this month"),
Component: NumberCard,
props: (data) => ({
title: _t("Total amount of new orders this month"),
value: data.total_amount,
}),
});

dashboardRegistry.add("average_quantity", {
id: "average_quantity",
description: _t("Average amount of t-shirt by order this month"),
Component: NumberCard,
props: (data) => ({
title: _t("Average amount of t-shirt by order this month"),
value: data.average_quantity,
}),
});

dashboardRegistry.add("nb_cancelled_orders", {
id: "nb_cancelled_orders",
description: _t("Number of cancelled orders this month"),
Component: NumberCard,
props: (data) => ({
title: _t("Number of cancelled orders this month"),
value: data.nb_cancelled_orders,
}),
});

dashboardRegistry.add("average_time", {
id: "average_time",
description: _t("Average time for an order"),
Component: NumberCard,
props: (data) => ({
title: _t("Average time for an order to go from 'new' to 'sent' or 'cancelled'"),
value: data.average_time,
}),
});

dashboardRegistry.add("orders_by_size", {
id: "orders_by_size",
description: _t("Shirt orders by size"),
Component: PieChartCard,
size: 2,
props: (data) => ({
title: _t("Shirt orders by size"),
data: data.orders_by_size,
}),
});
16 changes: 16 additions & 0 deletions awesome_dashboard/static/src/dashboard/number_card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Component, xml } from "@odoo/owl";

export class NumberCard extends Component {
static props = {
title: String,
value: { optional: true },
};

static template = xml`
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always make our props explicit. Or at least as much as possible. It helps everyone reading the component and debugging. It's probably the only piece of documentation we try to write on every single component 😄

Suggested change
static template = xml`
static props = {
title: String,
value: { optional: true },
};
static template = xml`

<t t-name="awesome_dashboard.NumberCard">
<div class="d-flex flex-column align-items-center">
<h3 t-out="props.title" class="text-muted"/>
<div class="display-1" t-out="props.value"/>
</div>
</t>`;
}
54 changes: 54 additions & 0 deletions awesome_dashboard/static/src/dashboard/pie_chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Component, onWillStart, useRef, onMounted, onWillUnmount, useEffect } from "@odoo/owl";
import { loadJS } from "@web/core/assets";

export class PieChart extends Component {
static props = {
data: Object
}

static template = "awesome_dashboard.PieChart";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, let's make the props explicit 👍


setup() {
this.canvasRef = useRef("canvas");
this.chart = null;
onWillStart(async () => {
await loadJS("/web/static/lib/Chart/Chart.js");
});
onMounted(() => {
this.renderChart();
});
onWillUnmount(() => {
this.chart?.destroy();
});
useEffect(() => {
this.renderChart();
}, () => [this.props.data]);
}

renderChart() {
if (!this.props.data) {
return;
}
if (this.chart) {
this.chart.destroy();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The chart is destroyed before re-rendering, which is good, but it is never destroyed when the component is unmounted. Chart.js keeps an object attached to the canvas, so cleaning it up teaches the right lifecycle habit for external libraries. Leaving it as-is accepts a small memory/resource leak when the dashboard is left or reloaded. What I would do instead is import onWillUnmount and destroy the chart there to clean it up a bit. 👍

# awesome_dashboard/static/src/dashboard/pie_chart.js (setup): destroy the Chart.js instance when Owl removes the component.
        onWillUnmount(() => {
            this.chart?.destroy();
        });

}
const config = {
type: 'pie',
data: {
labels: Object.keys(this.props.data),
datasets: [{
label: 'T-shirt sizes',
data: Object.values(this.props.data),
backgroundColor: [
'rgb(255, 99, 132)',
'rgb(54, 162, 235)',
'rgb(255, 205, 86)',
'rgb(75, 192, 192)',
'rgb(153, 102, 255)'
],
}],
},
};
this.chart = new Chart(this.canvasRef.el, config);
}
}
8 changes: 8 additions & 0 deletions awesome_dashboard/static/src/dashboard/pie_chart.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="awesome_dashboard.PieChart">
<div class="pie-chart-container d-flex justify-content-center" style="position: relative; height:40vh;">
<canvas t-ref="canvas"/>
</div>
</t>
</templates>
Loading