Skip to content
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
5 changes: 4 additions & 1 deletion awesome_dashboard/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
'views/views.xml',
],
'assets': {
'awesome_dashboard.dashboard': [
'awesome_dashboard/static/src/dashboard/**/*',
],
'web.assets_backend': [
'awesome_dashboard/static/src/**/*',
'awesome_dashboard/static/src/dashboard_action.js',
],
},
'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,54 @@
import {Component, onWillStart, onWillUnmount, useEffect, useRef} from "@odoo/owl";
import { loadJS } from "@web/core/assets";

export class PieChart extends Component {
static template = "awesome_owl.pie_chart";
static props = {
dataset: {
type: Object
},
};

setup() {
this.chart = null;
this.canvasRef = useRef("canvas");
onWillStart(() => loadJS("/web/static/lib/Chart/Chart.js"));
useEffect(() => this.renderChart());
onWillUnmount(this.onWillUnmount);
}

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

onWillUnmount() {
this.destroyChart();
}

renderChart() {
this.destroyChart();
const config = this.getChartConfig();
this.chart = new Chart(this.canvasRef.el, config);
}

getChartConfig() {
const labels = Object.keys(this.props.dataset);

const data = Object.values(this.props.dataset);

return {
type: 'pie',
data: {
labels: labels,
datasets: [
{
data: data,
}
],
}
};
}

}
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_owl.pie_chart">
<canvas t-ref="canvas"/>
</t>

</templates>
60 changes: 60 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {Component, onWillStart, useState, onWillUnmount } from "@odoo/owl";
import {_t} from "@web/core/l10n/translation";
import {useService} from "@web/core/utils/hooks";
import {Layout} from "@web/search/layout";
import {registry} from "@web/core/registry";
import {DashboardItem} from "./dashboard_item/dashboard_item"
import {PieChart} from "./chart/pie_chart/pie_chart"

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

setup() {
this.display = {
controlPanel: {},
};
this.action = useService("action");

this.dashStatsService = useService("awesome_dashboard.statistics");

this.state = useState({
stats: {},
});

onWillStart(async () => {
this.state.stats = await this.dashStatsService.loadStatistics();
})

const intervalId = setInterval(async () => {
const newStats = await this.dashStatsService.loadStatistics();
// Update the stats state keeping the reference
Object.assign(this.state.stats, newStats);
}, 1000*60*10);

onWillUnmount(() => {
clearInterval(intervalId);
});
}

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

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

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

<t t-name="awesome_dashboard.AwesomeDashboard">
<div class="o_action">
<Layout display="display">
<t t-set-slot="control-panel-create-button">
<button t-on-click="openCustomers" type="button" class="btn btn-primary" title="Customers">Customers</button>
<button t-on-click="openLeads" type="button" class="btn btn-primary" title="Leads">Leads</button>
</t>

<div class="p-3">
<DashboardItem>
<t t-set-slot="title">Number of new orders this month</t>
<t t-set-slot="number"><t t-esc="state.stats.nb_new_orders"/></t>
</DashboardItem>
<DashboardItem>
<t t-set-slot="title">Total amount of new orders this month</t>
<t t-set-slot="number"><t t-esc="state.stats.total_amount"/></t>
</DashboardItem>
<DashboardItem size="2">
<t t-set-slot="title">Average amount of t-shirt by order this month</t>
<t t-set-slot="number"><t t-esc="state.stats.average_quantity"/></t>
</DashboardItem>
<DashboardItem>
<t t-set-slot="title">Number of cancelled orders this month</t>
<t t-set-slot="number"><t t-esc="state.stats.nb_cancelled_orders"/></t>
</DashboardItem>
<DashboardItem size="2">
<t t-set-slot="title">Average time for an order to go from ‘new’ to ‘sent’ or ‘cancelled’</t>
<t t-set-slot="number"><t t-esc="state.stats.average_time"/></t>
</DashboardItem>
<DashboardItem size="2">
<t t-set-slot="title">Shirt orders by size</t>
<t t-set-slot="default"><PieChart dataset="state.stats.orders_by_size"/></t>
</DashboardItem>
</div>
</Layout>
</div>
</t>

</templates>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Component } from "@odoo/owl";

export class DashboardItem extends Component {
static template = "awesome_owl.dashboard_item";
static props = {
title: String,
size: { type: Number, optional: true },
slots: {
type: Object,
optional: true,
shape: {
default: { optional: true },
title: String,
value: String
},
},
};

static defaultProps = {
size: 1
}

setup() {
}

get cardWidth() {
return 18 * this.props.size;
}

}
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_owl.dashboard_item">
<div class="card d-inline-block align-top m-2" t-att-style="'width: ' + cardWidth + 'rem;'">
<div class="card-body">
<div class="card-title">
<t t-slot="title"/>
</div>
<div class="card-text">
<p class="text-success text-center fs-1"><t t-slot="number"/></p>
<t t-slot="default"/>
</div>
</div>
</div>
</t>

</templates>
23 changes: 23 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_stats_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { registry } from "@web/core/registry";
import { rpc } from "@web/core/network/rpc";
import { memoize } from "@web/core/utils/functions";

export async function loadStatistics() {
return rpc("/awesome_dashboard/statistics");
}

export const dashboardStatsService = {
dependencies: [],
start() {
const memoizedLoad = memoize(loadStatistics);

// Time key integer changing every 10m in order to miss the cache of memoize
const getTimeKey = () => Math.floor(Date.now() / (1000 * 60 * 10));

return {
loadStatistics: () => memoizedLoad(getTimeKey())
};
},
};

registry.category("services").add("awesome_dashboard.statistics", 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 { registry } from "@web/core/registry";
import { LazyComponent } from "@web/core/assets";

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

registry.category("actions").add("awesome_dashboard.dashboard", AwesomeDashboardLoader);
31 changes: 31 additions & 0 deletions awesome_owl/static/src/card/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Component, useState } from "@odoo/owl";

export class Card extends Component {
static template = "awesome_owl.card";
static props = {
title: String,
body: { type: String, optional: true },
slots: {
type: Object,
optional: true,
shape: {
default: { optional: true },
},
},
toggleMode: {
type: Boolean,
}
};

setup() {
this.state = useState({
isToggleOn: false
});
}

toggle() {
if (this.props.toggleMode) {
this.state.isToggleOn = !this.state.isToggleOn;
}
}
}
24 changes: 24 additions & 0 deletions awesome_owl/static/src/card/card.xml
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_owl.card">
<div class="card d-inline-block m-2" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title"><t t-esc="props.title"/>
<t t-if="props.toggleMode">
<span t-on-click="toggle" style="cursor: pointer; margin-left: 10px" class="text-warning mb-2">
<t t-esc="state.isToggleOn ? 'Untoggle' : 'Toggle'"/>
</span>
</t></h5>
<div class="card-text">
<t t-if="props.body" t-esc="props.body"/>

<div t-att-class="{'d-none': props.toggleMode and !state.isToggleOn}">
<t t-slot="default"/>
</div>
</div>
</div>
</div>
</t>

</templates>
27 changes: 27 additions & 0 deletions awesome_owl/static/src/counter/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Component, useState } from "@odoo/owl";

export class Counter extends Component {
static template = "awesome_owl.counter";

static props = {
initialValue: { type: Number, optional: true},
onChange: { type: Function, optional: true },
onRegister: { type: Function, optional: true },
};

setup() {
this.state = useState({ value: this.props.initialValue ?? 0});

if (this.props.onRegister) {
this.props.onRegister(this)
}
}

increment() {
this.state.value++;

if (this.props.onChange) {
this.props.onChange();
}
}
}
11 changes: 11 additions & 0 deletions awesome_owl/static/src/counter/counter.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_owl.counter">
<div class="border d-flex m-2 p-2">
<p class="mb-o align-self-center" style="margin: 0;">Counter: <t t-esc="state.value"/></p>
<button t-on-click="increment" class="btn m-2 border-black">Increment</button>
</div>
</t>

</templates>
Loading