1
0
mirror of https://github.com/kremalicious/blowfish.git synced 2024-12-28 23:57:52 +01:00

make prices a Map to preserve order

This commit is contained in:
Matthias Kretschmann 2019-05-25 02:33:54 +02:00
parent 7dcea78756
commit 196c27f5e3
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 44 additions and 37 deletions

View File

@ -11,8 +11,10 @@ export default class Ticker extends PureComponent {
static contextType = AppContext static contextType = AppContext
items = activeStyle => items = activeStyle =>
Object.keys(this.context.prices).map((key, i) => ( // convert Map to array first, cause for...of or forEach returns undefined,
<Item key={i} className="number-unit"> // so it cannot be mapped to a collection of elements
[...this.context.prices.entries()].map(([key, value]) => (
<Item key={key} className="number-unit">
<button <button
className="label label--price" className="label label--price"
onClick={() => this.context.toggleCurrencies(key)} onClick={() => this.context.toggleCurrencies(key)}
@ -23,7 +25,7 @@ export default class Ticker extends PureComponent {
: {} : {}
} }
> >
{cryptoFormatter(this.context.prices[key], key)} {cryptoFormatter(value, key)}
</button> </button>
</Item> </Item>
)) ))

View File

@ -1,7 +1,7 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import { AppContext } from '../store/createContext' import { AppContext } from '../store/createContext'
import Balance from './Balance' import Balance from './Balance'
import { prices } from '../../config' import { conversions } from '../../config'
const calculateTotalBalance = (accounts, currency) => { const calculateTotalBalance = (accounts, currency) => {
const balanceTotalArray = [] const balanceTotalArray = []
@ -23,15 +23,15 @@ export default class Total extends PureComponent {
static contextType = AppContext static contextType = AppContext
render() { render() {
const conversions = Object.assign( const conversionsBalance = Object.assign(
...prices.map(key => ({ ...conversions.map(key => ({
[key]: calculateTotalBalance(this.context.accounts, key) [key]: calculateTotalBalance(this.context.accounts, key)
})) }))
) )
const balanceNew = { const balanceNew = {
ocean: calculateTotalBalance(this.context.accounts, 'ocean'), ocean: calculateTotalBalance(this.context.accounts, 'ocean'),
...conversions ...conversionsBalance
} }
return ( return (

View File

@ -5,7 +5,13 @@ import { ipcRenderer } from 'electron'
import Store from 'electron-store' import Store from 'electron-store'
import { AppContext } from './createContext' import { AppContext } from './createContext'
import fetchData from '../utils/fetch' import fetchData from '../utils/fetch'
import { refreshInterval, prices, oceanTokenContract } from '../../config' import { refreshInterval, conversions, oceanTokenContract } from '../../config'
// construct initial prices Map to get consistent
// order for Ticker and Touchbar
let pricesMap = new Map()
pricesMap.set('ocean', 1)
conversions.map(key => pricesMap.set(key, 0))
export default class AppProvider extends PureComponent { export default class AppProvider extends PureComponent {
static propTypes = { static propTypes = {
@ -19,7 +25,7 @@ export default class AppProvider extends PureComponent {
accounts: [], accounts: [],
currency: 'ocean', currency: 'ocean',
needsConfig: false, needsConfig: false,
prices: Object.assign(...prices.map(key => ({ [key]: 0 }))), prices: pricesMap,
toggleCurrencies: currency => this.toggleCurrencies(currency), toggleCurrencies: currency => this.toggleCurrencies(currency),
setBalances: () => this.setBalances(), setBalances: () => this.setBalances(),
accentColor: '' accentColor: ''
@ -74,20 +80,17 @@ export default class AppProvider extends PureComponent {
} }
fetchAndSetPrices = async () => { fetchAndSetPrices = async () => {
const currencies = prices.join(',') const currencies = conversions.join(',')
const json = await fetchData( const json = await fetchData(
`https://api.coingecko.com/api/v3/simple/price?ids=ocean-protocol&vs_currencies=${currencies}` `https://api.coingecko.com/api/v3/simple/price?ids=ocean-protocol&vs_currencies=${currencies}`
) )
const newPrizes = Object.assign( let newPrices = new Map(this.state.prices) // make a shallow copy of the Map
...prices.map(key => ({ conversions.map(key => newPrices.set(key, json['ocean-protocol'][key])) // modify the copy
ocean: 1,
[key]: json['ocean-protocol'][key]
}))
)
ipcRenderer.send('prices-updated', newPrizes) ipcRenderer.send('prices-updated', Array.from(newPrices)) // convert Map to array, ipc messages seem to kill it
return newPrizes this.setState({ prices: newPrices })
return newPrices
} }
setBalances = async () => { setBalances = async () => {
@ -98,9 +101,9 @@ export default class AppProvider extends PureComponent {
for (const account of accountsPref) { for (const account of accountsPref) {
const oceanBalance = await this.getBalance(account) const oceanBalance = await this.getBalance(account)
const conversions = Object.assign( const conversionsBalance = Object.assign(
...prices.map(key => ({ ...conversions.map(key => ({
[key]: oceanBalance * this.state.prices[key] || 0 [key]: oceanBalance * this.state.prices.get(key) || 0
})) }))
) )
@ -108,7 +111,7 @@ export default class AppProvider extends PureComponent {
address: account, address: account,
balance: { balance: {
ocean: oceanBalance, ocean: oceanBalance,
...conversions ...conversionsBalance
} }
} }
@ -121,7 +124,8 @@ export default class AppProvider extends PureComponent {
} }
toggleCurrencies(currency) { toggleCurrencies(currency) {
ipcRenderer.send('currency-updated', this.state.prices, currency) const pricesNew = Array.from(this.state.prices)
ipcRenderer.send('currency-updated', pricesNew, currency)
this.setState({ currency }) this.setState({ currency })
} }

View File

@ -1,5 +1,5 @@
module.exports = { module.exports = {
prices: ['eur', 'usd', 'btc', 'eth'], conversions: ['eur', 'usd', 'btc', 'eth'],
refreshInterval: '1m', refreshInterval: '1m',
oceanTokenContract: '0x985dd3D42De1e256d09e1c10F112bCCB8015AD41' oceanTokenContract: '0x985dd3D42De1e256d09e1c10F112bCCB8015AD41'
} }

View File

@ -4,7 +4,6 @@ const pkg = require('../package.json')
const buildMenu = require('./menu') const buildMenu = require('./menu')
const { buildTouchbar, updateTouchbar } = require('./touchbar') const { buildTouchbar, updateTouchbar } = require('./touchbar')
const { rgbaToHex } = require('./utils') const { rgbaToHex } = require('./utils')
const { prices } = require('./config')
let mainWindow let mainWindow
@ -65,7 +64,7 @@ const createWindow = async () => {
// Load touchbar // Load touchbar
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
const accentColor = getAccentColor() const accentColor = getAccentColor()
buildTouchbar(prices, mainWindow, accentColor) buildTouchbar(mainWindow, accentColor)
ipcMain.on('prices-updated', (event, pricesNew) => { ipcMain.on('prices-updated', (event, pricesNew) => {
updateTouchbar(pricesNew, mainWindow, accentColor) updateTouchbar(pricesNew, mainWindow, accentColor)

View File

@ -1,5 +1,6 @@
const { TouchBar } = require('electron') const { TouchBar } = require('electron')
const { cryptoFormatter } = require('./utils') const { cryptoFormatter } = require('./utils')
const { conversions } = require('./config')
const { TouchBarButton } = TouchBar const { TouchBarButton } = TouchBar
@ -16,11 +17,11 @@ const createButton = (
backgroundColor: key === currentCurrency ? accentColor : '#141414' backgroundColor: key === currentCurrency ? accentColor : '#141414'
}) })
const buildTouchbar = (prices, mainWindow, accentColor) => { const buildTouchbar = (mainWindow, accentColor) => {
const touchBar = new TouchBar({ const touchBar = new TouchBar({
items: [ items: [
createButton(1, 'ocean', mainWindow, accentColor), createButton(1, 'ocean', mainWindow, accentColor),
...prices.map(key => createButton(0, key, mainWindow, accentColor)) ...conversions.map(key => createButton(0, key, mainWindow, accentColor))
] ]
}) })
@ -28,22 +29,23 @@ const buildTouchbar = (prices, mainWindow, accentColor) => {
} }
const updateTouchbar = ( const updateTouchbar = (
prices, pricesNew,
mainWindow, mainWindow,
accentColor, accentColor,
currentCurrency = 'ocean' currentCurrency = 'ocean'
) => { ) => {
const items = Object.keys(prices) const items = pricesNew.map(item => {
.filter(key => key !== 'ocean') return createButton(
.map(key => item[1],
createButton(prices[key], key, mainWindow, accentColor, currentCurrency) item[0],
mainWindow,
accentColor,
currentCurrency
) )
})
const touchBar = new TouchBar({ const touchBar = new TouchBar({
items: [ items: [...items]
createButton(1, 'ocean', mainWindow, accentColor, currentCurrency),
...items
]
}) })
mainWindow.setTouchBar(touchBar) mainWindow.setTouchBar(touchBar)