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

[IMP] first exo #142

Closed
wants to merge 9 commits into from
Closed
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 awesome_dashboard/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
'web.assets_backend': [
'awesome_dashboard/static/src/**/*',
],
'awesome_dashboard.dashboard': [
'awesome_dashboard/static/src/dashboard/**/*',
],

},
'license': 'AGPL-3'
}
1 change: 1 addition & 0 deletions awesome_dashboard/controllers/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import random
import time

from odoo import http
from odoo.http import request
Expand Down
10 changes: 0 additions & 10 deletions awesome_dashboard/static/src/dashboard.js

This file was deleted.

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

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

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

setup() {
this.action = useService("action");
this.stats = useState(useService("awesome_dashboard.statistics"));
}

displayCustomerKanban() {
this.action.doAction("base.action_partner_form")
}

displayLeads() {

this.action.doAction({
type: 'ir.actions.act_window',
name: 'Leads',
target: 'current',
res_model: 'crm.lead',
views: [[false, 'form'], [false,'tree']],
});
}

}

registry.category("lazy_components").add("awesome_dashboard", AwesomeDashboard);
3 changes: 3 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.o_dashboard {
background-color: gray;
}
30 changes: 30 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard.xml
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 display="{controlPanel: {}}" className="'o_dashboard h-100'" t-if="stats.isReady">
<t t-set-slot="layout-buttons">
<button class="btn btn-primary" t-on-click="displayCustomerKanban">Customers</button>
<button class="btn btn-primary" t-on-click="displayLeads">Leads</button>
</t>

<DashboardItem>
<p>Average Quantity</p>
<t t-esc="stats.average_quantity"/>
</DashboardItem>

<DashboardItem>

<PieChart data="stats['orders_by_size']"/>
</DashboardItem>

<!-- <t t-foreach="statsService" t-as="stat" t-key="stat">-->
<!-- <DashboardItem>-->
<!-- <t t-esc="stat"/><p> </p><t t-esc="stats[stat]"/>-->
<!-- </DashboardItem>-->
<!-- </t>-->
</Layout>
</t>

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

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

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

};

}
11 changes: 11 additions & 0 deletions awesome_dashboard/static/src/dashboard/dashboard_item.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.dashboard_item">
<div class="card d-inline-block m-2" t-att-style="'width:' + (18 * props.size) + 'rem;'">
<div class="card-body">
<t t-slot="default"/>
</div>
</div>
</t>
</templates>
46 changes: 46 additions & 0 deletions awesome_dashboard/static/src/dashboard/piechart/piechart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/** @odoo-module **/

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


export class PieChart extends Component {

static template = "awesome_dashboard.piechart";

static props = {
data: {type: Object}
}

setup(){

this.canvasRef = useRef("canvas");
onWillStart(async () => loadJS(["/web/static/lib/Chart/Chart.js"]));
useEffect(() => {this.renderChart()})

}


renderChart(){
if(this.chart){
this.chart.destroy()
}
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
@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_dashboard.AwesomeDashboard">
hello dashboard
<t t-name="awesome_dashboard.piechart">
<canvas t-ref="canvas"/>
</t>

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

import { registry } from "@web/core/registry";
import { useService } from "@web/core/utils/hooks";
import { memoize } from "@web/core/utils/functions";
import { reactive } from "@odoo/owl";

const statistics = {
dependencies: ["rpc"],
start(env, { rpc }){

const stats = reactive({isReady: false});

async function loadStatistics() {
let res = await rpc("/awesome_dashboard/statistics");

Object.assign(stats, res, {isReady: true});
console.log(stats);
}


setInterval(async () => {
await loadStatistics();
}, 1000*60*10);

loadStatistics();

// loadStatistics().then(() => {
// setInterval(loadStatistics, 10);
// });

return stats
}
}

registry.category("services").add("awesome_dashboard.statistics", statistics);
14 changes: 14 additions & 0 deletions awesome_dashboard/static/src/dashboard_action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @odoo-module **/

import { LazyComponent } from "@web/core/assets";
import { registry } from "@web/core/registry";
import { Component, xml } from "@odoo/owl";

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

registry.category("actions").add("awesome_dashboard.dashboard", DashboardAction);
22 changes: 22 additions & 0 deletions awesome_owl/static/src/card/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** @odoo-module **/

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

export class Card extends Component {
static template = "awesome_owl.card";
static props = {
title: {type: String},
slots: {type: Object}
};

setup (){
this.state = useState({ value:0, open:true});
}

onChange (){}

toggleVisible(){
this.state.open = !this.state.open;
}

}
13 changes: 13 additions & 0 deletions awesome_owl/static/src/card/card.xml
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_owl.card">
<div class="card d-inline-block m-2" style="width: 18rem;">
<button class="btn btn-primary" t-on-click="toggleVisible">Hide</button>
<h5 class="card-title"><t t-out="props.title"/></h5>
<div class="card-body" t-if="this.state.open == true">
<t t-slot="default"/>
</div>
</div>
</t>
</templates>
21 changes: 21 additions & 0 deletions awesome_owl/static/src/counter/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/** @odoo-module **/

import { Component, 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:0 });
}

increment (){
this.state.value++;
if (this.props.onChange){
this.props.onChange()
}
}
}
12 changes: 12 additions & 0 deletions awesome_owl/static/src/counter/counter.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_owl.counter">
<div class="p-3">
<div>
<p>Counter: <t t-esc="state.value"/></p>
<button class="btn btn-primary" t-on-click="increment">Increment</button>
</div>
</div>
</t>
</templates>
17 changes: 16 additions & 1 deletion awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";
import { markup, Component, useState } from "@odoo/owl";
import { Counter } from "./counter/counter";
import { Card } from "./card/card";
import { TodoList } from "./todo/todo_list";

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

value1 = "<div>Text</div>";
value2 = markup("<a href='https://github.com/odoo/owl/blob/master/doc/reference/templates.md#outputting-data' target='_blank'>Click Me</a>");

setup (){
this.state = useState({ totalSum:0 });
}

incrementTotalSum (){
this.state.totalSum += 1;
}
}
17 changes: 16 additions & 1 deletion awesome_owl/static/src/playground.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@

<t t-name="awesome_owl.playground">
<div class="p-3">
hello world
<Card title="'card 1'">
<Counter/>
</Card>
<div>
<TodoList/>
</div>
<div class="me-3">
<p>Hello World</p>
</div>
<div>
<h3>Total Sum: <t t-esc="state.totalSum"/></h3>
<Counter onChange.bind="incrementTotalSum"/>
<Counter onChange.bind="incrementTotalSum"/>
</div>


</div>
</t>

Expand Down
21 changes: 21 additions & 0 deletions awesome_owl/static/src/todo/todo_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/** @odoo-module **/

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

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

static props = {
todo: {type: Object, shape: {id: Number, description: String, isCompleted: Boolean}},
toggleState: {type: Function},
removeTodo: {type: Function}
};

change (){
this.props.toggleState(this.props.todo.id)
}

remove (){
this.props.removeTodo(this.props.todo.id)
}
}
Loading