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
54 changes: 54 additions & 0 deletions awesome_dashboard/static/src/chart/pie_chart/pie_chart.js
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,
}
],
}
};
}

}
8 changes: 8 additions & 0 deletions awesome_dashboard/static/src/chart/pie_chart/pie_chart.xml
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_owl.pie_chart">
<canvas t-ref="canvas"/>
</t>

</templates>
45 changes: 42 additions & 3 deletions awesome_dashboard/static/src/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
import { Component } from "@odoo/owl";
import { registry } from "@web/core/registry";
import {Component, onWillStart, useState} 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 template = "awesome_dashboard.AwesomeDashboard";
static components = { Layout, DashboardItem, PieChart };

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

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

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

}

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("actions").add("awesome_dashboard.dashboard", AwesomeDashboard);
3 changes: 3 additions & 0 deletions awesome_dashboard/static/src/dashboard.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.o_dashboard {
background-color: #5f5e5e;
}
36 changes: 35 additions & 1 deletion awesome_dashboard/static/src/dashboard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,41 @@
<templates xml:space="preserve">

<t t-name="awesome_dashboard.AwesomeDashboard">
hello dashboard
<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="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="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="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="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="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="stats.orders_by_size"/></t>
</DashboardItem>
</div>
</Layout>
</div>
</t>

</templates>
30 changes: 30 additions & 0 deletions awesome_dashboard/static/src/dashboard_item/dashboard_item.js
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;
}

}
18 changes: 18 additions & 0 deletions awesome_dashboard/static/src/dashboard_item/dashboard_item.xml
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>
18 changes: 18 additions & 0 deletions awesome_dashboard/static/src/dashboard_stats_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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() {
return {
loadStatistics: memoize(loadStatistics)
};
},
};

registry.category("services").add("awesome_dashboard.statistics", dashboardStatsService);
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>
30 changes: 29 additions & 1 deletion awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import { Component } from "@odoo/owl";
import { Component, useState } from "@odoo/owl";
import { Counter } from "./counter/counter";
import { Card } from "./card/card";
import { TodoList } from "./todo_list/TodoList";

export class Playground extends Component {
static template = "awesome_owl.playground";

static components = { Counter, Card, TodoList};

setup() {
/*
I define the counters here so I can show them as much as I want
*/
this.state = useState({
counters_num: 2,
counters_ref: []
});
}

// It registry the reference of the child here
registerCounter(id, counterInstance) {
this.state.counters_ref[id] = counterInstance;
}

get sum() {
return this.state.counters_ref.reduce(
(acc, counter) => acc + (counter?.state?.value || 0),
0
)
}

}
26 changes: 24 additions & 2 deletions awesome_owl/static/src/playground.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,30 @@
<templates xml:space="preserve">

<t t-name="awesome_owl.playground">
<div class="p-3">
hello world
<div class="border d-inline-flex flex-column m-4">
<div class="d-flex flex-row">
<t t-foreach="Array.from({ length: state.counters_num }, (_, i) => i)" t-as="i" t-key="i">
<Counter initialValue="1" onRegister="(instance) => this.registerCounter(i, instance)" />
</t>
</div>

<div class="mt-2 p-2">The sum is: <span t-esc="sum"></span></div>
</div>

<div class="border m-4">
<div class="border m-2 d-inline-block">
<TodoList/>
</div>
</div>

<div class="border m-4">
<Card title="'Card 1'" body="'Some content'" toggleMode="false"/>
<Card title="'Card 2'" toggleMode="false">
<Counter/>
</Card>
<Card title="'Card 3'" toggleMode="true">
<Counter/>
</Card>
</div>
</t>

Expand Down
Loading