-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Functional components - Single socket connection - 0 lines of css - Split util functions to dedicated file - Other minor changes
- Loading branch information
1 parent
1f29bbf
commit 252f7e1
Showing
16 changed files
with
108 additions
and
166 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,31 @@ | ||
import React, { Component, Fragment } from 'react'; | ||
import AccountDisplay from './account'; | ||
import React, { useState, useEffect, Fragment } from 'react'; | ||
import Account from './account'; | ||
import { Offcanvas, Button } from './offcanvas'; | ||
import { AccountOptions, getAccounts } from '../../services/accounts'; | ||
|
||
interface DisplayState { | ||
accounts: AccountOptions[]; | ||
} | ||
|
||
export default class Display extends Component<any, DisplayState> { | ||
id = 'displayOffcanvas'; | ||
|
||
constructor(props: any) { | ||
super(props); | ||
export default function Accounts() { | ||
const [accounts, setAccounts] = useState<AccountOptions[]>([]); | ||
const id = 'displayOffcanvas'; | ||
|
||
useEffect(() => { | ||
getAccounts() | ||
.then((resp) => resp.data.response) | ||
.then((accounts: AccountOptions[]) => this.setState({ accounts })); | ||
} | ||
.then((accounts: AccountOptions[]) => setAccounts(accounts)); | ||
}, []); | ||
|
||
private renderAccounts() { | ||
if (this.state == null) { | ||
return 'Loading...'; | ||
} else { | ||
return this.state.accounts.map((acc) => <AccountDisplay account={acc} />); | ||
} | ||
} | ||
|
||
private addAccount = (account: AccountOptions) => { | ||
this.setState({ accounts: [...this.state.accounts, account] }); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<Fragment> | ||
<div id="accounts"> | ||
<div id="list" className="overflow-auto p-1" style={{ maxHeight: 'calc(50vh - 60px)' }}> | ||
{this.renderAccounts()} | ||
</div> | ||
</div> | ||
<div className="d-flex mt-2 justify-content-center"> | ||
<Button id={this.id} message="Create a new account" /> | ||
<Offcanvas id={this.id} onFormEntry={this.addAccount} /> | ||
return ( | ||
<Fragment> | ||
<div id="accounts"> | ||
<div id="list" className="overflow-auto p-1" style={{ maxHeight: 'calc(50vh - 60px)' }}> | ||
{accounts.map((acc) => ( | ||
<Account account={acc} /> | ||
))} | ||
</div> | ||
</Fragment> | ||
); | ||
} | ||
</div> | ||
<div className="d-flex mt-2 justify-content-center"> | ||
<Button id={id} message="Create a new account" /> | ||
<Offcanvas id={id} onFormEntry={(acc: AccountOptions) => setAccounts([...accounts, acc])} /> | ||
</div> | ||
</Fragment> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// TODO: Fetch user profile photo correctly | ||
export function fetchSteamUserImage() { | ||
return 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/b5/b5bd56c1aa4644a474a2e4972be27ef9e82e517e_full.jpg'; // Default steam profile photo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,17 @@ | ||
import React, { Component, createRef } from 'react'; | ||
import io from 'socket.io-client'; | ||
import React, { useEffect, useState, Fragment } from 'react'; | ||
import Log from './log'; | ||
import socket from '../../services/socket'; | ||
|
||
interface LogsState { | ||
logs: JSX.Element[]; | ||
} | ||
|
||
export default class Logs extends Component<any, LogsState> { | ||
socket = io('ws://localhost:1228'); | ||
export default function Logs() { | ||
let [logs, setLogs] = useState<JSX.Element[]>([]); | ||
|
||
constructor(props: any) { | ||
super(props); | ||
this.state = { logs: [] }; | ||
this.openSocket(); | ||
} | ||
|
||
private openSocket() { | ||
this.socket.on('log', (msg: any) => { | ||
let { logs } = this.state; | ||
if (logs.length == 50) logs = logs.slice(1, 50); | ||
logs.push(createLog(msg)); | ||
this.setState({ logs }); | ||
useEffect(() => { | ||
socket.on('log', (log: any) => { | ||
if (logs.length >= 50) logs = logs.slice(1, 50); | ||
logs.push(<Log log={log} />); | ||
setLogs(logs); | ||
}); | ||
} | ||
|
||
render() { | ||
return <div className="logger">{this.state.logs}</div>; | ||
} | ||
} | ||
|
||
function createLog({ message, level, timestamp }: any) { | ||
return ( | ||
<div> | ||
<span className={`pe-3 text-${getColor(level)}`}>{new Date(timestamp).toLocaleTimeString()}</span> | ||
<span className="text-wrap text-muted mw-100">{message}</span> | ||
</div> | ||
); | ||
} | ||
}, []); | ||
|
||
function getColor(level: string) { | ||
switch (level) { | ||
case 'warn': | ||
return 'warning'; | ||
case 'info': | ||
return 'info'; | ||
case 'debug': | ||
return 'dark'; | ||
case 'error': | ||
return 'danger'; | ||
default: | ||
return 'secondary'; | ||
} | ||
return <Fragment>{logs}</Fragment>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react'; | ||
import { getColor } from './util'; | ||
|
||
export default function Log({ message, level, timestamp }: any) { | ||
return ( | ||
<div> | ||
<span className={`pe-3 text-${getColor(level)}`}>{new Date(timestamp).toLocaleTimeString()}</span> | ||
<span className="text-wrap text-muted mw-100">{message}</span> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export function getColor(level: string) { | ||
switch (level) { | ||
case 'warn': | ||
return 'warning'; | ||
case 'info': | ||
return 'info'; | ||
case 'debug': | ||
return 'dark'; | ||
case 'error': | ||
return 'danger'; | ||
default: | ||
return 'secondary'; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,17 @@ | ||
import React, { Component, createRef } from 'react'; | ||
import { Item as IItem } from './util'; | ||
import React, { useEffect, useState, Fragment } from 'react'; | ||
import Trade from './trade'; | ||
import io from 'socket.io-client'; | ||
import socket from '../../services/socket'; | ||
|
||
interface LogsState { | ||
trades: JSX.Element[]; | ||
} | ||
|
||
export default class Trades extends Component<any, LogsState> { | ||
socket = io('ws://localhost:1228'); | ||
|
||
constructor(props: any) { | ||
super(props); | ||
this.state = { trades: [] }; | ||
this.openSocket(); | ||
} | ||
export default function Trades() { | ||
let [trades, setTrades] = useState<JSX.Element[]>([]); | ||
|
||
private openSocket() { | ||
this.socket.on('trade', (trade: any) => { | ||
let { trades } = this.state; | ||
if (trades.length == 50) trades = trades.slice(1, 50); | ||
useEffect(() => { | ||
socket.on('trade', (trade: any) => { | ||
if (trades.length >= 50) trades = trades.slice(1, 50); | ||
trades.push(<Trade trade={trade} />); | ||
this.setState({ trades }); | ||
setTrades(trades); | ||
}); | ||
} | ||
}, []); | ||
|
||
render() { | ||
return this.state.trades; | ||
} | ||
return <Fragment>{trades}</Fragment>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
import React from 'react'; | ||
import React, { Fragment } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
import App from './app/app'; | ||
import Header from './header'; | ||
import Main from './main'; | ||
|
||
ReactDOM.render(<App />, document.getElementById('app')); | ||
const element = ( | ||
<Fragment> | ||
<Header /> | ||
<Main /> | ||
</Fragment> | ||
); | ||
|
||
ReactDOM.render(element, document.getElementById('app')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import io from 'socket.io-client'; | ||
|
||
const socket = io('ws://localhost:1228'); | ||
|
||
socket.on('connect_error', (err: any) => | ||
alert( | ||
`Oops! Occurred and error and we are disconnected from the app socket: ${err.message}. Make sure you started the all this app correctly.` | ||
) | ||
); | ||
|
||
export default socket; |