Skip to content

[ADD] estate, awesome_dashboard, awesome_owl: tutorial chapters #852

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

Draft
wants to merge 15 commits into
base: 18.0
Choose a base branch
from
Draft
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
49 changes: 29 additions & 20 deletions awesome_dashboard/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
# -*- coding: utf-8 -*-
{
'name': "Awesome Dashboard",

'summary': """
"name": "Awesome Dashboard",

Choose a reason for hiding this comment

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

Don't make unnecessary changes.

"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/AwesomeDashboard',
'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/AwesomeDashboard",
"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/dashboard_action.js",
"awesome_dashboard/static/src/dashboard/dashboard_action.xml",
"awesome_dashboard/static/src/dashboard/services/statistics.js",
],
"awesome_dashboard.dashboard": [
"awesome_dashboard/static/src/dashboard/services/statistics.js",
"awesome_dashboard/static/src/dashboard/components/dashboard_settings_dialog.js",
"awesome_dashboard/static/src/dashboard/components/dashboard_settings_dialog.xml",
"awesome_dashboard/static/src/dashboard/dashboard.js",
"awesome_dashboard/static/src/dashboard/dashboard_items.js",
"awesome_dashboard/static/src/dashboard/components/*.js",
"awesome_dashboard/static/src/dashboard/components/*.xml",
"awesome_dashboard/static/src/dashboard/*.xml",
"awesome_dashboard/static/src/dashboard/*.scss",
],
},
'license': 'AGPL-3'
"license": "AGPL-3",
}
2 changes: 1 addition & 1 deletion awesome_dashboard/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

from . import controllers
from . import controllers
22 changes: 11 additions & 11 deletions awesome_dashboard/controllers/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

logger = logging.getLogger(__name__)


Choose a reason for hiding this comment

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

unnecessary extra line.

class AwesomeDashboard(http.Controller):
@http.route('/awesome_dashboard/statistics', type='json', auth='user')
@http.route("/awesome_dashboard/statistics", type="json", auth="user")
def get_statistics(self):
"""
Returns a dict of statistics about the orders:
Expand All @@ -22,15 +23,14 @@ def get_statistics(self):
"""

return {
'average_quantity': random.randint(4, 12),
'average_time': random.randint(4, 123),
'nb_cancelled_orders': random.randint(0, 50),
'nb_new_orders': random.randint(10, 200),
'orders_by_size': {
'm': random.randint(0, 150),
's': random.randint(0, 150),
'xl': random.randint(0, 150),
"average_quantity": random.randint(4, 12),
"average_time": random.randint(4, 123),
"nb_cancelled_orders": random.randint(0, 50),
"nb_new_orders": random.randint(10, 200),
"orders_by_size": {
"m": random.randint(0, 150),
"s": random.randint(0, 150),
"xl": random.randint(0, 150),
},
'total_amount': random.randint(100, 1000)
"total_amount": random.randint(100, 1000),
}

Choose a reason for hiding this comment

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

unnecessary changes.

10 changes: 0 additions & 10 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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @odoo-module **/

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 }, };
}
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.DashboardItem" owl="1">
<t t-set="itemSize" t-value="props.size || 1"/>
<div t-attf-style="width: #{18 * itemSize}rem; background: #fff; border-radius: 0.75rem; box-shadow: 0 2px 8px rgba(0,0,0,0.07); padding: 1rem; margin: 1rem; display: inline-flex; justify-content: center; vertical-align: top; min-height: 3rem;">
<t t-slot="default"/>
</div>
</t>
</templates>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/** @odoo-module **/

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

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

setup() {
const allItems = registry.category("awesome_dashboard_items").getAll();
const removedIds = JSON.parse(localStorage.getItem("awesome_dashboard.hidden_items") || "[]");

// Local state with checkboxes
this.items = useState(
allItems.map((item) => ({
...item,
visible: !removedIds.includes(item.id),
}))
);
}

onApply() {
const hiddenIds = this.items.filter(item => !item.visible).map(item => item.id);
localStorage.setItem("awesome_dashboard.hidden_items", JSON.stringify(hiddenIds));
this.props.onClose(); // closes the dialog
window.location.reload(); // refresh to re-render dashboard with updated state
}

onToggleItem(item) {
item.visible = !item.visible;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="awesome_dashboard.DashboardSettingsDialog" owl="1">
<Dialog title="'Dashboard Settings'" t-on-close="props.onClose">
<div class="p-3">
<t t-foreach="items" t-as="item" t-key="item.id">
<div class="form-check mb-2">
<input
type="checkbox"
class="form-check-input"
t-att-checked="item.visible"
t-on-click="() => onToggleItem(item)"
t-att-id="'check_' + item.id"
/>
<label class="form-check-label" t-att-for="'check_' + item.id">
<t t-esc="item.description"/>
</label>
</div>
</t>
</div>

<t t-set-slot="footer">
<div class="text-end">
<button class="btn btn-primary" t-on-click="onApply">Apply</button>
</div>
</t>
</Dialog>
</t>
</templates>
11 changes: 11 additions & 0 deletions awesome_dashboard/static/src/dashboard/components/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: String,
value: [String, Number]
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<templates xml:space="preserve">
<t t-name="awesome_dashboard.NumberCard" owl="1">
<div class="o_dashboard_stat_block">
<span class="o_dashboard_stat_label"><strong><t t-esc="props.title" /></strong></span>
<span class="o_dashboard_stat_value"><t t-esc="props.value" /></span>
</div>
</t>
</templates>
80 changes: 80 additions & 0 deletions awesome_dashboard/static/src/dashboard/components/pie_chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/** @odoo-module **/

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

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

static props = {
data: { type: Object },
};

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

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

// Chart data setup
this.chartData = {
labels: Object.keys(this.props.data),
datasets: [
{
data: Object.values(this.props.data),
backgroundColor: [
"#FF6384", "#36A2EB", "#FFCE56", "#66BB6A", "#BA68C8",
],
},
],
};

onMounted(() => {
this.makeChart();
});

// Cleanup
this.cleanupChart = () => {
if (this.chart) {
this.chart.destroy();
this.chart = null;
}
};

onWillUnmount(this.cleanupChart);

// Reactive effect on props.data
useEffect(() => {
this.cleanupChart();
if (this.canvasRef.el) {
this.makeChart();
}
}, () => [this.props.data]);
}

makeChart() {
this.chart = new Chart(this.canvasRef.el, {
type: "pie",
data: this.chartData,
options: {
responsive: true,
maintainAspectRatio: true,
plugins: {
legend: { position: 'bottom' },
title: {
display: true,
text: "T-Shirts Sold by Size",
},
},
onClick: (event, elements) => {
if (elements.length > 0) {
const index = elements[0].index;
const label = this.chartData.labels[index];
}
},
},
});
}
}
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" owl="1">
<div style="width:100%; height:100%;">
<canvas t-ref="chart" style="width: 100%; height: 300px;"></canvas>
</div>
</t>
</templates>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";
import { PieChart } from "./pie_chart";

export class PieChartCard extends Component {
static template = "awesome_dashboard.PieChartCard";
static components = { PieChart };
static props = {
data: Object,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<templates xml:space="preserve">
<t t-name="awesome_dashboard.PieChartCard" owl="1">
<PieChart t-props="{ data: props.data }" />
</t>
</templates>
51 changes: 51 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/** @odoo-module **/

import { Component, useState, onWillStart } from "@odoo/owl";
import { Layout } from "@web/search/layout";
import { registry } from "@web/core/registry";
import { useService } from "@web/core/utils/hooks";
import { rpc } from "@web/core/network/rpc";
import { DashboardItem } from "./components/dashboard_item";
import { PieChart } from "./components/pie_chart";
import "./dashboard_items.js";
import "./services/statistics.js";
import { showDialog } from "@web/core/dialog/dialog";
import { DashboardSettingsDialog } from "./components/dashboard_settings_dialog";

class AwesomeDashboard extends Component {
static template = "awesome_dashboard.AwesomeDashboard";
static components = { Layout, DashboardItem, PieChart };
setup() {
this.action = useService("action");
const { statistics } = useService("awesome_dashboard.statistics");
this.state = useState(statistics);

const itemRegistry = registry.category("awesome_dashboard_items");
const allItems = itemRegistry.getAll();

const hiddenIds = JSON.parse(localStorage.getItem("awesome_dashboard.hidden_items") || "[]");
this.items = allItems.filter((item) => !hiddenIds.includes(item.id));

this.dialog = useService("dialog");
}


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

openSettingsDialog() {
this.dialog.add(DashboardSettingsDialog, {
onClose: () => { },
});
}

}

registry.category("lazy_components").add("awesome_dashboard.AwesomeDashboard", AwesomeDashboard);

Choose a reason for hiding this comment

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

There should always be one blank line at the end of the file.

Loading