Skip to content

[ADD] awesome_owl, awesome_dashboard : Training OWL #793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 18.0
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions awesome_dashboard/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
'web.assets_backend': [
'awesome_dashboard/static/src/**/*',
],
'awesome_dashboard.dashboard': [
'awesome_dashboard/static/src/dashboard/**/*',
],
},
'license': 'AGPL-3'
}
10 changes: 0 additions & 10 deletions awesome_dashboard/static/src/dashboard.js

This file was deleted.

11 changes: 11 additions & 0 deletions awesome_dashboard/static/src/dashboard/cards/number_card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";

export class NumberCard extends Component {
static template = "awesome_dashboard.NumberCard";
static props = {
title: { type: String, optional: true },
value: { type: [String, Number] },
};
}
11 changes: 11 additions & 0 deletions awesome_dashboard/static/src/dashboard/cards/number_card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.NumberCard">
<div class="text-center">
<div class="fs-3" t-esc="props.title" />
<div class="text-success fs-1" t-esc="props.value" />
</div>
</t>

</templates>
13 changes: 13 additions & 0 deletions awesome_dashboard/static/src/dashboard/cards/pie_chart_card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";
import { PieChart } from "@awesome_dashboard/dashboard/charts/pie_chart";

export class PieChartCard extends Component {
static template = "awesome_dashboard.PieChartCard";
static components = { PieChart };
static props = {
title: { type: String, optional: true },
chart: { type: Object, optional: true, shape: { ...PieChart.props } },
};
}
11 changes: 11 additions & 0 deletions awesome_dashboard/static/src/dashboard/cards/pie_chart_card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.PieChartCard">
<div class="text-center">
<div class="fs-3" t-esc="props.title" />
<PieChart t-props="props.chart" />
</div>
</t>

</templates>
42 changes: 42 additions & 0 deletions awesome_dashboard/static/src/dashboard/charts/pie_chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/** @odoo-module **/

import { Component, onWillStart, onWillUnmount, useRef, useEffect } from "@odoo/owl";
import { loadJS } from "@web/core/assets";

export class PieChart extends Component {
static template = "awesome_dashboard.PieChart";
static props = {
data: { type: Object },
options: { type: Object, optional: true },
};

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

onWillStart(async () => {
await loadJS("/web/static/lib/Chart/Chart.js");
});
useEffect(() => this.renderChart());
onWillUnmount(this.onWillUnmount);
}

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

renderChart() {
if (this.chart) {
this.chart.destroy();
}
if (this.canvasRef.el) {
this.chart = new Chart(this.canvasRef.el, {
type: "pie",
data: this.props.data,
options: this.props.options,
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.AwesomeDashboard">
hello dashboard
<t t-name="awesome_dashboard.PieChart">
<canvas t-ref="canvas" />
</t>

</templates>
64 changes: 64 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/** @odoo-module **/

import { Component, useState, useEffect } from "@odoo/owl";
import { Layout } from "@web/search/layout";
import { useService } from "@web/core/utils/hooks";
import { AwesomeDashboardItem } from "@awesome_dashboard/dashboard/dashboard_item";
import { registry } from "@web/core/registry";
import { CogMenu } from "@web/search/cog_menu/cog_menu";
import { AwesomeDashboardConfigDialog } from "@awesome_dashboard/dashboard/dialogs/dashboard_config_dailog";

Choose a reason for hiding this comment

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

Suggested change
import { AwesomeDashboardConfigDialog } from "@awesome_dashboard/dashboard/dialogs/dashboard_config_dailog";
import { AwesomeDashboardConfigDialog } from "@awesome_dashboard/dashboard/dialogs/dashboard_config_dialog";

Misspelling, happens 🤷
Don't forget to rename your file too


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

setup() {
this.action = useService("action");
this.stats = useState(useService("awesome_dashboard_statistics"));
const config = JSON.parse(localStorage.getItem("awesome_dashboard_config"));
const data = registry.category("awesome_dashboard").get("data");
this.setActives(config, data);
this.items = useState({ data });
this.dialog = useService("dialog");
}

onClickCustomers() {
this.action.doAction({
type: "ir.actions.act_window",
name: "Customers",
target: "current",
res_model: "res.partner",
views: [[false, "kanban"]],
});
}

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

setActives(ref, toUpdate) {
for (const key in ref) {
const entry = ref[key];
const dataItem = toUpdate[key];
if (dataItem) {
dataItem.active = entry.active;
}
}
}

openConfiguration() {
this.dialog.add(AwesomeDashboardConfigDialog, {
onConfirm: (configItems) => {
this.setActives(configItems, this.items.data);
},
});
}
}

registry.category("lazy_components").add("awesome_dashboard.AwesomeDashboard", AwesomeDashboard);
19 changes: 19 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.o_dashboard {
background-color: #f0f0f0;
padding: 20px;

.o_dashboard_items {
display: flex;
flex-wrap: wrap;
align-items: stretch;
gap: 20px;

.o_dashboard_item {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 5px;
padding: 10px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
}
}
33 changes: 33 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.AwesomeDashboard">
<Layout display="{ controlPanel: {} }" className="'o_dashboard o_awesome_dashboard h-100'">
<t t-set-slot="layout-buttons">
<div class="o_list_buttons d-flex gap-1 d-empty-none align-items-baseline" role="toolbar" aria-label="Main actions">
<button type="button" class="btn btn-primary" t-on-click="onClickCustomers">
Customers
</button>
<button type="button" class="btn btn-primary" t-on-click="onClickLeads">
Leads
</button>
</div>
</t>
<t t-set-slot="control-panel-additional-actions">
<button t-on-click="openConfiguration" class="btn p-0 ms-1 border-0">
<i class="fa fa-cog"></i>
</button>
</t>
<div class="o_dashboard_items">
<t t-foreach="Object.entries(items.data)" t-as="entry" t-key="entry[0]">
<t t-set="key" t-value="entry[0]"/>
<t t-set="item" t-value="entry[1]"/>
<AwesomeDashboardItem t-if="item.active" size="item.size">
<t t-component="item.Component" t-props="item.props(stats)" />
</AwesomeDashboardItem>
</t>
</div>
</Layout>
</t>

</templates>
12 changes: 12 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";

export class AwesomeDashboardItem extends Component {
static template = "awesome_dashboard.AwesomeDashboardItem";
static props = {
size: { type: Number, optional: true },
slots: { type: Object },
};
static defaultProps = { size: 1 };
}
10 changes: 10 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_item.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.AwesomeDashboardItem">
<div class="o_dashboard_item" t-attf-style="min-width: {{ props.size * 18}}rem;">
<t t-slot="default"/>
</div>
</t>

</templates>
72 changes: 72 additions & 0 deletions awesome_dashboard/static/src/dashboard/data/dashboard_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/** @odoo-module **/

import { registry } from "@web/core/registry";
import { PieChartCard } from "@awesome_dashboard/dashboard/cards/pie_chart_card";
import { NumberCard } from "@awesome_dashboard/dashboard/cards/number_card";

registry.category("awesome_dashboard").add("data", {
average_quantity: {
Component: NumberCard,
props: (stats) => ({
title: "Average amount of t-shirt by order this month",
value: stats.data.average_quantity,
}),
active: true,
},
average_time: {
Component: NumberCard,
props: (stats) => ({
title: "Average time to deliver an order this month",
value: stats.data.average_time,
}),
active: true,
},
nb_cancelled_orders: {
Component: NumberCard,
size: 2.5,
props: (stats) => ({
title: "Number of cancelled orders this month",
value: stats.data.nb_cancelled_orders,
}),
active: true,
},
nb_new_orders: {
Component: NumberCard,
size: 2.5,
props: (stats) => ({
title: "Number of new orders this month",
value: stats.data.nb_new_orders,
}),
active: true,
},
total_amount: {
Component: NumberCard,
size: 2.5,
props: (stats) => ({
title: "Total amount of orders this month",
value: stats.data.total_amount,
}),
active: true,
},
orders_by_size: {
Component: PieChartCard,
props: (stats) => ({
title: "Orders by size",
chart: {
data: {
datasets: [
{
data: [
stats.data.orders_by_size.m,
stats.data.orders_by_size.s,
stats.data.orders_by_size.xl,
],
},
],
labels: ["M", "S", "XL"],
},
},
}),
active: true,
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";
import { Dialog } from "@web/core/dialog/dialog";

export class AwesomeDashboardConfigDialog extends Component {
static template = "awesome_dashboard.AwesomeDashboardConfigDialog";
static components = { Dialog };
static props = { close: { type: Function, optional: true }, onConfirm: { type: Function, optional: true } };
static defaultProps = {
close: () => {},
onConfirm: () => {},
};

setup() {
if (localStorage.getItem("awesome_dashboard_config") === null) {
localStorage.setItem(
"awesome_dashboard_config",
JSON.stringify({
average_quantity: { title: "Average Quantity", active: true },
average_time: { title: "Average Time", active: true },
nb_cancelled_orders: { title: "Cancelled Orders", active: true },
nb_new_orders: { title: "New Orders", active: true },
total_amount: { title: "Total Amount", active: true },
orders_by_size: { title: "Orders by Size", active: true },
})
);
}
this.configItems = JSON.parse(localStorage.getItem("awesome_dashboard_config"));
}

onSave() {
localStorage.setItem("awesome_dashboard_config", JSON.stringify(this.configItems));
this.props.onConfirm(this.configItems);
this.props.close();
}

discard() {
this.props.close();
}
}
Loading