Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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/*'), # Lazy loaded, so remove from main bundle
],
'awesome_dashboard.dashboard': [
'awesome_dashboard/static/src/dashboard/*' # create new bundle to be lazy loaded
]
},
'license': 'AGPL-3'
}
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,39 @@
import { Dialog } from "@web/core/dialog/dialog";
import { CheckBox } from "@web/core/checkbox/checkbox";
import { browser } from "@web/core/browser/browser";
import { Component, useState } from "@odoo/owl";

export class ConfigurationDialog extends Component {
static template = "awesome_dashboard.ConfigurationDialog";
static components = { Dialog, CheckBox };

static props = {
items: Array,
disabledItems: Array,
onUpdateConfiguration: Function,
};


setup() {
this.checkBoxItems = useState(this.props.items.map((item) => { // Add 'enabled' field to Item -> return Array of Objects

Choose a reason for hiding this comment

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

Suggested change
this.checkBoxItems = useState(this.props.items.map((item) => { // Add 'enabled' field to Item -> return Array of Objects
this.checkBoxItems = useState(this.props.items.map((item) => {

what's that comment for 😆

Copy link
Author

Choose a reason for hiding this comment

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

it's just a note for me, that item object have a new field now

return {
...item,
enabled: !this.props.disabledItems.includes(item.id),
}
}));
}

onChange(checked, changedItem) {
changedItem.enabled = checked;
}

onDone(){
const newDisabledItems = this.checkBoxItems.filter(item => !item.enabled).map(item => item.id);
browser.localStorage.setItem(
"disabledDashboardItems",
newDisabledItems,
);
this.props.onUpdateConfiguration(newDisabledItems);
this.props.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="awesome_dashboard.ConfigurationDialog">
<Dialog title="'Dashboard items configuration'">
Which cards do you whish to see ?
<t t-foreach="checkBoxItems" t-as="item" t-key="item.id">
<CheckBox value="item.enabled" onChange="(event) => this.onChange(event, item)">
<t t-esc="item.description"/>
</CheckBox>
</t>
<t t-set-slot="footer">
<button class="btn btn-primary" t-on-click="this.onDone">
Done
</button>
</t>
</Dialog>
</t>
</templates>
53 changes: 53 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { registry } from "@web/core/registry";
import { Layout } from "@web/search/layout";
import { useService } from "@web/core/utils/hooks";
import { browser } from "@web/core/browser/browser";

Choose a reason for hiding this comment

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

don't need it

import { PieChart } from "./pie_chart/pie_chart";
import { DashboardItem } from "./dashboard_item";
import { ConfigurationDialog } from "./configuration_dialog/configuration_dialog";
import { Component, useState } from "@odoo/owl";


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

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

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

openLeads(){
this.action.doAction({ // define the action inline
type: 'ir.actions.act_window',
name: 'Leads',
target: 'current',
res_model: 'crm.lead',
views: [
[false, 'form'],
[false, 'list']],
Comment on lines +41 to +42

Choose a reason for hiding this comment

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

It doesn't make much sense to give form then list. Because they can't open the list view from the form

});
}

openConfiguration(){

Choose a reason for hiding this comment

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

Suggested change
openConfiguration(){
openConfiguration() {

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

updateConfiguration(newDisabledItems){

Choose a reason for hiding this comment

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

Suggested change
updateConfiguration(newDisabledItems){
updateConfiguration(newDisabledItems) {

this.state.disabledItems = newDisabledItems // update the state

Choose a reason for hiding this comment

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

Suggested change
this.state.disabledItems = newDisabledItems // update the state
this.state.disabledItems = newDisabledItems

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

.o_dashboard_buttons {
display: flex;
gap: 0.5rem;
}
27 changes: 27 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.AwesomeDashboard">
<Layout className="'o_dashboard h-100'">
<div class="o_dashboard_content d-flex align-items-center">
<div class="o_dashboard_buttons">
<button t-on-click="openCustomers" type="button" class="btn btn-primary mr-2">Customers</button>
<button t-on-click="openLeads" type="button" class="btn btn-secondary">Leads</button>
</div>
<div class="d-flex align-items-center" style="margin-left:10px">
<h3 class="m-0 me-2">Dashboard</h3>
<button t-on-click="openConfiguration" type="button" class="btn p-0 border-0">
<i class="fa fa-cog"/>
</button>
</div>
</div>
<t t-foreach="items" t-as="item" t-key="item.id">
<DashboardItem t-if="statistics.isReady and !state.disabledItems.includes(item.id)" size="item.size">
<t t-if="statistics.isReady">
<t t-component="item.Component" t-props="item.props(statistics)" />
</t>
</DashboardItem>
</t>
</Layout>
</t>
</templates>
9 changes: 9 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Component } from "@odoo/owl";

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

Choose a reason for hiding this comment

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

Suggested change
size: { type: Number, default: 1, optional: true },
size: { type: Number, optional: true },

This default: 1 doesn't do anything. If you want a default value for your props you should use defaultProps

slots: { type: Object, optional: true },
};
}
11 changes: 11 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_item.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.dashboard_item">
<div class="card d-inline-block m-2" t-attf-style="width: {{props.size *18}}rem;">
<div class="card-body d-flex justify-content-center align-items-center flex-column">
<t t-slot="default"/>
</div>
</div>
</t>
</templates>
65 changes: 65 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { NumberCard } from "./number_card/number_card";
import { PieChartCard } from "./pie_chart_card/pie_chart_card";
import { registry } from "@web/core/registry";

const items = [
{
id: "average_quantity",
description: "Average amount",
Component: NumberCard,
props: (data) => ({
title: "Average amount of t-shirt by order this month",

Choose a reason for hiding this comment

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

You can translate titles in this file

value: data.average_quantity
}),
},
{
id: "average_time",
description: "Average Time",
Component: NumberCard,
props: (data) => ({
title: "Average Time",
value: data.average_time
}),
},
{
id: "nb_new_orders",
description: "Number of new orders",
Component: NumberCard,
props: (data) => ({
title: "Number of new orders",
value: data.nb_new_orders
}),
},
{
id: "nb_cancelled_orders",
description: "Number of cancelled orders",
Component: NumberCard,
props: (data) => ({
title: "Number of cancelled orders",
value: data.nb_cancelled_orders
}),
},
{
id: "total_amount",
description: "Total amount",
Component: NumberCard,
props: (data) => ({
title: "Total amount",
value: data.total_amount
}),
},
{
id: "pie_chart",
description: "Shirts orders by size",
Component: PieChartCard,
size: 2,
props: (data) => ({
title: "Shirts orders by size",
value: data.orders_by_size
}),
},
]

items.forEach((item => {
registry.category("awesome_dashboard.items").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: { type: String },
value: { type: 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" owl="1">
<t t-esc="props.title"/>
<div class="fs-1 fw-bold text-success text-center">
<t t-esc="props.value"/>
</div>
</t>
</templates>

Choose a reason for hiding this comment

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

line at the end of the file

69 changes: 69 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,69 @@
import { loadJS } from "@web/core/assets";
import { Component, onWillStart, onMounted, onWillUnmount, useEffect, useRef } from "@odoo/owl";

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

Choose a reason for hiding this comment

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

Suggested change
static template = "awesome_dashboard.pie_chart";
static template = "awesome_dashboard.PieChart";

name templates in PascalCase always

static props = {
title: String,
data: Object,
};

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

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

useEffect(() => {
if (this.pieChart) {
this.pieChart.destroy();
}
Comment on lines +21 to +23

Choose a reason for hiding this comment

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

You don't need to have checks like this in JS. You can be cooler if you do just this:

Suggested change
if (this.pieChart) {
this.pieChart.destroy();
}
this.pieChart?.destroy();

The ? checks the left side and executes the function only if left side is not undefined

this.renderPieChart();
});
onWillUnmount(() => {
if (this.pieChart) {
this.pieChart.destroy();
}
Comment on lines +27 to +29

Choose a reason for hiding this comment

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

Same here

Suggested change
if (this.pieChart) {
this.pieChart.destroy();
}
this.pieChart?.destroy();

});
}

renderPieChart() {
if (!this.canvasRef.el || !this.props.data) {
return;
}

const key = Object.keys(this.props.data);
const value = Object.values(this.props.data);
const backgroundColor = [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
];
const borderColor = [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
];
Comment on lines +39 to +54

Choose a reason for hiding this comment

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

You're really specific with it I like it 😆


this.pieChart = new Chart(this.canvasRef.el, {
type: 'pie',
data: {
labels: key,
datasets: [{
label: this.props.title,
data: value,
backgroundColor: backgroundColor,
borderColor: borderColor,
borderWidth: 1
}]
},
});
}
}
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.pie_chart">
<h4 t-esc="props.title"/>
<canvas t-ref="canvas" width="400" height="400"></canvas>
</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: { type: String },
value: { type: Object },

Choose a reason for hiding this comment

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

Suggested change
title: { type: String },
value: { type: Object },
title: String,
value: Object,

}
}

Choose a reason for hiding this comment

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

line at the end of the file

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" owl="1">
<t t-esc="props.title"/>
<PieChart title="''" data="props.value"/>

Choose a reason for hiding this comment

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

Do you need to give it a title if it's empty

</t>
</templates>

Choose a reason for hiding this comment

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

Line at the end of the file

Loading