Skip to content

Awesome Dashboard #808

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 2 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
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,11 @@
/** @odoo-module **/

import { Component, useState} 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,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="awesome_dashboard.number_card">
<h5>
<t t-esc="props.title"/>
</h5>
<h3 style="color: purple">
<t t-esc="props.value"/>
</h3>
</t>
</templates>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/** @odoo-module **/

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


export class Piechart extends Component {
static template = "awesome_dashboard.piechart";
static props = {
data: Array,
labels: Array,
slots: {type: Object, optional: true},
size: Number,
};

/**
* Instantiates a Chart (Chart.js lib) to render the graph according to
* the current config.
*/
renderChart() {
if (this.chart) {
this.chart.destroy();
}
this.chart = new Chart(this.canvasRef.el, {
type: 'pie',
data: {
labels: this.props.labels,
datasets: [{
data: this.props.data,
borderWidth: 1
}]
},
options: {
responsive: true,
}
});
}

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

onWillStart(() => loadJS(["/web/static/lib/Chart/Chart.js"]));

useEffect(() => this.renderChart());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="awesome_dashboard.piechart">
<canvas t-ref="canvas" width="400" height="200"></canvas>
</t>
</templates>
98 changes: 98 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/** @odoo-module **/

import { Component, useState, onWillStart, onWillRender} from "@odoo/owl";
import { registry } from "@web/core/registry";
import { Layout } from "@web/search/layout";
import { useService } from "@web/core/utils/hooks";
import { _t } from "@web/core/l10n/translation";
import { DashboardItem } from "./dashboard_item/dashboard_item";
import { Piechart } from "./components/piechart/piechart";
import { Dialog } from "@web/core/dialog/dialog";
import { CheckBox } from "@web/core/checkbox/checkbox";
import { browser } from "@web/core/browser/browser";

class AwesomeDashboard extends Component {
static template = "awesome_dashboard.AwesomeDashboard";
static components = { Layout, DashboardItem, Piechart}
layout_display = { controlPanel: {} }

setup() {
this.action = useService("action");
this.dialog = useService("dialog");
this.statsService = useService("awesome_dashboard.dashboard_stats");
this.data_proxy = useState(this.statsService.getDataProxy());

onWillStart(async () => {
this.data_proxy.data = await this.statsService.fetchNow();
})

this.items = registry.category("awesome_dashboard").get("dashboard_items");
this.config = useState({
disabledItems: browser.localStorage.getItem("AwesomeDashboard.config.disabledItems")?.split(",") || []
});
}

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

updateConfiguration(newDisabledItems) {
this.config.disabledItems = newDisabledItems;
}

isEnabled(itemId) {
return !this.config.disabledItems.includes(itemId);
}

goToCustomers() {
this.action.doAction("base.action_partner_form");
}

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

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

class ConfigurationDialog extends Component {
static template = "awesome_dashboard.ConfigurationDialog";
static components = { Dialog, CheckBox };
static props = ["close", "items", "disabledItems", "onUpdateConfiguration"];

setup() {
// A map where the key is the id of the dashboard item, and the value is whether the item is enabled or not.
this.item_enabled_map = useState(Object.fromEntries(
this.props.items.map(item => [item.id, !this.props.disabledItems.includes(item.id)])
));
}

done() {
this.props.close();
}

onChange(checked, changedItem) {
this.item_enabled_map[changedItem.id] = checked;

const newDisabledItems = Object.values(this.props.items).filter(
(item) => !this.item_enabled_map[item.id]
).map((item) => item.id);

browser.localStorage.setItem(
"AwesomeDashboard.config.disabledItems",
newDisabledItems,
);

this.props.onUpdateConfiguration(newDisabledItems);
}
}
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: rgb(162, 162, 199);
}
35 changes: 35 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="awesome_dashboard.AwesomeDashboard">
<Layout display="layout_display" className="'o_dashboard h-100'">
<button class="btn btn-primary" t-on-click="goToCustomers">Customers</button>
<button class="btn btn-primary" t-on-click="goToLeads">Leads</button>
<button t-on-click="openConfiguration" class="btn">
<i class="fa fa-cog"></i>
</button>
<div>
<t t-foreach="items" t-as="item" t-key="item.id">
<DashboardItem t-if="isEnabled(item.id)" size="item.size || 1">
<t t-set="itemProp" t-value="item.props ? item.props(data_proxy.data) : {'data': data_proxy.data}"/>
<t t-component="item.Component" t-props="itemProp" />
</DashboardItem>
</t>
</div>
</Layout>
</t>
<t t-name="awesome_dashboard.ConfigurationDialog">
<Dialog title="'Dashboard items configuration'">
Which cards do you wish to see ?
<t t-foreach="props.items" t-as="item" t-key="item.id">
<CheckBox value="this.item_enabled_map[item.id]" 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,11 @@
/** @odoo-module **/

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

export class DashboardItem extends Component {
static template = "awesome_dashboard.dashboard_item";
static props = {
slots: {type: Object, optional: true},
size: Number,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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">
<t t-slot="default">
<p class="card-text">
Default Content
</p>
</t>
</div>
</div>
</t>
</templates>
73 changes: 73 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Piechart } from "./components/piechart/piechart";
import { NumberCard } from "./components/number_card/number_card";
import { registry } from "@web/core/registry";

const item1 = {
id: "average_quantity",
description: "Average amount of t-shirt",
Component: NumberCard,

size: 1,
props: (data) => ({
title: "Average amount of t-shirt by order this month",
value: data.average_quantity
}),
};
const item2 = {
id: "average_time",
description: 'AVG time for an order to go from "new" to "sent" or "cancelled"',
Component: NumberCard,

size: 1,
props: (data) => ({
title: 'AVG time for an order to go from "new" to "sent" or "cancelled"',
value: data.average_time
}),
};
const item3 = {
id: "nb_new_orders",
description: "New orders",
Component: NumberCard,

size: 1,
props: (data) => ({
title: "New orders this month",
value: data.nb_new_orders
}),
};
const item4 = {
id: "nb_cancelled_orders",
description: "Cancelled orders",
Component: NumberCard,

size: 1,
props: (data) => ({
title: "Cancelled orders this month",
value: data.nb_cancelled_orders
}),
};
const item5 = {
id: "total_amount",
description: "Total orders",
Component: NumberCard,

size: 1,
props: (data) => ({
title: "Total orders",
value: data.total_amount
}),
};

const item6 = {
id: "orders_per_size",
description: "Shirt Orders per size",
Component: Piechart,

size: 1,
props: (data) => ({
labels: Object.keys(data.orders_by_size),
data: Object.values(data.orders_by_size)
}),
}

registry.category("awesome_dashboard").add("dashboard_items", [item1, item2, item3, item4, item5, item6]);
41 changes: 41 additions & 0 deletions awesome_dashboard/static/src/dashboard/services/dashboard_stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { registry } from "@web/core/registry";
import { memoize } from "@web/core/utils/functions";
import { rpc } from "@web/core/network/rpc";
import { reactive } from "@odoo/owl";

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function fetch() {
sleep(1000);
return await rpc("/awesome_dashboard/statistics");
}

const dashboardStatsService = {
async: ["fetchNow"],


start(env, {}) {
const data_proxy = reactive({data: {}});

// Better to not use memoize since the cache needs to be invalidated.
// memoized_fetch = memoize(fetch);

setInterval(async ()=>{
data_proxy.data = await fetch();
}, 10000);

const getDataProxy = ()=>{
return data_proxy;
}

const fetchNow = ()=>{
return fetch();
}

return { getDataProxy, fetchNow };
},
};

registry.category("services").add("awesome_dashboard.dashboard_stats", dashboardStatsService);
12 changes: 12 additions & 0 deletions awesome_dashboard/static/src/dashboard_action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Component, xml} from "@odoo/owl";
import { LazyComponent } from '@web/core/assets'
import { registry } from "@web/core/registry";

export class AwesomeDashboardLoader extends Component {
static components = { LazyComponent};
static template = xml`
<LazyComponent bundle="'awesome_dashboard.dashboard'" Component="'AwesomeDashboard'" props="props" />
`;
}

registry.category("actions").add("awesome_dashboard.dashboard", AwesomeDashboardLoader);
Loading