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.

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


import { DashboardItem } from "./dashboard_item/dashboard_item";
import { PieChart } from "./pie_chart";

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) => {
return {
...item,
enabled: !this.props.disabledItems.includes(item.id),
}
}));
}

done() {
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.onUpdateConfiguration(newDisabledItems);
}

}

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

setup() {
this.action = useService("action");
this.statService = useService("statistics");
this.statistics = this.statService.onUpdate;
this.items = registry.category("awesome_dashboard").getAll();
this.dialog = useService("dialog");
this.state = useState({
// NOTE; ConfigurationDialog performs saving into browser local storage
disabledItems: browser.localStorage.getItem("disabledDashboardItems")?.split(",") || [],
})

onWillStart(async () => {
const result = await this.statService.loadStatistics();
// WARN; Perform immediate/synchronous update of state because sub-components
// logic isn't safeguarded against undefined values.
for (const [key, value] of Object.entries(result)) {
this.statistics[key] = value;
}
});
}

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

async openLeads() {
this.action.doAction("crm.crm_lead_all_leads");
}

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

async updateConfiguration(newDisabledItems) {
this.state.disabledItems = 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-color: aquamarine;
}
46 changes: 46 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.AwesomeDashboard">
<div class="o_control_panel_main_buttons d-flex gap-1 d-empty-none d-print-none">
<button type="button" class="btn btn-primary" t-on-click.stop="openCustomers">
Customers
</button>
<button type="button" class="btn btn-primary" t-on-click.stop="openLeads">
Leads
</button>
<button type="button" t-on-click.stop="openSettings">
<i class="fa fa-gear"/>
</button>
</div>

<Layout
className="'o_dashboard'"
display="{controlPanel: {}}"
>
<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) : {'stats': statistics}"/>
<t t-component="item.Component" t-props="itemProp" />
</DashboardItem>
</t>
</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.onChange(ev, item)">
<t t-esc="item.description"/>
</CheckBox>
</t>
<t t-set-slot="footer">
<button class="btn btn-primary" t-on-click="done">
Done
</button>
</t>
</Dialog>
</t>

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

export class DashboardItem extends Component {
static template = "awesome_dashboard.DashboardItem";
static props = {
size: {
type: Number,
default: 1,
optional: true,
},
slots: {
type: Object,
}
}
}
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.DashboardItem">
<div class="card m-2" t-attf-style="width: {{18*props.size}}rem">
<t t-slot="default"/>
</div>
</t>

</templates>
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";
import { PieChartCard } from "./pie_chart_card";

const items = [
{
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",
Component: NumberCard,
props: (data) => ({
title: "Average time for an order to go from 'new' to 'sent' or 'cancelled'",
value: data.average_time,
})
},
{
id: "number_new_orders",
description: "New orders this month",
Component: NumberCard,
props: (data) => ({
title: "Number of new orders this month",
value: data.nb_new_orders,
})
},
{
id: "cancelled_orders",
description: "Cancelled orders this month",
Component: NumberCard,
props: (data) => ({
title: "Number of cancelled orders this month",
value: data.nb_cancelled_orders,
})
},
{
id: "amount_new_orders",
description: "amount orders this month",
Component: NumberCard,
props: (data) => ({
title: "Total amount of new orders this month",
value: data.total_amount,
})
},
{
id: "pie_chart",
description: "Shirt orders by size",
Component: PieChartCard,
size: 2,
props: (data) => ({
title: "Shirt orders by size",
values: data.orders_by_size,
})
}
];

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

export class NumberCard extends Component {
static template = "awesome_dashboard.NumberCard";
static props = {
size: {
type: Number,
default: 1,
optional: true,
},
title: {type: String},
value: {type: String},
slots: {
type: Object,
}
}
}
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"/>
<t t-esc="props.value"/>
</t>

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

export class PieChartCard extends Component {
static components = { PieChart };
static template = "awesome_dashboard.PieChartCard";
static props = {
size: {
type: Number,
default: 1,
optional: true,
},
title: {type: String},
values: {type: Object},
slots: {
type: Object,
}
}
}
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.PieChartCard">
<t t-esc="props.title"/>
<PieChart stats="props.values"/>
</t>

</templates>
66 changes: 66 additions & 0 deletions awesome_dashboard/static/src/dashboard/pie_chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Component, onWillStart, useEffect, onWillUnmount, useRef } from "@odoo/owl";
import { loadJS } from "@web/core/assets";

export class PieChart extends Component {
static template = "awesome_dashboard.PieChart";
static props = {
// ERROR; Redirection through props/useState fails meta-function application!
// eg this.props.stats.keys() won't return what you think!
stats: {
type: Object,
shape: {
m: Number,
s: Number,
xl: Number,
}
},
slots: { type: Object, optional: true },
};

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

onWillStart(async () => {
// Injects the script directly into the frontend
await loadJS("/web/static/lib/Chart/Chart.js");
});
useEffect(() => this.renderChart());
onWillUnmount(this.onWillUnmount);
}

onWillUnmount() {
if (this.chart) {
this.chart.destroy();
}
}

getChartConfig() {
const stats = this.props.stats;
console.log(stats);
return {
type: 'doughnut',
data: {
datasets: [{
data: [stats.m, stats.s, stats.xl],
}],
labels: ["m", "s", "xl"],
},
options: {},
};
}

// NOTE; Ripped from https://github.com/odoo/odoo/blob/1f4e583ba20a01f4c44b0a4ada42c4d3bb074273/addons/web/static/src/views/graph/graph_renderer.js#L618
renderChart() {
if (this.chart) {
this.chart.destroy();
}
const config = this.getChartConfig();
this.chart = new Chart(this.canvasRef.el, config);
// To perform its animations, ChartJS will perform each animation
// step in the next animation frame. The initial rendering itself
// is delayed for consistency. We can avoid this by manually
// advancing the animation service.
// (?) Chart.animationService.advance();
}
}
Loading