Skip to content

Commit

Permalink
example using React hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
olmobrutall committed Feb 20, 2019
1 parent cb4911f commit 4783afa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Framework
107 changes: 39 additions & 68 deletions Southwind.React/App/Southwind/Templates/SalesEstimation.tsx
Original file line number Original file line Diff line number Diff line change
@@ -1,87 +1,58 @@
import * as React from 'react' import * as React from 'react'
import { ProductEntity, ProductPredictorPublication } from '../Southwind.Entities' import { ProductEntity, ProductPredictorPublication } from '../Southwind.Entities'
import { ValueLine, EntityLine, EntityCombo, EntityList, EntityDetail, EntityStrip, EntityRepeater, TypeContext, FormControlReadonly, FormGroup } from '@framework/Lines' import { ValueLine, EntityLine, EntityCombo, EntityList, EntityDetail, EntityStrip, EntityRepeater, TypeContext, FormControlReadonly, FormGroup } from '@framework/Lines'
import * as Finder from '@framework/Finder'; import * as Finder from '@framework/Finder';
import { is } from '@framework/Signum.Entities'; import { is, liteKey } from '@framework/Signum.Entities';
import { ajaxPost } from '@framework/Services'; import { ajaxPost, useAPI } from '@framework/Services';
import { Gradient, Color } from '@extensions/Basics/Color'; import { Gradient, Color } from '@extensions/Basics/Color';
import { toLite } from '@framework/Signum.Entities'; import { toLite } from '@framework/Signum.Entities';
import { classes } from '@framework/Globals'; import { classes } from '@framework/Globals';
import { EntityControlMessage } from '@framework/Signum.Entities'; import { EntityControlMessage } from '@framework/Signum.Entities';
import { PredictorEntity } from '@extensions/MachineLearning/Signum.Entities.MachineLearning'; import { PredictorEntity } from '@extensions/MachineLearning/Signum.Entities.MachineLearning';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'


interface SalesEstimationProps {
ctx: TypeContext<ProductEntity>
}


interface SalesEstimationState { var gradient = new Gradient([
estimation?: number; { 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<SalesEstimationProps, SalesEstimationState> { export default function SalesEstimation({ ctx }: { ctx: TypeContext<ProductEntity> }) {


constructor(props: SalesEstimationProps) { function handleViewClick(e: React.MouseEvent<any>) {
super(props); e.preventDefault();
this.state = {}; Finder.exploreWindowsOpen({ queryName: PredictorEntity, parentToken: "Entity.Publication", parentValue: ProductPredictorPublication.MonthlySales }, e);
}

componentWillMount() {
this.loadData(this.props);
}

componentWillReceiveProps(newProps: SalesEstimationProps) {
if (!is(newProps.ctx.value, this.props.ctx.value))
this.loadData(newProps);
}

loadData(props: SalesEstimationProps) {
ajaxPost<number>({ url: "~/api/salesEstimation" }, toLite(props.ctx.value))
.then(estimation => this.setState({ estimation }))
.done();
} }


gradient = new Gradient([ const estimation = useAPI(undefined, [ctx.value.id], signal => ajaxPost<number>({ url: "~/api/salesEstimation", signal }, toLite(ctx.value)));
{ 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 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 ( return (
<FormGroup ctx={ctx} labelText="Sales next month" helpText="Monthly estimation using Predicitor extension"> <FormGroup ctx={ctx} labelText="Sales next month" helpText="Monthly estimation using Predicitor extension">
{ {
this.state.estimation != null && estimation != null &&
<div className={ctx.inputGroupClass}> <div className={ctx.inputGroupClass}>
<div className="input-group-prepend"> <div className="input-group-prepend">
<span className="input-group-text"> <span className="input-group-text">
<FontAwesomeIcon icon={["far", "lightbulb"]} /> <FontAwesomeIcon icon={["far", "lightbulb"]} />
</span> </span>
</div>
<p className={classes(ctx.formControlClass, "readonly numeric")} style={{ color: color && color.toString() }}>
{this.state.estimation}
</p>
<div className="input-group-append">
<a href="#" className={classes("sf-line-button", "sf-view", "btn input-group-text")}
onClick={this.handleViewClick}
title={EntityControlMessage.View.niceToString()}>
<FontAwesomeIcon icon={"arrow-right"} />
</a>
</div>
</div> </div>
} <p className={classes(ctx.formControlClass, "readonly numeric")} style={{ color: color && color.toString() }}>
</FormGroup> {estimation}
); </p>
} <div className="input-group-append">

<a href="#" className={classes("sf-line-button", "sf-view", "btn input-group-text")}
handleViewClick = (e: React.MouseEvent<any>) => { onClick={handleViewClick}
e.preventDefault(); title={EntityControlMessage.View.niceToString()}>
Finder.exploreWindowsOpen({ queryName: PredictorEntity, parentToken: "Entity.Publication", parentValue: ProductPredictorPublication.MonthlySales }, e); <FontAwesomeIcon icon={"arrow-right"} />
} </a>
}//SalesEstimation </div>
</div>
}
</FormGroup>
);
}


0 comments on commit 4783afa

Please sign in to comment.