-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[ADD] awesome_owl, awesome_dashboard: basic client-side modules for todos and t-shirt sales dashboard #1307
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
base: 19.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,28 @@ | ||
| # -*- coding: utf-8 -*- | ||
| { | ||
| 'name': "Awesome Dashboard", | ||
|
|
||
| 'summary': """ | ||
| "name": "Awesome Dashboard", | ||
| "summary": """ | ||
| Starting module for "Discover the JS framework, chapter 2: Build a dashboard" | ||
| """, | ||
|
|
||
| 'description': """ | ||
| "description": """ | ||
| Starting module for "Discover the JS framework, chapter 2: Build a dashboard" | ||
| """, | ||
|
|
||
| 'author': "Odoo", | ||
| 'website': "https://www.odoo.com/", | ||
| 'category': 'Tutorials', | ||
| 'version': '0.1', | ||
| 'application': True, | ||
| 'installable': True, | ||
| 'depends': ['base', 'web', 'mail', 'crm'], | ||
|
|
||
| 'data': [ | ||
| 'views/views.xml', | ||
| "author": "Odoo", | ||
| "website": "https://www.odoo.com/", | ||
| "category": "Tutorials", | ||
| "version": "0.1", | ||
| "application": True, | ||
| "installable": True, | ||
| "depends": ["base", "web", "mail", "crm"], | ||
| "data": [ | ||
| "views/views.xml", | ||
| ], | ||
| 'assets': { | ||
| 'web.assets_backend': [ | ||
| 'awesome_dashboard/static/src/**/*', | ||
| "assets": { | ||
| "web.assets_backend": [ | ||
| "awesome_dashboard/static/src/dashboard_action.js", | ||
| ], | ||
| "awesome_dashboard.dashboard": [ | ||
| "awesome_dashboard/static/src/dashboard/**/*", | ||
| ], | ||
| }, | ||
| 'license': 'AGPL-3' | ||
| "license": "AGPL-3", | ||
| } |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { Component, useState } from "@odoo/owl"; | ||
| import { _t } from "@web/core/l10n/translation" | ||
| import { Layout } from "@web/search/layout" | ||
| import { registry } from "@web/core/registry"; | ||
| import { useService } from "@web/core/utils/hooks"; | ||
| import { DashboardItem } from "./dashboard_item"; | ||
| import { PieChart } from "./pie_chart"; | ||
| import { DashboardConfigDialog } from "./dashboard_config_dialog"; | ||
|
|
||
| class AwesomeDashboard extends Component { | ||
| static template = "awesome_dashboard.AwesomeDashboard"; | ||
|
|
||
| static components = { Layout, DashboardItem, PieChart, DashboardConfigDialog }; | ||
|
|
||
| setup() { | ||
| this.action = useService("action"); | ||
| this.statisticsService = useService("awesome_dashboard.statistics") | ||
| this.dialog = useService("dialog"); | ||
| this.statistics = useState(this.statisticsService); | ||
| this.configState = useState({ | ||
| disabledItems: JSON.parse(localStorage.getItem("disabled_dashboard_items") || "[]") | ||
| }); | ||
| } | ||
|
|
||
| get items() { | ||
| const allItems = registry.category("awesome_dashboard").getAll(); | ||
| return allItems.filter(item => !this.configState.disabledItems.includes(item.id)); | ||
| } | ||
|
|
||
| openConfiguration() { | ||
| this.dialog.add(DashboardConfigDialog, { | ||
| onConfigSaved: () => { | ||
| this.configState.disabledItems = JSON.parse(localStorage.getItem("disabled_dashboard_items") || "[]"); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| openCustomers() { | ||
| this.action.doAction("base.action_partner_form"); | ||
| } | ||
|
|
||
| openLeads() { | ||
| this.action.doAction({ | ||
| type: "ir.actions.act_window", | ||
| name: _t("Leads"), | ||
| res_model: "crm.lead", | ||
| views: [[false, "list"], [false, "form"]], | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| registry.category("lazy_components").add("AwesomeDashboard", AwesomeDashboard); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| .o_dashboard { | ||
| background-color: #f0f2f5; | ||
| display: flex; | ||
| flex-direction: column; | ||
| } | ||
| .o_dashboard .display-1 { | ||
| color: var(--primary); | ||
| font-weight: 500; | ||
| } |
| 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_dashboard.AwesomeDashboard"> | ||
| <Layout display="{controlPanel: {} }" className="'o_dashboard h-100'"> | ||
| <t t-set-slot="control-panel-always-buttons"> | ||
| <button class="btn btn-primary" t-on-click="openCustomers">Customers</button> | ||
| <button class="btn btn-primary" t-on-click="openLeads">Leads</button> | ||
| </t> | ||
| <t t-set-slot="control-panel-additional-actions"> | ||
| <button class="d-print-none btn lh-sm p-0 border-0" t-on-click="openConfiguration"> | ||
| <i class="fa fa-fw fa-cog"/> | ||
| </button> | ||
| </t> | ||
| <div class="d-flex flex-wrap"> | ||
| <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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /** @odoo-module **/ | ||
| import { Component, useState } from "@odoo/owl"; | ||
| import { Dialog } from "@web/core/dialog/dialog"; | ||
| import { registry } from "@web/core/registry"; | ||
|
|
||
| export class DashboardConfigDialog extends Component { | ||
| static components = { Dialog }; | ||
| static template = "awesome_dashboard.DashboardConfigDialog"; | ||
|
|
||
| setup() { | ||
| this.allItems = registry.category("awesome_dashboard").getAll(); | ||
|
|
||
| const disabledItems = JSON.parse(localStorage.getItem("disabled_dashboard_items") || "[]"); | ||
|
|
||
| const initialStatus = {}; | ||
| for (const item of this.allItems) { | ||
| initialStatus[item.id] = !disabledItems.includes(item.id); | ||
| } | ||
|
|
||
| this.state = useState(initialStatus); | ||
| } | ||
|
|
||
| onApply() { | ||
| const disabledIds = Object.keys(this.state).filter(id => !this.state[id]); | ||
|
|
||
| localStorage.setItem("disabled_dashboard_items", JSON.stringify(disabledIds)); | ||
|
|
||
| this.props.onConfigSaved(); | ||
|
|
||
| this.props.close(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <templates xml:space="preserve"> | ||
| <t t-name="awesome_dashboard.DashboardConfigDialog"> | ||
| <Dialog title="'Dashboard items configuration'"> | ||
| <div class="p-3"> | ||
| <p class="text-muted">Which cards do you wish to see?</p> | ||
| <div t-foreach="allItems" t-as="item" t-key="item.id" class="form-check my-2"> | ||
| <input | ||
| class="form-check-input" | ||
| type="checkbox" | ||
| t-model="state[item.id]" | ||
| t-att-id="'check_' + item.id" | ||
| /> | ||
| <label class="form-check-label" t-att-for="'check_' + item.id" t-out="item.description"/> | ||
| </div> | ||
| </div> | ||
| <t t-set-slot="footer"> | ||
| <button class="btn btn-primary" t-on-click="onApply">Apply</button> | ||
| <button class="btn btn-secondary" t-on-click="props.close">Cancel</button> | ||
| </t> | ||
| </Dialog> | ||
| </t> | ||
| </templates> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { Component } from "@odoo/owl"; | ||
|
|
||
| export class DashboardItem extends Component { | ||
| static template = "awesome_dashboard.DashboardItem"; | ||
|
|
||
| static props = { | ||
| size: { type: Number, optional: true }, | ||
| slots: { type: Object, optional: true }, | ||
| } | ||
| static defaultProps = { | ||
| size: 1, | ||
| } | ||
| } |
| 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="p-2 col-12" t-attf-class="col-lg-#{ (props.size || 1) * 3 }"> | ||
| <div class="o_dashboard_item card shadow-sm h-100"> | ||
| <div class="card-body"> | ||
| <t t-slot="default"/> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </t> | ||
| </templates> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { registry } from "@web/core/registry"; | ||
| import { _t } from "@web/core/l10n/translation"; | ||
| import { NumberCard } from "./number_card"; | ||
| import { PieChartCard } from "./pie_chart_card"; | ||
|
|
||
| const dashboardRegistry = registry.category("awesome_dashboard"); | ||
|
|
||
| dashboardRegistry.add("nb_new_orders", { | ||
| id: "nb_new_orders", | ||
| description: _t("Number of new orders this month"), | ||
| Component: NumberCard, | ||
| props: (data) => ({ | ||
| title: _t("Number of new orders this month"), | ||
| value: data.nb_new_orders, | ||
| }), | ||
| }); | ||
|
|
||
| dashboardRegistry.add("total_amount", { | ||
| id: "total_amount", | ||
| description: _t("Total amount of new orders this month"), | ||
| Component: NumberCard, | ||
| props: (data) => ({ | ||
| title: _t("Total amount of new orders this month"), | ||
| value: data.total_amount, | ||
| }), | ||
| }); | ||
|
|
||
| dashboardRegistry.add("average_quantity", { | ||
| id: "average_quantity", | ||
| description: _t("Average amount of t-shirt by order this month"), | ||
| Component: NumberCard, | ||
| props: (data) => ({ | ||
| title: _t("Average amount of t-shirt by order this month"), | ||
| value: data.average_quantity, | ||
| }), | ||
| }); | ||
|
|
||
| dashboardRegistry.add("nb_cancelled_orders", { | ||
| id: "nb_cancelled_orders", | ||
| description: _t("Number of cancelled orders this month"), | ||
| Component: NumberCard, | ||
| props: (data) => ({ | ||
| title: _t("Number of cancelled orders this month"), | ||
| value: data.nb_cancelled_orders, | ||
| }), | ||
| }); | ||
|
|
||
| dashboardRegistry.add("average_time", { | ||
| id: "average_time", | ||
| description: _t("Average time for an order"), | ||
| Component: NumberCard, | ||
| props: (data) => ({ | ||
| title: _t("Average time for an order to go from 'new' to 'sent' or 'cancelled'"), | ||
| value: data.average_time, | ||
| }), | ||
| }); | ||
|
|
||
| dashboardRegistry.add("orders_by_size", { | ||
| id: "orders_by_size", | ||
| description: _t("Shirt orders by size"), | ||
| Component: PieChartCard, | ||
| size: 2, | ||
| props: (data) => ({ | ||
| title: _t("Shirt orders by size"), | ||
| data: data.orders_by_size, | ||
| }), | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { Component, xml } from "@odoo/owl"; | ||
|
|
||
| export class NumberCard extends Component { | ||
| static props = { | ||
| title: String, | ||
| value: { optional: true }, | ||
| }; | ||
|
|
||
| static template = xml` | ||
| <t t-name="awesome_dashboard.NumberCard"> | ||
| <div class="d-flex flex-column align-items-center"> | ||
| <h3 t-out="props.title" class="text-muted"/> | ||
| <div class="display-1" t-out="props.value"/> | ||
| </div> | ||
| </t>`; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { Component, onWillStart, useRef, onMounted, onWillUnmount, useEffect } from "@odoo/owl"; | ||
| import { loadJS } from "@web/core/assets"; | ||
|
|
||
| export class PieChart extends Component { | ||
| static props = { | ||
| data: Object | ||
| } | ||
|
|
||
| static template = "awesome_dashboard.PieChart"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, let's make the props explicit 👍 |
||
|
|
||
| setup() { | ||
| this.canvasRef = useRef("canvas"); | ||
| this.chart = null; | ||
| onWillStart(async () => { | ||
| await loadJS("/web/static/lib/Chart/Chart.js"); | ||
| }); | ||
| onMounted(() => { | ||
| this.renderChart(); | ||
| }); | ||
| onWillUnmount(() => { | ||
| this.chart?.destroy(); | ||
| }); | ||
| useEffect(() => { | ||
| this.renderChart(); | ||
| }, () => [this.props.data]); | ||
| } | ||
|
|
||
| renderChart() { | ||
| if (!this.props.data) { | ||
| return; | ||
| } | ||
| if (this.chart) { | ||
| this.chart.destroy(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The chart is destroyed before re-rendering, which is good, but it is never destroyed when the component is unmounted. Chart.js keeps an object attached to the canvas, so cleaning it up teaches the right lifecycle habit for external libraries. Leaving it as-is accepts a small memory/resource leak when the dashboard is left or reloaded. What I would do instead is import # awesome_dashboard/static/src/dashboard/pie_chart.js (setup): destroy the Chart.js instance when Owl removes the component.
onWillUnmount(() => {
this.chart?.destroy();
}); |
||
| } | ||
| const config = { | ||
| type: 'pie', | ||
| data: { | ||
| labels: Object.keys(this.props.data), | ||
| datasets: [{ | ||
| label: 'T-shirt sizes', | ||
| data: Object.values(this.props.data), | ||
| backgroundColor: [ | ||
| 'rgb(255, 99, 132)', | ||
| 'rgb(54, 162, 235)', | ||
| 'rgb(255, 205, 86)', | ||
| 'rgb(75, 192, 192)', | ||
| 'rgb(153, 102, 255)' | ||
| ], | ||
| }], | ||
| }, | ||
| }; | ||
| this.chart = new Chart(this.canvasRef.el, config); | ||
| } | ||
| } | ||
| 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_dashboard.PieChart"> | ||
| <div class="pie-chart-container d-flex justify-content-center" style="position: relative; height:40vh;"> | ||
| <canvas t-ref="canvas"/> | ||
| </div> | ||
| </t> | ||
| </templates> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We always make our props explicit. Or at least as much as possible. It helps everyone reading the component and debugging. It's probably the only piece of documentation we try to write on every single component 😄