Skip to content
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

Technical Training - thsc #628

Open
wants to merge 4 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,7 @@ dmypy.json

# Pyre type checker
.pyre/

# VS Code
.vscode/
.vscode/settings.json
4 changes: 4 additions & 0 deletions awesome_dashboard/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
'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'
}
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.

37 changes: 37 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/** @odoo-module **/

import { Component, useState } from "@odoo/owl";
import { registry } from "@web/core/registry";
import { useService } from "@web/core/utils/hooks";
import { Layout } from "@web/search/layout";
import { _t } from "@web/core/l10n/translation";
import { DashboardItem } from "./dashboard_item";

class AwesomeDashboard extends Component {
static template = "awesome_dashboard.AwesomeDashboard";

static components = { Layout, DashboardItem };

setup() {
this.action = useService("action");

this.items = registry.category("awesome_dashboard").getAll();

this.statistics = useState(useService("awesome_dashboard.statistics"));
}

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

async openLeads(activity) {
this.action.doAction({
type: 'ir.actions.act_window',
name: _t('All leads'),
res_model: 'crm.lead',
views: [[false, 'list'], [false, 'form']],
});
}
}

registry.category("lazy_components").add("AwesomeDashboard", AwesomeDashboard);
8 changes: 8 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.o_dashboard {
background-color: grey;
}

h1 {
color: green;
text-align: center;
}
21 changes: 21 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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="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>
<div class="d-flex flex-wrap" t-if="statistics.isReady">
<t t-foreach="items" t-as="item" t-key="item.id">
<DashboardItem 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>
</Layout>
</t>

</templates>
22 changes: 22 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";
import { _t } from "@web/core/l10n/translation";

export class DashboardItem extends Component {
static template = "awesome_dashboard.DashboardItem";

static props = {
slots: {
type: Object,
shape: {
default: Object
},
},
size: {
type: Number,
default: 1,
optional: true,
},
};
}
12 changes: 12 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_item.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.DashboardItem">
<div class="card border-dark m-2" t-attf-style="width: {{18*props.size}}rem;">
<div class="card-body">
<t t-slot="default"/>
</div>
</div>
</t>

</templates>
67 changes: 67 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/** @odoo-module **/

import { NumberCard } from "./number_card/number_card";
import { PieChartCard } from "./pie_chart_card/pie_chart_card";
import { registry } from "@web/core/registry";

const items = [
{
id: "nb_new_orders",
description: "Number of new orders",
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",
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",
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",
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: "pie_chart",
description: "Shirt orders by size",
Component: PieChartCard,
size: 2,
props: (data) => ({
title: "Shirt orders by size",
value: data.orders_by_size,
})
},
]

items.forEach(item => {
registry.category("awesome_dashboard").add(item.id, item);
});
12 changes: 12 additions & 0 deletions awesome_dashboard/static/src/dashboard/number_card/number_card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @odoo-module **/

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

export class NumberCard extends Component {
static template = "awesome_dashboard.NumberCard";

static props = {
title: String,
value: Number,
}
}
11 changes: 11 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,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.NumberCard" owl="1">
<t t-esc="props.title"/>
<h1 className="'o_dashboard_number'">
<t t-esc="props.value"/>
</h1>
</t>

</templates>
47 changes: 47 additions & 0 deletions awesome_dashboard/static/src/dashboard/pie_chart_card/pie_chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** @odoo-module **/

import { Component, onMounted, onWillStart, onWillUnmount, useRef } from "@odoo/owl";
import { getColor } from "@web/core/colors/colors";
import { loadJS } from "@web/core/assets";

export class PieChart extends Component {
static template = "awesome_dashboard.PieChart";

static props = {
data: Object,
label: String,
}

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

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

onMounted(() => {
this.renderChart();
});
onWillUnmount(() => {
this.chart.destroy();
});
}

renderChart() {
const labels = Object.keys(this.props.data);
const data = Object.values(this.props.data);
const color = labels.map((_, index) => getColor(index));

this.chart = new Chart(this.canvasRef.el, {
type: "pie",
data: {
labels: labels,
datasets: [
{
label: this.props.label,
data: data,
backgroundColor: color,
},
],
},
});
}
}
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.PieChart">
<div t-att-class="'h-100 ' + props.class" t-ref="root">
<div class="h-100 position-relative" t-ref="container">
<canvas t-ref="canvas" />
</div>
</div>
</t>

</templates>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";
import { PieChart } from "./pie_chart";

export class PieChartCard extends Component {
static template = "awesome_dashboard.PieChartCard";

static components = { PieChart };

static props = {
title: String,
values: Object,
}
}
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.PieChartCard" owl="1">
<t t-esc="props.title"/>
<PieChart data="props.values" label="props.title"></PieChart>
</t>

</templates>
22 changes: 22 additions & 0 deletions awesome_dashboard/static/src/dashboard/statistics_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { registry } from "@web/core/registry";
import { rpc } from "@web/core/network/rpc";
import { reactive } from "@odoo/owl";

const statisticsService = {
async: ["loadStatistics"],

start() {
const statistics = reactive({ isReady: false });

async function loadData(params) {
Object.assign(statistics, await rpc("/awesome_dashboard/statistics"), { isReady: true});
}

setInterval(loadData, 10*1000);
loadData();

return statistics;
},
};

registry.category("services").add("awesome_dashboard.statistics", statisticsService);
12 changes: 12 additions & 0 deletions awesome_dashboard/static/src/dashboard_loader.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);
16 changes: 16 additions & 0 deletions awesome_owl/static/src/card/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Component, useState } from "@odoo/owl";

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

static props = {
title: String,
slots: Object
};

state = useState({ open: true });

flipState() {
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="flipState">Toggle</button></h5>
<t t-slot="default" t-if="state.open"/>
</div>
</div>
</t>


</templates>
Loading