From 7eb978125a11186405b5bd52b84ad0eaf8cccb7b Mon Sep 17 00:00:00 2001 From: Ares19v <138339303+Ares19v@users.noreply.github.com> Date: Tue, 8 Oct 2024 02:28:38 +0530 Subject: [PATCH 1/3] Update App.tsx --- src/App.tsx | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 0728518c0d8..15dcbc7bea8 100755 --- a/src/App.tsx +++ b/src/App.tsx @@ -8,11 +8,12 @@ import './App.css'; */ interface IState { data: ServerRespond[], + showGraph: boolean, } /** * The parent element of the react app. - * It renders title, button and Graph react element. + * It renders title, button, and Graph react element. */ class App extends Component<{}, IState> { constructor(props: {}) { @@ -22,6 +23,8 @@ class App extends Component<{}, IState> { // data saves the server responds. // We use this state to parse data down to the child element (Graph) as element property data: [], + // showGraph controls whether to display the graph or not + showGraph: false, }; } @@ -29,18 +32,24 @@ class App extends Component<{}, IState> { * Render Graph react component with state.data parse as property data */ renderGraph() { - return () + return this.state.showGraph ? : null; } /** * Get new data from server and update the state with the new data */ getDataFromServer() { - DataStreamer.getData((serverResponds: ServerRespond[]) => { - // Update the state by creating a new array of data that consists of - // Previous data in the state and the new data from server - this.setState({ data: [...this.state.data, ...serverResponds] }); - }); + let x = 0; + const interval = setInterval(() => { + DataStreamer.getData((serverResponds: ServerRespond[]) => { + this.setState({ data: [...this.state.data, ...serverResponds], showGraph: true }); + }); + + x++; + if (x > 1000) { + clearInterval(interval); + } + }, 100); } /** @@ -54,11 +63,6 @@ class App extends Component<{}, IState> {
@@ -67,7 +71,7 @@ class App extends Component<{}, IState> {
- ) + ); } } From 297911faaaa6008d0967f2d4575b193127745941 Mon Sep 17 00:00:00 2001 From: Ares19v <138339303+Ares19v@users.noreply.github.com> Date: Tue, 8 Oct 2024 02:30:51 +0530 Subject: [PATCH 2/3] Update Graph.tsx --- src/Graph.tsx | 113 ++++++++++++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 54 deletions(-) diff --git a/src/Graph.tsx b/src/Graph.tsx index 3b2a7da1a38..15dcbc7bea8 100644 --- a/src/Graph.tsx +++ b/src/Graph.tsx @@ -1,73 +1,78 @@ import React, { Component } from 'react'; -import { Table } from '@finos/perspective'; -import { ServerRespond } from './DataStreamer'; -import './Graph.css'; +import DataStreamer, { ServerRespond } from './DataStreamer'; +import Graph from './Graph'; +import './App.css'; /** - * Props declaration for + * State declaration for */ -interface IProps { +interface IState { data: ServerRespond[], + showGraph: boolean, } /** - * Perspective library adds load to HTMLElement prototype. - * This interface acts as a wrapper for Typescript compiler. + * The parent element of the react app. + * It renders title, button, and Graph react element. */ -interface PerspectiveViewerElement { - load: (table: Table) => void, -} +class App extends Component<{}, IState> { + constructor(props: {}) { + super(props); -/** - * React component that renders Perspective based on data - * parsed from its parent through data property. - */ -class Graph extends Component { - // Perspective table - table: Table | undefined; - - render() { - return React.createElement('perspective-viewer'); + this.state = { + // data saves the server responds. + // We use this state to parse data down to the child element (Graph) as element property + data: [], + // showGraph controls whether to display the graph or not + showGraph: false, + }; } - componentDidMount() { - // Get element to attach the table from the DOM. - const elem: PerspectiveViewerElement = document.getElementsByTagName('perspective-viewer')[0] as unknown as PerspectiveViewerElement; - - const schema = { - stock: 'string', - top_ask_price: 'float', - top_bid_price: 'float', - timestamp: 'date', - }; + /** + * Render Graph react component with state.data parse as property data + */ + renderGraph() { + return this.state.showGraph ? : null; + } - if (window.perspective && window.perspective.worker()) { - this.table = window.perspective.worker().table(schema); - } - if (this.table) { - // Load the `table` in the `` DOM reference. + /** + * Get new data from server and update the state with the new data + */ + getDataFromServer() { + let x = 0; + const interval = setInterval(() => { + DataStreamer.getData((serverResponds: ServerRespond[]) => { + this.setState({ data: [...this.state.data, ...serverResponds], showGraph: true }); + }); - // Add more Perspective configurations here. - elem.load(this.table); - } + x++; + if (x > 1000) { + clearInterval(interval); + } + }, 100); } - componentDidUpdate() { - // Everytime the data props is updated, insert the data into Perspective table - if (this.table) { - // As part of the task, you need to fix the way we update the data props to - // avoid inserting duplicated entries into Perspective table again. - this.table.update(this.props.data.map((el: any) => { - // Format the data from ServerRespond to the schema - return { - stock: el.stock, - top_ask_price: el.top_ask && el.top_ask.price || 0, - top_bid_price: el.top_bid && el.top_bid.price || 0, - timestamp: el.timestamp, - }; - })); - } + /** + * Render the App react component + */ + render() { + return ( +
+
+ Bank & Merge Co Task 2 +
+
+ +
+ {this.renderGraph()} +
+
+
+ ); } } -export default Graph; +export default App; From 6aebdf76241048d5ab7fe26d95410c8415781f68 Mon Sep 17 00:00:00 2001 From: Ares19v <138339303+Ares19v@users.noreply.github.com> Date: Tue, 8 Oct 2024 02:36:39 +0530 Subject: [PATCH 3/3] Update Graph.tsx --- src/Graph.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Graph.tsx b/src/Graph.tsx index 15dcbc7bea8..8e85ae56f9e 100644 --- a/src/Graph.tsx +++ b/src/Graph.tsx @@ -52,6 +52,7 @@ class App extends Component<{}, IState> { }, 100); } + /** * Render the App react component */