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
7 changes: 2 additions & 5 deletions awesome_dashboard/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
# -*- coding: utf-8 -*-
{
'name': "Awesome Dashboard",

'summary': """
Starting module for "Discover the JS framework, chapter 2: Build a dashboard"
""",

'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',
],
'assets': {
'web.assets_backend': [
'awesome_dashboard/static/src/**/*',
],
'awesome_dashboard.dashboard': ['awesome_dashboard/static/src/dashboard/**/*'],
},
'license': 'AGPL-3'
'license': 'AGPL-3',
}
8 changes: 0 additions & 8 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.

43 changes: 43 additions & 0 deletions awesome_dashboard/static/src/dashboard/config_dialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Component, xml } from "@odoo/owl";
import { Dialog } from "@web/core/dialog/dialog";
import { CheckBox } from "@web/core/checkbox/checkbox";
import { registry } from "@web/core/registry";

export class ConfigDialog extends Component {
static template = xml`<Dialog title="'Config'" close="close">
<t t-foreach="items" t-as="item" t-key="item.id">
<CheckBox id="item.id" value="!props.hiddenItems.includes(item.id)" t-on-change="onChange">
<t t-out="item.description"/>
</CheckBox>
</t>
<t t-set-slot="footer">
<button class="btn btn-primary" t-on-click="onClose">
Update
</button>
</t>
</Dialog>`;
static components = { Dialog, CheckBox };
static props = {
close: Function,
hiddenItems: Array,
};

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

onChange(event) {
if (!event.target.checked) {
this.props.hiddenItems.push(event.target.id);
} else {
const itemIndex = this.props.hiddenItems.findIndex((item) => item === event.target.id);
if (itemIndex !== -1) {
this.props.hiddenItems.splice(itemIndex, 1);
}
}
}

onClose() {
this.props.close();
}
}
51 changes: 51 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Component, useState } from "@odoo/owl";
import { Layout } from "@web/search/layout";
import { useService } from "@web/core/utils/hooks";
import { registry } from "@web/core/registry";

import { DashboardItem } from "./dashboard_item";
import { ConfigDialog } from "./config_dialog";
import { useLocalStorage } from "./use_local_storage";

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

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

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

this.items = registry.category("awesome_dashboard").getAll();
this.hiddenItems = useLocalStorage("awesome_dashboard.hidden_items", []);

this.dialogService = useService("dialog");
}

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

openLeadsView() {
this.action.doAction({
type: "ir.actions.act_window",
name: "Leads",
res_model: "crm.lead",
views: [
[false, "kanban"],
[false, "form"],
],
});
}

openConfigDialog() {
this.dialogService.add(ConfigDialog, {
hiddenItems: this.hiddenItems,
});
}
}

registry.category("actions").add("awesome_dashboard.dashboard", AwesomeDashboard);
5 changes: 5 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.o_dashboard {
background-color: gray;
padding: 4rem;
min-height: 100%;
}
26 changes: 26 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.AwesomeDashboard">
<Layout display="display" className="'o_dashboard'">
<t t-set-slot="control-panel-create-button">
<button type="button" class="btn btn-primary" title="Customers" t-on-click="openCustomersView">Customers</button>
<button type="button" class="btn btn-primary" title="Leads" t-on-click="openLeadsView">Leads</button>
</t>
<t t-set-slot="control-panel-additional-actions">
<button type="button" class="btn">
<i class="fa fa-cog" t-on-click="openConfigDialog" />
</button>
</t>
<div class="d-flex flex-wrap gap-4">
<t t-foreach="items" t-as="item" t-key="item.id" t-if="!hiddenItems.includes(item.id)">
<DashboardItem size="item.size || 1">
<t t-set="itemProp" t-value="item.props ? item.props(statistics.value) : {'data': statistics.value}" />
<t t-component="item.Component" t-props="itemProp" />
</DashboardItem>
</t>
</div>
</Layout>
</t>

</templates>
19 changes: 19 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Component, xml } from "@odoo/owl";

export class DashboardItem extends Component {
static template = xml`<div class="card" t-att-style="'width: ' + 18*props.size + 'rem;'">
<div class="card-body">
<t t-slot="default"/>
</div>
</div>`;
static props = {
size: {
type: Number,
optional: true,
},
slots: { type: Object, optional: true },
};
static defaultProps = {
size: 1,
};
}
49 changes: 49 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { registry } from "@web/core/registry";

import { NumberCard } from "./number_card";
import { PieChartCard } from "./pie_chart_card";

const items = [
{
id: "average_quantity",
description: "Average amount of t-shirt",
Component: NumberCard,
size: 3,
props: (data) => ({
title: "Average amount of t-shirt by order this month",
value: data.average_quantity,
}),
},
{
id: "average_time",
description: "Average time between order creation and shipment",
Component: NumberCard,
size: 1,
props: (data) => ({
title: "Average time between order creation and shipment",
value: data.average_time,
}),
},
{
id: "nb_cancelled_orders",
description: "Average time between order creation and shipment",
Component: NumberCard,
size: 1,
props: (data) => ({
title: "Average time between order creation and shipment",
value: data.nb_cancelled_orders,
}),
},
{
id: "orders_by_size",
description: "Graph of t-shirt sizes ordered",
Component: PieChartCard,
size: 2,
props: (data) => ({
title: "Graph of t-shirt sizes ordered",
value: data.orders_by_size,
}),
},
];

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

export class NumberCard extends Component {
static template = xml`<div>
<p class="font-weight-bold"><t t-out="props.title"/></p>
<p class="text-success fs-2 text-center"><t t-out="props.value"/></p>
</div>`;
}
42 changes: 42 additions & 0 deletions awesome_dashboard/static/src/dashboard/pie_chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Component, onWillStart, useEffect, useRef, xml } from "@odoo/owl";
import { loadJS } from "@web/core/assets";

export class PieChart extends Component {
static template = xml`<div><canvas t-ref="piechart"/></div>`;
static props = {
data: Object,
};

chartRef = useRef("piechart");

setup() {
this.chart = null;

onWillStart(() => loadJS(["/web/static/lib/Chart/Chart.js"]));
useEffect(
() => {
if (!this.chartRef.el) {
return;
}
this.chart = new Chart(this.chartRef.el, {
type: "pie",
data: {
labels: Object.keys(this.props.data),
datasets: [
{
data: Object.values(this.props.data),
},
],
},
});

return () => {
if (this.chart) {
this.chart.destroy();
}
};
},
() => [this.chartRef, this.props.data]
);
}
}
10 changes: 10 additions & 0 deletions awesome_dashboard/static/src/dashboard/pie_chart_card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Component, xml } from "@odoo/owl";
import { PieChart } from "./pie_chart";

export class PieChartCard extends Component {
static template = xml`<div>
<p class="font-weight-bold"><t t-out="props.title"/></p>
<PieChart data="props.value" />
</div>`;
static components = { PieChart };
}
29 changes: 29 additions & 0 deletions awesome_dashboard/static/src/dashboard/statistics_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { reactive } from "@odoo/owl";
import { rpc } from "@web/core/network/rpc";
import { registry } from "@web/core/registry";

async function loadStatistics() {
return rpc("/awesome_dashboard/statistics");
}

const cachedStatsServices = {
start() {
const statistics = reactive({ value: null });

loadStatistics().then((value) => {
statistics.value = value;
});

setInterval(
() =>
loadStatistics().then((value) => {
statistics.value = value;
}),
600_000
);

return statistics;
},
};

registry.category("services").add("awesome_dashboard.statistics", cachedStatsServices);
9 changes: 9 additions & 0 deletions awesome_dashboard/static/src/dashboard/use_local_storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { reactive, useState } from "@odoo/owl";

export function useLocalStorage(key, initialState) {
const state = JSON.parse(localStorage.getItem(key)) || initialState;
const store = (obj) => localStorage.setItem(key, JSON.stringify(obj));
const reactiveState = reactive(state, () => store(reactiveState));
store(reactiveState);
return useState(state);
}
10 changes: 10 additions & 0 deletions awesome_dashboard/static/src/dashboard_action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Component, xml } from "@odoo/owl";
import { registry } from "@web/core/registry";
import { LazyComponent } from "@web/core/assets";

export class DashboardLoader extends Component {
static components = { LazyComponent };
static template = xml`<LazyComponent bundle="'awesome_dashboard.dashboard'" Component="'awesome_dashboard.AwesomeDashboard'"/>`;
}

registry.category("actions").add("awesome_dashboard.dashboard_action", DashboardLoader);
17 changes: 17 additions & 0 deletions awesome_owl/static/src/Card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Component, useState } from "@odoo/owl";

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

setup() {
this.state = useState({ minimized: false });
this.toggle = this.toggle.bind(this);
}

toggle() {
console.log("toggle");
console.log(this.state.minimized);
this.state.minimized = !this.state.minimized;
console.log(this.state.minimized);
}
}
16 changes: 16 additions & 0 deletions awesome_owl/static/src/Card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="awesome_owl.Card">
<div class="card">
<div class="card-header">
<button class="btn btn-secondary" t-on-click="toggle">Toggle</button>
</div>
<div class="card-body" t-if="!state.minimized">
<h1 class="card-title">
<t t-slot="title">Card</t>
</h1>
<t t-slot="default" />
</div>
</div>
</t>
</templates>
Loading