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
44 changes: 43 additions & 1 deletion awesome_dashboard/static/src/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
import { Component } from "@odoo/owl";
import { Component, onWillStart } from "@odoo/owl";
import { registry } from "@web/core/registry";
import { useService } from "@web/core/utils/hooks";
import { Layout } from "@web/search/layout";

import { DashboardItem } from "./dashboard_item/dashboard_item";
import { PieChart } from "./pie_chart/pie_chart";

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

setup() {
this.action = useService("action");
this.state = {
numOrders: 0,
newOrders: 0,
tShirtByOrder: 0,
cancelledOrders: 0,
timeFromNew: 0,
ordersBySize: { labels: [], values: [] },
}
this.statistics = useService("awesome_dashboard.statistics");
onWillStart(async () => {
const loadedStatistics = await this.statistics.loadStatistics();
this.state.numOrders = loadedStatistics.numOrders;
this.state.newOrders = loadedStatistics.newOrders;
this.state.tShirtByOrder = loadedStatistics.tShirtByOrder;
this.state.cancelledOrders = loadedStatistics.cancelledOrders;
this.state.timeFromNew = loadedStatistics.timeFromNew;
this.state.ordersBySize.labels = loadedStatistics.sizeLabels;
this.state.ordersBySize.values = loadedStatistics.sizeValues;
});
}

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

openLeads() {
this.action.doAction({
type: "ir.actions.act_window",
res_model: "crm.lead",
target: "current",
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: coral;
}
35 changes: 33 additions & 2 deletions awesome_dashboard/static/src/dashboard.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.AwesomeDashboard">
hello dashboard
<Layout className="'o_dashboard h-100'" display="{ controlPanel: {} }">
<t t-set-slot="layout-buttons">
<button class="btn btn-primary" t-on-click="openCustomers">Customers</button>
<button class="btn btn-primary" t-on-click="openLeads">Leads</button>
</t>
<DashboardItem size="5">
Number of new orders this month
<t t-esc="state.numOrders"/>
</DashboardItem>
<DashboardItem size="5">
Total amount of new orders this month
<t t-esc="state.newOrders"/>
</DashboardItem>
<DashboardItem size="5">
Average amount of t-shirt by order this month
<t t-esc="state.tShirtByOrder"/>
</DashboardItem>
<DashboardItem size="5">
Number of cancelled orders this month
<t t-esc="state.cancelledOrders"/>
</DashboardItem>
<DashboardItem size="5">
Average time for an order to go from new to sent or cancelled
<t t-esc="state.timeFromNew"/>
</DashboardItem>
<DashboardItem size="2">
<PieChart
labels="state.ordersBySize.labels"
values="state.ordersBySize.values"
title="'Shirts orders by sizes'"
/>
</DashboardItem>
</Layout>
</t>

</templates>
12 changes: 12 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,12 @@
import { Component } from "@odoo/owl"

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

static defaultProps = {
size: 1,
};
}
10 changes: 10 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,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;">
<div class="card-body">
<t t-slot="default"/>
</div>
</div>
</t>
</templates>
24 changes: 24 additions & 0 deletions awesome_dashboard/static/src/dashboard_statistics_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { rpc } from "@web/core/network/rpc";
import { registry } from "@web/core/registry";
import { memoize } from "@web/core/utils/functions";

const loadStatistics = memoize(async () => {
const response = await rpc("/awesome_dashboard/statistics");
return {
numOrders: response.nb_new_orders,
newOrders: response.total_amount,
ShirtByOrder: response.average_quantity,
cancelledOrders: response.nb_cancelled_orders,
timeFromNew: response.average_time,
sizeLabels: Object.keys(response.orders_by_size),
sizeValues: Object.values(response.orders_by_size),
}
})

const dashboardStatistics = {
start(env) {
return { loadStatistics }
}
}

registry.category("services").add("awesome_dashboard.statistics", dashboardStatistics);
41 changes: 41 additions & 0 deletions awesome_dashboard/static/src/pie_chart/pie_chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Component, onWillStart, onMounted, onWillUnmount, useRef } from "@odoo/owl"
import { loadJS } from "@web/core/assets";

export class PieChart extends Component {
static template = "awesome_dashboard.pie_chart";
static props = {
labels: { type: Array, element: String },
values: { type: Array, element: Number },
title: { type: String },
}

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

onWillStart(async () => {
await loadJS("/web/static/lib/Chart/Chart.js");
});
onMounted(() => {
this.renderChart();
});
onWillUnmount(() => {
if (this.chart) {
this.chart.destroy();
}
});

}

renderChart() {
this.chart = new Chart(this.chartRef.el, {
type: "pie",
data: {
datasets: [{
data: this.props.values
}],
labels: this.props.labels,
}
})
}
}
9 changes: 9 additions & 0 deletions awesome_dashboard/static/src/pie_chart/pie_chart.xml
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.pie_chart">
<div class="h-100 d-flex flex-column gap-2">
<t t-esc="props.title"/>
<canvas class="h-100" t-ref="canvas"/>
</div>
</t>
</templates>
24 changes: 24 additions & 0 deletions awesome_owl/static/src/card/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Component, useState } from "@odoo/owl";

export class Card extends Component {
static template = "awesome_owl.card";

static props = {
title: {type: String},
slots: {
type: Object,
shape: {
default: true
},
}
};

setup() {
this.state = useState({open: true});
this.toggleOpen = this.toggleOpen.bind(this);
}

toggleOpen() {
this.state.open = !this.state.open;
}
}
14 changes: 14 additions & 0 deletions awesome_owl/static/src/card/card.xml
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_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" />
<button t-on-click="() => toggleOpen()">Toggle</button>
</h5>
<t t-slot="default" t-if="state.open"/>
</div>
</div>
</t>
</templates>
20 changes: 20 additions & 0 deletions awesome_owl/static/src/counter/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Component, xml, useState } from "@odoo/owl";

export class Counter extends Component {
static template = "awesome_owl.counter";
static props = {
onChange: {type: Function, optional: true}
};

setup() {
this.state = useState({value: 1});
}

increment() {
this.state.value++;
if (this.props.onChange) {
this.props.onChange();
}
}
}

9 changes: 9 additions & 0 deletions awesome_owl/static/src/counter/counter.xml
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_owl.counter">
<div>
<p>Counter: <t t-esc="state.value"/></p>
<button t-on-click="increment">Increment</button>
</div>
</t>
</templates>
15 changes: 14 additions & 1 deletion awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { Component } from "@odoo/owl";
import { Component, markup, useState } from "@odoo/owl";
import { Card } from "./card/card"
import { Counter } from "./counter/counter"
import { TodoList } from "./todo/todo_list";

export class Playground extends Component {
static template = "awesome_owl.playground";
static components = { Card, Counter, TodoList };

setup() {
this.state = useState({value: 2});
this.onChange = this.onChange.bind(this);
}

onChange() {
this.state.value++;
}
}
14 changes: 11 additions & 3 deletions awesome_owl/static/src/playground.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_owl.playground">
<div class="p-3">
hello world
<p>hello world</p>
<p>The sum is <t t-esc="state.value" /></p>
</div>
<div>
<Card title="'Card 1'">
<Counter onChange="onChange"/>
</Card>
<Card title="'Card 2'">
<Counter onChange="onChange"/>
</Card>
</div>
<div><TodoList /></div>
</t>

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

export class TodoItem extends Component {
static template = "awesome_owl.todo_item";
static props = {
item: {type: Object, shape: {
id: {type: Number},
description: {type: String},
isCompleted: {type: Boolean, optional: true}
}},
toggleState: {type: Function},
removeTodo: {type: Function},
};

setup() {
this.markCompleted = this.markCompleted.bind(this);
this.deleteTask = this.deleteTask.bind(this);
}

markCompleted() {
this.props.toggleState(this.props.item.id);
}

deleteTask() {
this.props.removeTodo(this.props.item.id);
}
}

15 changes: 15 additions & 0 deletions awesome_owl/static/src/todo/todo_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="awesome_owl.todo_item">
<div class="d-flex flex-row align-items-center gap-2">
<input type="checkbox" class="m-0 p-0" t-on-change="() => markCompleted()"/>
<p
class="m-0"
t-att-class="{'text-muted': props.item.isCompleted, 'text-decoration-line-through': props.item.isCompleted}"
>
<t t-esc="props.item.id"/>. <t t-esc="props.item.description"/>
</p>
<span class="fa fa-remove" t-on-click="() => deleteTask()"/>
</div>
</t>
</templates>
Loading