import * as React from 'react'; import { connect } from 'react-redux'; import { ApplicationState } from '../store'; import * as PulpTestsStore from '../store/PulpTests'; import PulpTestForm from '../components/PulpTestForm'; import { GridCellProps } from '@progress/kendo-react-grid'; import { Window } from '@progress/kendo-react-dialogs'; import SPCChart from '../components/SPCChart' import FilterCard from '../components/FilterCard' import { v4 as uuidv4 } from 'uuid'; import { systems, PulpTestProps, PulpTest, gridState, Direction, materials, materialgrades, pulpblends } from '../interfaces/common' import JHDataGrid from '../components/DataGrid'; import { authProvider } from '../store/azuread'; import { Card, CardBody, PanelBar, PanelBarItem } from '@progress/kendo-react-layout'; import { DatePicker } from '@progress/kendo-react-dateinputs'; import { ComboBox } from '@progress/kendo-react-dropdowns'; import * as ss from 'simple-statistics' class CustomCell extends React.Component { render() { if (this.props.field !== undefined) { const value = this.props.dataItem[this.props.field]; return ( {(value === null) ? '' : this.props.dataItem[this.props.field].toString()} ); } else { return ( ) } } } class FetchData extends React.PureComponent { constructor(props: any) { super(props) const dataState = { skip: 0, take: 500, sort: [{ field: 'sample_taken_on', dir: Direction.DSC }], group: [] } this.state = { data: this.props.pulptests, dataState: dataState, currentLocale: { languge: 'en-US', localse: 'en' }, visibleForm: false, visiblePopup: true, systems: [], material: [], newFormSystem: { id: 0, name: "one", subtype: "one" }, materialgradedropdowndata: [], pulpblenddropdowndata: [], consistency_oven_dried: false, contentHeight: 0, contentWidth: 0, grid: null, gridWidth: 1400, pulptest: undefined, httpMethod: '', consistency_spc: { usl: undefined, lsl: undefined, target: undefined, ucl: undefined, lcl: undefined, stdv: undefined, mean: undefined }, freeness_spc: { usl: undefined, lsl: undefined, target: undefined, ucl: undefined, lcl: undefined, stdv: undefined, mean: undefined }, token: "", reportEndTime: undefined, reportStartTime: undefined, reportLine: undefined } this.toggleDialog = this.toggleDialog.bind(this);//bind toggle this.closeDialog = this.closeDialog.bind(this);//bind close this.readSignalR = this.readSignalR.bind(this); } async toggleDialog(e: systems, data: any, httpMethod: string) { this.setState({ visibleForm: !this.state.visibleForm, newFormSystem: e, httpMethod: httpMethod, }); if (httpMethod === "PUT") { this.setState({ pulptest: data }) } else { this.setState({ pulptest: {} as PulpTest }) } this.setState({ token: (await authProvider.getIdToken()).idToken.rawIdToken }) } closeDialog() { this.setState({ visiblePopup: !this.state.visiblePopup, }); //this.props.closeDialog() } dataStateChange = (event: any) => { this.setState({ data: [...this.props.pulptests], dataState: event.data }) } public async componentDidMount() { this.ensureDataFetched(); this.ensureInitialDataFetched(); this.readSignalR(); var token = (await authProvider.getIdToken()).idToken.rawIdToken; this.setState({ token: token }) let griddy = document.querySelector('.k-grid') as HTMLElement let griddyHeader = document.querySelectorAll('.k-grid-header-wrap') if (griddy !== null) { } if (griddyHeader !== null) { griddyHeader.forEach(function (header: any, key: number, listobj: any) { header.className = header.className + " jh-header" }) } } public setPercentage = (percentage: number) => { return Math.round(500 / 100) * percentage; } public readSignalR() { if (this.props.registerSignalR) { this.props.registerSignalR(Math.random()); } } public render() { var form; if (this.state.systems.length > 0 && this.props.isLoading === false) { form = return (

Pulp Tests

{this.renderButtons()}
{this.renderExport()}
{this.renderPulpCharts()}
{this.renderPulpTestsTable()}
{this.state.visibleForm &&

TEST FOR {this.state.newFormSystem.name}

{form}
} {this.props.visibleReportPopup && this.exportCSVFile()}
); } else { return
Loading...
} } public setFilterParams() { } private async ensureDataFetched() { let materialData: materials[] = [] as materials[]; let systemData = [] as systems[]; let materialgradeData = [] as materialgrades[]; let pulpblendData = [] as pulpblends[]; let consistencyOven = false; var token = null if (authProvider.getLoginInProgress() === false) { token = (await authProvider.getIdToken()).idToken.rawIdToken } if (this.state.systems[0] === undefined) { try { materialData = await fetch('materials/entities.json', { headers: !token ? { 'Access-Control-Allow-Origin': '*' } : { 'Authorization': "Bearer " + token, 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' } }) .then((response: Response) => { if (!response.ok) { throw response; } else { return response.json(); } }) .then((data) => { return [data] })// as materials[] this.setState({ material: materialData }) } catch (error) { console.log(error) } try { systemData = await fetch('systems/entities.json?materialid=2', { headers: !token ? { 'Access-Control-Allow-Origin': '*' } : { 'Authorization': "Bearer " + token, 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' } }) .then((response: Response) => { if (!response.ok) { throw response; } else { return response.json(); } }) .then((data) => { return data }) this.setState({ systems: systemData }) } catch (error) { console.log(error) } try { materialgradeData = await fetch('material_grades/entities.json?materialid=2', { headers: !token ? { 'Access-Control-Allow-Origin': '*' } : { 'Authorization': "Bearer " + token, 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' } }) .then((response: Response) => { if (!response.ok) { throw response; } else { return response.json(); } }) .then((data) => { return data }) this.setState({ materialgradedropdowndata: materialgradeData }) } catch (error) { console.log(error) } try { pulpblendData = await fetch( process.env.REACT_APP_PULPBLENDS + 'api/entities.json', {//pulp_blends/entities.json', { headers: !token ? { 'Access-Control-Allow-Origin': '*' } : { 'Authorization': "Bearer " + token, 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' } }) .then((response: Response) => { if (!response.ok) { throw response; } else { return response.json(); } }) .then((data) => { data = data.map((pulpblendData: pulpblends) => { pulpblendData.descriptionCombined = pulpblendData.description + ' - ' + pulpblendData.code; return pulpblendData; }) return data }) this.setState({ pulpblenddropdowndata: pulpblendData }) } catch (error) {} try { consistencyOven = await fetch('preferences/getpreferencesbykey.json?key=consistency_oven_dried', { headers: !token ? { 'Access-Control-Allow-Origin': '*' } : { 'Authorization': "Bearer " + token, 'no-cors': '*', 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' } }) .then((response: Response) => { if (!response.ok) { throw response; } else { return response.json(); } }) .then((data) => { return JSON.parse(data.treated_value) }) this.setState({ consistency_oven_dried: consistencyOven }) } catch (error) { console.log(error) } } } private ensureInitialDataFetched() { if (this.props.requestPulpTests) { this.props.requestPulpTests(Math.random()); } } public renderButtons() { return ( this.state.systems.map((e: systems, key: any) => )); } public SPCHelperBlock(filteredData: any, systemname: any, processLimitData: any, categoriesData: any, materialGradeData: any, title: string, decimals: number = 2) { let spcCode = if (filteredData.length > 0 && this.props.pulptest !== undefined) { spcCode = ( ) } return spcCode } public renderPulpCharts() { if (this.props.pulptests !== undefined) { let resultsSystem = [... new Set(this.props.pulptests.map((e: PulpTest) => e.systemname))] let filteredData = [] let categoryData = [] let materialGradeData = [] let consistencyChartData = [] let consistencyProcessLimitData = [] let freenessChartData = [] let freenessProcessLimitData = [] return ( {this.props.pulptests.length > 0 &&
{resultsSystem.map ((e: string) => { filteredData = this.props.pulptests.filter((filter: PulpTest) => filter.systemname === e) categoryData = filteredData.map(datapoint => new Date(datapoint.sample_taken_on).getHours() + ':' + new Date(datapoint.sample_taken_on).getMinutes()) materialGradeData = [...new Set(filteredData.map(dataitem => dataitem.materialgradename))] consistencyChartData = filteredData.map((data: PulpTest) => { return { materialgradename: data.materialgradename, datavalue: data.consistency, dataextended: data } }) consistencyProcessLimitData = filteredData.map((data: PulpTest) => { return { materialgradename: data.materialgradename, datausl: data.consistency_usl, datalsl: data.consistency_lsl, datatarget: data.consistency_target } }) freenessChartData = filteredData.map((data: PulpTest) => { return { materialgradename: data.materialgradename, datavalue: data.corrected_freeness, dataextended: data } }) freenessProcessLimitData = filteredData.map((data: PulpTest) => { return { materialgradename: data.materialgradename, datausl: data.freeness_usl, datalsl: data.freeness_lsl, datatarget: data.freeness_target } }) return ( {this.SPCHelperBlock(consistencyChartData, e, consistencyProcessLimitData, categoryData, materialGradeData, "Consistency")} {this.SPCHelperBlock(freenessChartData, e, freenessProcessLimitData, categoryData, materialGradeData, "Freeness")} ) }) }
}
) } } MyCustomCell = (props: any) => public renderPulpTestsTable() { if (this.props.pulptests.length > 0) { let gridAllData: PulpTest[] = this.props.pulptests //has all the pulp test data let materialgradenames = [...new Set(this.props.pulptests.map((e: PulpTest) => e.materialgradename))]//I should probably do this somewhere else so it can be reused let pulpblendcodes = [...new Set(this.props.pulptests.map((e: PulpTest) => e.pulpBlendCode))]//I should probably do this somewhere else so it can be reused var GridTables = this.state.systems.map((system: systems, key: any) => { let gridAllSystemData = gridAllData.filter((filter: PulpTest) => filter.systemname === system.name) let returnGrid = if (gridAllSystemData.length > 0) { let mean = undefined; let last24sData = gridAllSystemData.filter((filter: PulpTest) => filter.sample_taken_on > new Date(new Date().getTime() - (24 * 60 * 60 * 1000))).map((filter: PulpTest) => filter.consistency) if (last24sData.length > 0) { mean = ss.mean(last24sData).toFixed(2) } returnGrid =

{system.name} - Avg Consistency Last 24 Hours: {mean}

} return returnGrid; }) return ( {GridTables} ); } else { return ( ) } } public editButton = (props: any) => { return ( ) } public renderEditForm(data: any) { data = JSON.parse(data.target.value) this.toggleDialog(data.system, data, "PUT") } // Re-usable change handler public handleSingleChange = (event: any) => { this.setState({ [event.target.name]: event.target.value } as Pick, () => {}); } public renderExport() { return (
) } public exportCSVFile() { let window = //this.state.visiblePopup: {true } --- not sure where and how to set visiblePopup back to true
START:
END:
LINE:
return (
{this.state.visiblePopup && window}
) } public download = (reportType: string = "xlsx") => async ( event: any ): Promise => { var data = { sample_tested_at_from: new Date(Date.now()), sample_tested_at_to: new Date(Date.now()), line: "", scope: "", } let apiURL = '/api/entities/' if (reportType === "xslx") { apiURL = apiURL + 'report' } else { apiURL = apiURL + 'reportcsv' } if (this.state.reportStartTime !== undefined && this.state.reportEndTime !== undefined) { if (this.state.reportLine !== undefined) { apiURL = apiURL + "?sampled_at=" + this.state.reportStartTime.toLocaleDateString() + '..' + this.state.reportEndTime.toLocaleDateString() + '&system_id=' + this.state.reportLine.id; } else { apiURL = apiURL + "?sampled_at=" + this.state.reportStartTime.toLocaleDateString() + '..' + this.state.reportEndTime.toLocaleDateString() } window.location.href = apiURL; this.closeDialog(); } else { alert("error: parameters are incomplete"); } } } export default connect( (state: ApplicationState) => state.pulpTests, // Selects which state properties are merged into the component's props PulpTestsStore.actionCreators // Selects which action creators are merged into the component's props )(FetchData as any);