From ffd7f76669657680b8213686def87e395d3196d2 Mon Sep 17 00:00:00 2001 From: Evgeny Sokovikov Date: Mon, 23 Mar 2020 19:11:46 +0400 Subject: [PATCH] rewrite currency screen usign funcional components --- screen/settings/currency.js | 129 +++++++++++++++++------------------- 1 file changed, 62 insertions(+), 67 deletions(-) diff --git a/screen/settings/currency.js b/screen/settings/currency.js index c511b196..0bb55ae8 100644 --- a/screen/settings/currency.js +++ b/screen/settings/currency.js @@ -1,4 +1,4 @@ -import React, { Component } from 'react'; +import React, { useState, useEffect } from 'react'; import { FlatList, TouchableOpacity, ActivityIndicator, View } from 'react-native'; import { SafeBlueArea, BlueNavigationStyle, BlueListItem, BlueText, BlueCard } from '../../BlueComponents'; import PropTypes from 'prop-types'; @@ -7,80 +7,69 @@ import { FiatUnit } from '../../models/fiatUnit'; let loc = require('../../loc'); let currency = require('../../currency'); -export default class Currency extends Component { - static navigationOptions = () => ({ - ...BlueNavigationStyle(), - title: loc.settings.currency, - }); +const data = Object.values(FiatUnit); - constructor(props) { - super(props); - this.state = { data: Object.values(FiatUnit), isSavingNewPreferredCurrency: false }; - } +const Currency = () => { + const [isSavingNewPreferredCurrency, setIsSavingNewPreferredCurrency] = useState(false); + const [selectedCurrency, setSelectedCurrency] = useState(null); - async componentDidMount() { - try { - const preferredCurrency = await currency.getPreferredCurrency(); - if (preferredCurrency === null) { - throw Error(); + useEffect(() => { + const fetchCurrency = async () => { + try { + const preferredCurrency = await currency.getPreferredCurrency(); + if (preferredCurrency === null) { + throw Error(); + } + setSelectedCurrency(preferredCurrency); + } catch (_error) { + setSelectedCurrency(FiatUnit.USD); } - this.setState({ selectedCurrency: preferredCurrency }); - } catch (_error) { - this.setState({ selectedCurrency: FiatUnit.USD }); - } - } + }; + fetchCurrency(); + }, []); - renderItem = ({ item }) => { + if (selectedCurrency !== null && selectedCurrency !== undefined) { return ( - { - this.setState({ isSavingNewPreferredCurrency: true, selectedCurrency: item }, async () => { - await currency.setPrefferedCurrency(item); - await currency.startUpdater(); - this.setState({ isSavingNewPreferredCurrency: false }); - }); - }} - > - - ) : ( - - ), - } - : { hideChevron: true })} + + `${index}`} + data={data} + extraData={data} + renderItem={({ item }) => { + return ( + { + setIsSavingNewPreferredCurrency(true); + setSelectedCurrency(item); + await currency.setPrefferedCurrency(item); + await currency.startUpdater(); + setIsSavingNewPreferredCurrency(false); + }} + > + } + : { hideChevron: true })} + /> + + ); + }} /> - - ); - }; - - render() { - if (this.state.selectedCurrency !== null && this.state.selectedCurrency !== undefined) { - return ( - - `${index}`} - data={this.state.data} - extraData={this.state.data} - renderItem={this.renderItem} - /> - - Prices are obtained from CoinDesk - - - ); - } - return ( - - - + + Prices are obtained from CoinDesk + + ); } -} + return ( + + + + ); +}; Currency.propTypes = { navigation: PropTypes.shape({ @@ -88,3 +77,9 @@ Currency.propTypes = { goBack: PropTypes.func, }), }; + +Currency.navigationOptions = () => ({ + ...BlueNavigationStyle(), + title: loc.settings.currency, +}); +export default Currency;