Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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,6 +24,10 @@
'assets': {
'web.assets_backend': [
'awesome_dashboard/static/src/**/*',
('remove', 'awesome_dashboard/static/src/dashboard/**/*'),
],
'awesome_dashboard.dashboard': [
'awesome_dashboard/static/src/dashboard/**/*',
],
},
'license': 'AGPL-3'
Expand Down
8 changes: 0 additions & 8 deletions awesome_dashboard/static/src/dashboard.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Dialog } from "@web/core/dialog/dialog";
import { Component } from "@odoo/owl";

export class DashboardDialog extends Component {
static template = "awesome_dashboard.configuration-dialog";
static props = {
items: { type: Array, element: Object },
disabledItems: { type: Array, element: String },
updateConfiguration: Function,
close: { type: Function, optional: true },
};
static components = { Dialog };

setup() {
this.items = this.props.items.map((item) => ({
...item,
disabled: this.props.disabledItems.includes(item.id),
}));
}

toggleMasked(item) {
item.disabled = !item.disabled;
}

applyConfigurationChanges() {
const disabledItems = this.items.filter((item) => item.disabled).map((item) => item.id);
this.props.updateConfiguration(disabledItems);
if (this.props.close) {
this.props.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.configuration-dialog">
<Dialog title="'Dashboard items configuration'">
<t t-set-slot="default">
<p>Which cards do you wish to see ?</p>
<t t-foreach="items" t-as="item" t-key="item.id">
<div class="form-check">
<input class="form-check-input" type="checkbox" t-on-change="() => toggleMasked(item)" t-att-id="item.id" t-att-checked="!item.disabled"/>
<label t-att-for="item.id">
<t t-esc="item.description"/>
</label>
</div>
</t>
</t>

<t t-set-slot="footer">
<button type="button" class="btn btn-primary" t-on-click="applyConfigurationChanges">Done</button>
</t>
</Dialog>
</t>

</templates>
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: Number,
slots: { type: Object, optional: true },
};
}
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.dashboard-item">
<div class="card d-inline-block m-2" t-attf-style="width: {{18*props.size}}rem;">
<t t-slot="default"/>
</div>
</t>

</templates>
63 changes: 63 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard-registry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { registry } from "@web/core/registry";
import { NumberCard } from "./number-card/number-card";
import { PieChartCard } from "./pie-chart/pie-chart-card";

const registryItems = [
{
id: "nb_new_orders",
description: "Number of new orders this month",
component: NumberCard,
props: (data) => ({
title: "Number of new orders this month",
value: data.nb_new_orders,
}),
},
{
id: "total_amount",
description: "Total amount of new orders this month",
component: NumberCard,
props: (data) => ({
title: "Total amount of new orders this month",
value: data.total_amount,
}),
},
{
id: "average_quantity",
description: "Average amount of t-shirt by order this month",
component: NumberCard,
props: (data) => ({
title: "Average amount of t-shirt by order this month",
value: data.average_quantity,
}),
},
{
id: "nb_cancelled_orders",
description: "Number of cancelled orders this month",
component: NumberCard,
props: (data) => ({
title: "Number of cancelled orders this month",
value: data.nb_cancelled_orders,
}),
},
{
id: "average_time",
description: "Average time for an order to go from 'new' to 'sent' or 'cancelled'",
component: NumberCard,
props: (data) => ({
title: "Average time for an order to go from 'new' to 'sent' or 'cancelled'",
value: data.average_time,
}),
},
{
id: "orders_by_size",
description: "Ordered T-shirts by size",
component: PieChartCard,
size: 2,
props: (data) => ({
title: "Ordered T-shirts by size",
value: data.orders_by_size,
}),
},
];

registryItems.forEach((item) => registry.category("awesome_dashboard").add(item.id, item));
59 changes: 59 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { registry } from "@web/core/registry";
import { useService, useOwnedDialogs } from "@web/core/utils/hooks";
import { Layout } from "@web/search/layout";
import { Component, useState } from "@odoo/owl";
import { DashboardItem } from "./dashboard-item/dashboard-item";
import { NumberCard } from "./number-card/number-card";
import { PieChartCard } from "./pie-chart/pie-chart-card";
import { DashboardDialog } from "./configuration-dialog/configuration-dialog";
import { browser } from "@web/core/browser/browser";

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

setup() {
this.action = useService("action");
this.statistics = useState(useService("awesome_dashboard.statistics"));
this.configDialog = useOwnedDialogs();

// Dashboard items
this.items = registry.category("awesome_dashboard").getAll();
this.state = useState({
disabledItems: JSON.parse(
browser.localStorage.getItem("awesome_dashboard.disabled") ?? "[]"
),
});
}

actionCustomers() {
this.action.doAction("base.action_partner_form", { viewType: "kanban" });
}

actionLeads() {
this.action.doAction({
type: "ir.actions.act_window",
name: "All leads",
res_model: "crm.lead",
views: [
[false, "list"],
[false, "form"],
],
});
}

updateConfiguration(disabledItems) {
browser.localStorage.setItem("awesome_dashboard.disabled", JSON.stringify(disabledItems));
this.state.disabledItems = disabledItems;
}

openDashboardConfig() {
this.configDialog(DashboardDialog, {
items: this.items,
disabledItems: this.state.disabledItems,
updateConfiguration: this.updateConfiguration.bind(this),
});
}
}

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

<t t-name="awesome_dashboard.AwesomeDashboard">
<Layout display="{controlPanel: {} }" className="'o_dashboard h-100'">
<t t-set-slot="control-panel-always-buttons">
<button type="button" class="btn btn-primary" t-on-click="actionCustomers">Customers</button>
<button type="button" class="btn btn-primary" t-on-click="actionLeads">Leads</button>
</t>
<t t-set-slot="control-panel-additional-actions">
<span role="button" class="fa fa-fw fa-cog" t-on-click="openDashboardConfig"/>
</t>
<t t-set-slot="default">
<div t-if="statistics.isReady">
<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>
</t>
</Layout>
</t>

</templates>
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.number-card";
static props = {
title: String,
value: Number,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.number-card-value {
color: green;
font-weight: bold;
text-align: center;
}
12 changes: 12 additions & 0 deletions awesome_dashboard/static/src/dashboard/number-card/number-card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.number-card">
<div class="card-body">
<h5 class="card-title" style="text-align: center;"><t t-esc="props.title"/></h5>
<br/>
<h1 class="number-card-value" t-esc="props.value"/>
</div>
</t>

</templates>
11 changes: 11 additions & 0 deletions awesome_dashboard/static/src/dashboard/pie-chart/pie-chart-card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Component } from "@odoo/owl";
import { PieChart } from "./pie-chart";

export class PieChartCard extends Component {
static template = "awesome_dashboard.pie-chart-card";
static props = {
title: String,
value: { type: Object, values: Number },
};
static components = { PieChart };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.pie-chart-card">
<div class="card-body">
<h5 class="card-title" style="text-align: center;"><t t-esc="props.title"/></h5>
<br/>
<PieChart data="props.value"/>
</div>
</t>

</templates>
57 changes: 57 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,57 @@
import {
Component,
onWillStart,
onWillUnmount,
onMounted,
useRef,
onWillUpdateProps,
} from "@odoo/owl";
import { loadJS } from "@web/core/assets";
import { DashboardItem } from "../dashboard-item/dashboard-item";

export class PieChart extends Component {
static template = "awesome_dashboard.pie-chart";
static components = { DashboardItem };
static props = {
data: { type: Object, values: Number },
};

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

onWillStart(() => loadJS("/web/static/lib/Chart/Chart.js"));
onMounted(this.renderChart);
onWillUpdateProps(this.updateChart);
onWillUnmount(this.destroyChart);
}

renderChart() {
this.destroyChart();

const labels = Object.keys(this.props.data);
const datapoints = Object.values(this.props.data);
this.chart = new Chart(this.canvasRef.el, {
type: "doughnut",
data: {
labels: labels,
datasets: [{ data: datapoints }],
},
});
}

updateChart(newProps) {
if (this.chart) {
const datapoints = Object.values(newProps.data);
Object.assign(this.chart.data.datasets[0], { data: datapoints });
this.chart.update();
}
}

destroyChart() {
if (this.chart) {
this.chart.destroy();
this.chart = null;
}
}
}
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.pie-chart">
<canvas t-ref="canvas"/>
</t>

</templates>
Loading