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
4 changes: 4 additions & 0 deletions awesome_dashboard/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
'assets': {
'web.assets_backend': [
'awesome_dashboard/static/src/**/*',
('remove', 'awesome_dashboard/static/src/dashboard/**/*'),
],
'awesome_dashboard.dashboard': [
'awesome_dashboard/static/src/dashboard/**/*'
]
},
'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.

82 changes: 82 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Component, useState } from "@odoo/owl";
import { browser } from "@web/core/browser/browser";
import { CheckBox } from "@web/core/checkbox/checkbox";
import { Dialog } from "@web/core/dialog/dialog";
import { registry } from "@web/core/registry";
import { useService } from "@web/core/utils/hooks";
import { Layout } from "@web/search/layout";
import { DashboardItem } from "./dashboard_item/dashboard_item";

class AwesomeDashboard extends Component {
static template = "awesome_dashboard.AwesomeDashboard";
static components = { Layout, DashboardItem };

setup() {
this.action = useService("action");
this.statistics = useState(useService("awesome_dashboard.statistics"));
this.items = registry.category("awesome_dashboard").getAll();
this.display = {
controlPanel: {},
};
this.dialog = useService("dialog");
this.state = useState({
disabledItems: browser.localStorage.getItem("disabledDashboardItems")?.split(",") || []
});
}

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

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

openConfiguration() {
this.dialog.add(ConfigurationDialog, {
items: this.items,
disabledItems: this.state.disabledItems,
onUpdateConfiguration: this.updateConfiguration.bind(this),
})
}

updateConfiguration(newDisabledItems) {
this.state.disabledItems = newDisabledItems;
}
}

class ConfigurationDialog extends Component {
static template = "awesome_dashboard.ConfigurationDialog";
static components = { Dialog, CheckBox };
static props = ["close", "items", "disabledItems", "onUpdateConfiguration"];

setup() {
this.items = useState(this.props.items.map((item) => (
{ ...item, enabled: !this.props.disabledItems.includes(item.id) }
)));
}

toggleCheckbox(checked, toggledItem) {
toggledItem.enabled = checked;
const newDisabledItems = Object.values(this.items).filter(
(item) => !item.enabled
).map((item) => item.id);

browser.localStorage.setItem("disabledDashboardItems", newDisabledItems);
this.props.onUpdateConfiguration(newDisabledItems);
}

done() {
this.props.close();
}
}

registry.category("lazy_components").add("AwesomeDashboard", AwesomeDashboard);
3 changes: 3 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.o_dashboard {
background-color: gray;
}
42 changes: 42 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.AwesomeDashboard">
<Layout display="display" className="'o_dashboard h-100'">
<t t-set-slot="layout-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 t-on-click="openConfiguration" class="btn p-0 ms-1">
<i class="fa 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-if="!state.disabledItems.includes(item.id)">
<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>

<t t-name="awesome_dashboard.ConfigurationDialog">
<Dialog title="'Dashboard items configuration'">
Which cards do you whish to see ?
<t t-foreach="items" t-as="item" t-key="item.id">
<CheckBox value="item.enabled" onChange="(ev) => this.toggleCheckbox(ev, item)">

Choose a reason for hiding this comment

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

A small detail: we usually reserve ev name for variables that contain events. Here it just contains the new value

<t t-esc="item.description"/>
</CheckBox>
</t>
<t t-set-slot="footer">
<button class="btn btn-primary" t-on-click="done">
Apply
</button>
</t>
</Dialog>
</t>

</templates>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Component } from "@odoo/owl";

export class DashboardItem extends Component {
static template = "awesome_dashboard.DashboardItem"
static props = {
slots: {
type: Object,
shape: { default: Object },
},
size: {
type: Number,
optional: true,
default: 1,

Choose a reason for hiding this comment

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

This line does not set a default value for the size props. You can confirm that by removing the fallback || 1 (where DashboardItem is used) and see that the layout changes.
Look for static defaultProps to see how default values should be provided for props 😉

},
};
}
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="card m-2 border-dark" t-attf-style="width: {{18*props.size}}rem;">
<div class="card-body">
<t t-slot="default"/>
</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 { NumberCard } from "./number_card/number_card";
import { PieChartCard } from "./pie_chart_card/pie_chart_card";

const items = [
{
id: "nb_new_orders",
description: "Number of new orders this month",
Component: NumberCard,
size: 1,
props: (data) => ({
title: "Number of new orders this month",
value: data.nb_new_orders
})
},
{
id: "total_amount",
description: "Total amount of new orders this month",
Component: NumberCard,
size: 2,
props: (data) => ({
title: "Total amount of new orders this month",
value: data.total_amount
})
},
{
id: "nb_cancelled_orders",
description: "Number of cancelled orders this month",
Component: NumberCard,
props: (data) => ({
title: "Number of cancelled orders this month",
value: data.nb_cancelled_orders
})
},
{
id: "average_quantity",
description: "Average amount of t-shirt",
Component: NumberCard,
props: (data) => ({
title: "Average amount of t-shirt by order this month",
value: data.average_quantity
})
},
{
id: "average_time",
description: "Average time for an order to go from 'new' to 'sent' or 'cancelled'",
Component: NumberCard,
props: (data) => ({
title: "Average time for an order to go from 'new' to 'sent' or 'cancelled'",
value: data.average_time
})
},
{
id: "orders_by_size",
description: "Shirt orders by size",
Component: PieChartCard,
size: 2,
props: (data) => ({
title: "Shirt orders by size",
data: data.orders_by_size
})
},
];

items.forEach(item => {
registry.category("awesome_dashboard").add(item.id, item);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Component } from "@odoo/owl";

export class NumberCard extends Component {
static template = "awesome_dashboard.NumberCard";
static props = {
title: String,
value: Number,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="awesome_dashboard.NumberCard">
<t t-esc="props.title"/>
<div class="fs-1 fw-bold text-success text-center">
<t t-esc="props.value"/>
</div>
</t>
</templates>
39 changes: 39 additions & 0 deletions awesome_dashboard/static/src/dashboard/pie_chart/pie_chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { loadJS } from "@web/core/assets";
import { getColor } from "@web/core/colors/colors";
import { Component, onWillStart, useRef, onMounted, onWillUnmount } from "@odoo/owl";

export class PieChart extends Component {
static template = "awesome_dashboard.PieChart";
static props = {
data: Object,

Choose a reason for hiding this comment

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

It looks like the chart is not updated when the statistic service updates the values.
You can have a look at https://github.com/odoo/owl/blob/v2.8.2/doc/reference/hooks.md#lifecycle-hooks for a bt more information about the life-cycle hooks that you can use :)

};

setup() {
this.canvasRef = useRef("canvas");
onWillStart(() => loadJS("/web/static/lib/Chart/Chart.js"));
onMounted(() => {
this.renderChart();
});
onWillUnmount(() => {
this.chart.destroy();
});
}

renderChart() {
const labels = Object.keys(this.props.data);
const data = Object.values(this.props.data);
const color = labels.map((_, index) => getColor(index));
this.chart = new Chart(this.canvasRef.el, {
type: "pie",
data: {
labels: labels,
datasets: [
{
data: data,
backgroundColor: color,
},
],
},
});
}
}
10 changes: 10 additions & 0 deletions awesome_dashboard/static/src/dashboard/pie_chart/pie_chart.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="awesome_dashboard.PieChart">
<div t-att-class="'h-100 ' + props.class" t-ref="root">
<div class="h-100 position-relative" t-ref="container">
<canvas t-ref="canvas" />
</div>
</div>
</t>
</templates>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Component } from "@odoo/owl";
import { PieChart } from "../pie_chart/pie_chart";

export class PieChartCard extends Component {
static template = "awesome_dashboard.PieChartCard";
static components = { PieChart };
static props = {
title: String,
data: Object,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="awesome_dashboard.PieChartCard">
<t t-esc="props.title"/>
<PieChart data="props.data"/>
</t>
</templates>
22 changes: 22 additions & 0 deletions awesome_dashboard/static/src/dashboard/statistics_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { reactive } from "@odoo/owl";
import { rpc } from "@web/core/network/rpc";
import { registry } from "@web/core/registry";

const statisticsService = {

start() {
const statistics = reactive({ isReady: false });

Choose a reason for hiding this comment

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

It looks like the isReady field is not used anywhere else. If that is the case, I think you can remove it completely


async function loadData() {
const data = await rpc("/awesome_dashboard/statistics");
Object.assign(statistics, data, { isReady: true });
}

setInterval(loadData, 1000*60*10);
loadData();

return statistics;
},
}

registry.category("services").add("awesome_dashboard.statistics", statisticsService);
12 changes: 12 additions & 0 deletions awesome_dashboard/static/src/dashboard_action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Component, xml } from "@odoo/owl";
import { LazyComponent } from "@web/core/assets";
import { registry } from "@web/core/registry";

export class AwesomeDashboardLoader extends Component {
static components = { LazyComponent };
static template = xml`
<LazyComponent bundle="'awesome_dashboard.dashboard'" Component="'AwesomeDashboard'" />
`;
}

registry.category("actions").add("awesome_dashboard.dashboard", AwesomeDashboardLoader);
Loading