-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] awesome_dashboard: Tutorial chapter 2: items 1-10
- Loading branch information
Showing
15 changed files
with
280 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** @odoo-module **/ | ||
|
||
import { Component, useState, onWillStart } from "@odoo/owl"; | ||
import { registry } from "@web/core/registry"; | ||
import { Layout } from "@web/search/layout"; | ||
import { useService } from "@web/core/utils/hooks"; | ||
import { AwesomeDashboardItem } from "./dashboard_item" | ||
|
||
class AwesomeDashboard extends Component { | ||
static template = "awesome_dashboard.AwesomeDashboard"; | ||
static components = { Layout, AwesomeDashboardItem} | ||
setup(){ | ||
this.action = useService("action"); | ||
/** Previous implementation: leaving here for tutorial review purposes **/ | ||
/** this.statistics = useService("awesome_dashboard.statistics"); | ||
this.data = useState({ }); | ||
onWillStart(async () => { | ||
this.data = await this.statistics(); | ||
}); **/ | ||
this.data = useState(useService("awesome_dashboard.statistics")); | ||
this.dashboardItems = registry.category("awesome_dashboard").getAll(); | ||
} | ||
openCustomerView(){ | ||
this.action.doAction("base.action_partner_form"); | ||
} | ||
openLeadsViews() { | ||
this.action.doAction({ | ||
type: "ir.actions.act_window", | ||
res_model: "crm.lead", | ||
views: [[false, "list"], [false, "form"]], | ||
target: 'current', | ||
}); | ||
} | ||
} | ||
|
||
registry.category("lazy_components").add("AwesomeDashboard", AwesomeDashboard); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates xml:space="preserve"> | ||
|
||
<t t-name="awesome_dashboard.AwesomeDashboard"> | ||
<Layout className="'o_dashboard h-100'" display="{controlPanel: {} }"> | ||
<t t-set-slot="layout-buttons"> | ||
<button class="btn btn-primary" t-on-click="openCustomerView">Customers</button> | ||
<button class="btn btn-primary" t-on-click="openLeadsViews">Leads</button> | ||
</t> | ||
<!-- Previous implementation: leaving for tutorial review purposes --> | ||
<!-- t t-foreach="data" t-as="item" t-key="item"> | ||
<div t-if="item != 'orders_by_size'"> | ||
<AwesomeDashboardItem size="2"> | ||
<t t-set-slot="title"> | ||
<span t-esc="item"/> | ||
</t> | ||
<t t-esc="item_value"/> | ||
</AwesomeDashboardItem> | ||
</div> | ||
</t--> | ||
<t t-foreach="dashboardItems" t-as="item" t-key="item.id"> | ||
<AwesomeDashboardItem size="item.size || 1"> | ||
<t t-set="itemProp" t-value="item.props ? item.props(data) : {'data': data}"/> | ||
<t t-component="item.Component" t-props="itemProp"/> | ||
</AwesomeDashboardItem> | ||
</t> | ||
</Layout> | ||
</t> | ||
|
||
</templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** @odoo-module **/ | ||
|
||
import { Component } from "@odoo/owl"; | ||
|
||
export class AwesomeDashboardItem extends Component { | ||
static template = "awesome_dashboard.dashboard_item"; | ||
static props = { | ||
size: { | ||
type: Number, | ||
optional: true, | ||
}, | ||
slots: { | ||
type: Object, | ||
optional: true, | ||
}, | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
awesome_dashboard/static/src/components/dashboard_item.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_dashboard.dashboard_item"> | ||
<div class="card" t-attf-style="width: {{18*props.size}}rem;"> | ||
<div class="card-body"> | ||
<h5 class="card-title"><t t-slot="title"/></h5> | ||
<p class="card-text"> | ||
<t t-slot="default"/> | ||
</p> | ||
</div> | ||
</div> | ||
</t> | ||
|
||
</templates> |
66 changes: 66 additions & 0 deletions
66
awesome_dashboard/static/src/components/dashboard_item_list.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** @odoo-module **/ | ||
|
||
import { registry } from "@web/core/registry"; | ||
import { AwesomeDashboardNumberCard } from "./dashboard_number_card" | ||
import { AwesomeDashboardPieChart } from "./dashboard_pie_chart" | ||
|
||
const dashboardItems = [ | ||
{ | ||
id: "average_quantity", | ||
description: "Average quantity of t-shirts", | ||
Component: AwesomeDashboardNumberCard, | ||
props: (data) => ({ | ||
title: "Average quantity of t-shirts by order this month", | ||
value: data.average_quantity, | ||
}) | ||
}, | ||
{ | ||
id: "average_time", | ||
description: "Average time for an order", | ||
Component: AwesomeDashboardNumberCard, | ||
props: (data) => ({ | ||
title: "Average time for an order to be processed", | ||
value: data.average_time, | ||
}) | ||
}, | ||
{ | ||
id: "nb_new_orders", | ||
description: "Number of new orders", | ||
Component: AwesomeDashboardNumberCard, | ||
props: (data) => ({ | ||
title: "Number of new orders, this month", | ||
value: data.nb_new_orders, | ||
}) | ||
}, | ||
{ | ||
id: "nb_cancelled_orders", | ||
description: "Number of cancelled orders", | ||
Component: AwesomeDashboardNumberCard, | ||
props: (data) => ({ | ||
title: "Number of cancelled orders, this month", | ||
value: data.nb_cancelled_orders, | ||
}) | ||
}, | ||
{ | ||
id: "total_amount", | ||
description: "Total amount of orders", | ||
Component: AwesomeDashboardNumberCard, | ||
props: (data) => ({ | ||
title: "Total amount of orders, this month", | ||
value: data.total_amount, | ||
}) | ||
}, | ||
{ | ||
id: "orders_by_size", | ||
description: "Shirt orders", | ||
Component: AwesomeDashboardPieChart, | ||
props: (data) => ({ | ||
title: "Shirt orders, by size", | ||
data: data.orders_by_size, | ||
}) | ||
} | ||
] | ||
|
||
dashboardItems.forEach(item => { | ||
registry.category("awesome_dashboard").add(item.id, item); | ||
}); |
11 changes: 11 additions & 0 deletions
11
awesome_dashboard/static/src/components/dashboard_number_card.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** @odoo-module **/ | ||
|
||
import { Component } from "@odoo/owl"; | ||
|
||
export class AwesomeDashboardNumberCard extends Component { | ||
static template = "awesome_dashboard.dashboard_number_card"; | ||
static props = { | ||
title: { type: String, }, | ||
value: { type: Number, } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
awesome_dashboard/static/src/components/dashboard_number_card.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates xml:space="preserve"> | ||
|
||
<t t-name="awesome_dashboard.dashboard_number_card"> | ||
<div> | ||
<h5><t t-esc="props.title"/></h5> | ||
<div> | ||
<t t-esc="props.value"/> | ||
</div> | ||
</div> | ||
</t> | ||
|
||
</templates> |
41 changes: 41 additions & 0 deletions
41
awesome_dashboard/static/src/components/dashboard_pie_chart.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** @odoo-module **/ | ||
|
||
import { Component, onWillStart, onWillUnmount, useEffect, useRef } from "@odoo/owl"; | ||
import { loadJS } from '@web/core/assets'; | ||
|
||
export class AwesomeDashboardPieChart extends Component { | ||
static template = "awesome_dashboard.dashboard_pie_chart"; | ||
static props = { | ||
title: { type: String, }, | ||
data: { type: Object, } | ||
} | ||
setup(){ | ||
this.chart = null; | ||
this.canvasRef = useRef("canvas"); | ||
onWillStart(async() => { | ||
await loadJS("/web/static/lib/Chart/Chart.js"); | ||
}); | ||
useEffect(() => this.renderChart(this.props.data)); | ||
onWillUnmount(this.onWillUnmount); | ||
} | ||
renderChart(data){ | ||
if (this.chart) { | ||
this.chart.destroy(); | ||
} | ||
this.chart = new Chart(this.canvasRef.el, { | ||
type: 'pie', | ||
data: { | ||
labels: Object.keys(data), | ||
datasets: [{ | ||
label: 'Shirts sold by size', | ||
data: Object.values(data), | ||
}] | ||
} | ||
}); | ||
} | ||
onWillUnmount(){ | ||
if (this.chart) { | ||
this.chart.destroy(); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
awesome_dashboard/static/src/components/dashboard_pie_chart.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<templates xml:space="preserve"> | ||
|
||
<t t-name="awesome_dashboard.dashboard_pie_chart"> | ||
<div> | ||
<h5><t t-esc="props.title"/></h5> | ||
<div> | ||
<canvas t-ref="canvas" /> | ||
</div> | ||
</div> | ||
</t> | ||
|
||
</templates> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** @odoo-module **/ | ||
|
||
import { Component, xml } from "@odoo/owl"; | ||
import { registry } from "@web/core/registry"; | ||
import { LazyComponent } from "@web/core/assets"; | ||
|
||
class AwesomeDashboardAction extends Component { | ||
static components = { LazyComponent }; | ||
static template = xml`<LazyComponent bundle="'awesome_dashboard.components'" Component="'AwesomeDashboard'" props="props"/>`; | ||
} | ||
|
||
registry.category("actions").add("awesome_dashboard.dashboard", AwesomeDashboardAction); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.o_dashboard{ | ||
background-color: #AAAAAA; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** @odoo-module **/ | ||
|
||
import { registry } from "@web/core/registry"; | ||
import { rpc } from "@web/core/network/rpc"; | ||
import { reactive } from "@odoo/owl"; | ||
|
||
const serviceStatistics = { | ||
start(){ | ||
const statistics = reactive({ }); | ||
async function statisticsLoad() { | ||
const result = await rpc("/awesome_dashboard/statistics"); | ||
Object.assign(statistics, result); | ||
} | ||
setInterval(statisticsLoad, 10000); /** Timout set to 10 seconds for testing **/ | ||
statisticsLoad(); | ||
return statistics; | ||
} | ||
} | ||
|
||
registry.category("services").add("awesome_dashboard.statistics", serviceStatistics); |