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
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.

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

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

setup() {
this.action = useService("action");
this.statistics = useState(useService("awesome_dashboard.statistics"));
this.dialog = useService("dialog");
this.items = registry.category("awesome_dashboard").getAll();

this.state = useState({
disabledItems: browser.localStorage.getItem("disabledDashboardItems")?.split(",") || []
});
}

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

openConfig() {
this.dialog.add(ConfigDialog, {
items: this.items,
disabledItems: this.state.disabledItems,
onUpdateConfig: this.updateConfig.bind(this),
})
}

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

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

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

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

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

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

browser.localStorage.setItem(
"disabledDashboardItems",
newDisabledItems,
);

this.props.onUpdateConfig(newDisabledItems);
}
}

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: grey;
}
44 changes: 44 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.awesome_dashboard">
<Layout display="{ controlPanel: {} }" className="'o_dashboard h-100'">
<t t-set-slot="layout-buttons">
<div class="d-inline-flex gap-2 mx-2">
<button class="btn btn-primary d-flex align-items-center gap-2" t-on-click="openCustomerList">
<i class="fa fa-users"/><span>Customers</span>
</button>
<button class="btn btn-secondary d-flex align-items-center gap-2" t-on-click="openLeadsList">
<i class="fa fa-vcard-o"/><span>Leads</span>
</button>
<button name="dashboard_config" t-on-click="openConfig">
<i class="fa fa-gear"/>
</button>
</div>
</t>
<div class="j-flex flex-wrap">
<t t-foreach="items" t-as="item" t-key="item.id">
<DashboardItem t-if="!state.disabledItems.includes(item.id)" 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>
<t t-name="awesome_dashboard.config_dialog">
<Dialog title="'Dashboard Items Configuration'">
<t t-foreach="items" t-as="item" t-key="item.id">
<CheckBox value="item.enabled" onChange="(ev) => this.onChange(ev, item)">
<t t-out="item.description"/>
</CheckBox>
</t>
<t t-set-slot="footer">
<button class="btn btn-primary" t-on-click="apply">
Apply
</button>
</t>
</Dialog>
</t>

</templates>
64 changes: 64 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { registry } from "@web/core/registry";
import { NumberCard } from "../number_card/number_card";
import { PiechartCard } from "../piechart_card/piechart_card";

export const items = [
{
id: "average_quantity",
description: "Average amount of t-shirts",
Component: NumberCard,
props: (data) => ({
title: "Average amount of t-shirt by order this month",
value: data.average_quantity,
}),
},
{
id: "average_time",
description: "Average order time",
Component: NumberCard,
props: (data) => ({
title: "Average time for an order to reach a final state.",
value: data.average_time,
}),
},
{
id: "nb_new_orders",
description: "New orders this month",
Component: NumberCard,
props: (data) => ({
title: "Number of new orders this month",
value: data.nb_new_orders,
}),
},
{
id: "nb_cancelled_orders",
description: "Cancelled orders this month",
Component: NumberCard,
props: (data) => ({
title: "Number of cancelled orders this month",
value: data.nb_cancelled_orders,
}),
},
{
id: "total_amount",
description: "Total new orders this month",
Component: NumberCard,
props: (data) => ({
title: "Total amount of new orders this month",
value: data.total_amount,
}),
},
{
id: "orders_by_size",
description: "Shirt orders by size",
Component: PiechartCard,
props: (data) => ({
title: "Shirt orders by size",
values: data.orders_by_size,
}),
},
];

items.forEach(item => {
registry.category("awesome_dashboard").add(item.id, item);
});
10 changes: 10 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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'" props="props"/>`;
}

registry.category("actions").add("awesome_dashboard.dashboard", AwesomeDashboardLoader);
19 changes: 19 additions & 0 deletions awesome_dashboard/static/src/dashboard_item/dashboard_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Component } from "@odoo/owl";

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

get size() {
return this.props.size || 1;
}
}
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.dashboard_item">
<div class="dashboard-card p-4 m-2 d-inline-block align-top rounded-3 bg-white text-start"
t-att-style="'width: ' + (18 * size) + 'rem;'">
<t t-slot="default"/>
</div>
</t>
</templates>
13 changes: 13 additions & 0 deletions awesome_dashboard/static/src/number_card/number_card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Component } from "@odoo/owl";

export class NumberCard extends Component {
static template = "awesome_dashboard.number_card";
static props = {
title: {
type: String,
},
value: {
type: Number,
},
};
}
9 changes: 9 additions & 0 deletions awesome_dashboard/static/src/number_card/number_card.xml
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.number_card">
<h3>
<t t-esc="props.title"/>
</h3>
<t t-out="props.value"/>
</t>
</templates>
48 changes: 48 additions & 0 deletions awesome_dashboard/static/src/piechart/piechart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Component, onMounted, onPatched, onWillStart, onWillUnmount, useRef } from "@odoo/owl"
import { loadJS } from "@web/core/assets";

export class Piechart extends Component {
static template = "awesome_dashboard.piechart";
static props = {
title: String,
data: {
type: Object,
optional: true,
},
};

setup() {
this.canvasRef = useRef("canvas");

onWillStart(async () => loadJS("/web/static/lib/Chart/Chart.js"));
onWillUnmount(() => {
this.chart.destroy();
});
onMounted(() => {
this.renderChart();
});
onPatched(() => {
this.chart?.destroy();
this.renderChart();
});

}

renderChart() {
const chartData = this.props.data || {};
const labels = Object.keys(chartData);
const data = Object.values(chartData);
this.chart = new Chart(this.canvasRef.el, {
type: "pie",
data: {
labels: labels,
datasets: [
{
label: this.props.title,
data: data,
},
],
},
});
}
}
10 changes: 10 additions & 0 deletions awesome_dashboard/static/src/piechart/piechart.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'" t-ref="root">
<div class="h-100 position-relative" t-ref="container">
<canvas t-ref="canvas" />
</div>
</div>
</t>
</templates>
15 changes: 15 additions & 0 deletions awesome_dashboard/static/src/piechart_card/piechart_card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Component } from "@odoo/owl";
import { Piechart } from "../piechart/piechart";

export class PiechartCard extends Component {
static template = "awesome_dashboard.piechart_card";
static components = { Piechart: Piechart };
static props = {
title: {
type: String,
},
values: {
type: Object,
},
};
}
9 changes: 9 additions & 0 deletions awesome_dashboard/static/src/piechart_card/piechart_card.xml
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.piechart_card">
<h3>
<t t-esc="props.title"/>
</h3>
<Piechart title="props.title" data="props.values"/>
</t>
</templates>
22 changes: 22 additions & 0 deletions awesome_dashboard/static/src/services/statistics_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { registry } from "@web/core/registry";
import { rpc } from "@web/core/network/rpc";
import { reactive } from "@odoo/owl";

const statisticsService = {
dependencies: [],

start(_env) {
const statistics = reactive({ isReady: false });
const loadData = async () => {
const updates = await rpc("/awesome_dashboard/statistics");
Object.assign(statistics, updates, { isReady: true });
};

setInterval(loadData, 3 * 1000);
loadData();

return statistics;
},
};

registry.category("services").add("awesome_dashboard.statistics", statisticsService);
Loading