From 4783afaefe0dc7310eaa6f21219c33bbfe1e61e7 Mon Sep 17 00:00:00 2001 From: Olmo del Corral Date: Wed, 20 Feb 2019 23:28:59 +0100 Subject: [PATCH] example using React hooks --- Framework | 2 +- .../Southwind/Templates/SalesEstimation.tsx | 107 +++++++----------- 2 files changed, 40 insertions(+), 69 deletions(-) diff --git a/Framework b/Framework index e42d0bf1..9ae1966a 160000 --- a/Framework +++ b/Framework @@ -1 +1 @@ -Subproject commit e42d0bf149d6133b4ef8326e6095c2fb73f6a940 +Subproject commit 9ae1966a4c7b835093f69e44c2f17a31e9415a67 diff --git a/Southwind.React/App/Southwind/Templates/SalesEstimation.tsx b/Southwind.React/App/Southwind/Templates/SalesEstimation.tsx index 803ec6a3..31e94a65 100644 --- a/Southwind.React/App/Southwind/Templates/SalesEstimation.tsx +++ b/Southwind.React/App/Southwind/Templates/SalesEstimation.tsx @@ -1,9 +1,9 @@ -import * as React from 'react' +import * as React from 'react' import { ProductEntity, ProductPredictorPublication } from '../Southwind.Entities' import { ValueLine, EntityLine, EntityCombo, EntityList, EntityDetail, EntityStrip, EntityRepeater, TypeContext, FormControlReadonly, FormGroup } from '@framework/Lines' import * as Finder from '@framework/Finder'; -import { is } from '@framework/Signum.Entities'; -import { ajaxPost } from '@framework/Services'; +import { is, liteKey } from '@framework/Signum.Entities'; +import { ajaxPost, useAPI } from '@framework/Services'; import { Gradient, Color } from '@extensions/Basics/Color'; import { toLite } from '@framework/Signum.Entities'; import { classes } from '@framework/Globals'; @@ -11,77 +11,48 @@ import { EntityControlMessage } from '@framework/Signum.Entities'; import { PredictorEntity } from '@extensions/MachineLearning/Signum.Entities.MachineLearning'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' -interface SalesEstimationProps { - ctx: TypeContext -} -interface SalesEstimationState { - estimation?: number; -} +var gradient = new Gradient([ + { value: 0, color: Color.parse("red") }, + { value: 1, color: Color.parse("gold") }, + { value: 2, color: Color.parse("green") }, + { value: 3, color: Color.parse("blue") }, +]); -export default class SalesEstimation extends React.Component { +export default function SalesEstimation({ ctx }: { ctx: TypeContext }) { - constructor(props: SalesEstimationProps) { - super(props); - this.state = {}; - } - - componentWillMount() { - this.loadData(this.props); - } - - componentWillReceiveProps(newProps: SalesEstimationProps) { - if (!is(newProps.ctx.value, this.props.ctx.value)) - this.loadData(newProps); - } - - loadData(props: SalesEstimationProps) { - ajaxPost({ url: "~/api/salesEstimation" }, toLite(props.ctx.value)) - .then(estimation => this.setState({ estimation })) - .done(); + function handleViewClick(e: React.MouseEvent) { + e.preventDefault(); + Finder.exploreWindowsOpen({ queryName: PredictorEntity, parentToken: "Entity.Publication", parentValue: ProductPredictorPublication.MonthlySales }, e); } - gradient = new Gradient([ - { value: 0, color: Color.parse("red") }, - { value: 1, color: Color.parse("gold") }, - { value: 2, color: Color.parse("green") }, - { value: 3, color: Color.parse("blue") }, - ]); - - render() { - const ctx = this.props.ctx; + const estimation = useAPI(undefined, [ctx.value.id], signal => ajaxPost({ url: "~/api/salesEstimation", signal }, toLite(ctx.value))); - const color = this.state.estimation != null ? this.gradient.getCachedColor(this.props.ctx.value.unitsInStock! / (Math.max(1, this.state.estimation))) : undefined; + const color = estimation != null ? gradient.getCachedColor(ctx.value.unitsInStock! / (Math.max(1, estimation))) : undefined; - return ( - - { - this.state.estimation != null && -
-
- - - -
-

- {this.state.estimation} -

-
- - - -
+ return ( + + { + estimation != null && +
+
+ + +
- } - - ); - } - - handleViewClick = (e: React.MouseEvent) => { - e.preventDefault(); - Finder.exploreWindowsOpen({ queryName: PredictorEntity, parentToken: "Entity.Publication", parentValue: ProductPredictorPublication.MonthlySales }, e); - } -}//SalesEstimation +

+ {estimation} +

+
+ + + +
+
+ } +
+ ); +}