From 7bca5ecdc703a7df6f7385c2a3f88f36851ddd26 Mon Sep 17 00:00:00 2001 From: Fernando Porazzi Date: Fri, 15 Apr 2022 18:22:49 +0200 Subject: [PATCH] Extract getCookie function into its own file (#94) The getCookie function was being implemented a couple of times across the pages. These changes create a single implementation and imports the helper function whenever needed. --- frontend/src/components/BottomBar.js | 210 ++++++++++++------------- frontend/src/components/MakerPage.js | 143 ++++++++--------- frontend/src/components/OrderPage.js | 109 ++++++------- frontend/src/components/TradeBox.js | 87 +++++----- frontend/src/components/UserGenPage.js | 23 +-- frontend/src/utils/cookies.js | 15 ++ 6 files changed, 264 insertions(+), 323 deletions(-) create mode 100644 frontend/src/utils/cookies.js diff --git a/frontend/src/components/BottomBar.js b/frontend/src/components/BottomBar.js index 608eedb6..64f4bdb5 100644 --- a/frontend/src/components/BottomBar.js +++ b/frontend/src/components/BottomBar.js @@ -29,6 +29,8 @@ import EmojiEventsIcon from '@mui/icons-material/EmojiEvents'; import AmbossIcon from "./icons/AmbossIcon"; import FavoriteIcon from '@mui/icons-material/Favorite'; +import { getCookie } from "../utils/cookies"; + // pretty numbers function pn(x) { if(x == null){ @@ -40,22 +42,6 @@ function pn(x) { } } -function getCookie(name) { - let cookieValue = null; - if (document.cookie && document.cookie !== '') { - const cookies = document.cookie.split(';'); - for (let i = 0; i < cookies.length; i++) { - const cookie = cookies[i].trim(); - // Does this cookie string begin with the name we want? - if (cookie.substring(0, name.length + 1) === (name + '=')) { - cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); - break; - } - } - } - return cookieValue; - } - class BottomBar extends Component { constructor(props) { super(props); @@ -124,11 +110,11 @@ class BottomBar extends Component { - {this.state.network == 'testnet'? + {this.state.network == 'testnet'? - {this.state.node_id.slice(0, 12)+"... (1ML)"} @@ -137,7 +123,7 @@ class BottomBar extends Component { - {this.state.node_id.slice(0, 12)+"... (AMBOSS)"} @@ -157,7 +143,7 @@ class BottomBar extends Component { - {this.state.robosats_running_commit_hash.slice(0, 12)+"..."} @@ -216,7 +202,7 @@ class BottomBar extends Component {

{t("Support is only offered via public channels. Join our Telegram community if you have questions or want to hang out with other cool robots. Please, use our Github Issues if you find a bug or want to see new features!")}

- + @@ -247,7 +233,7 @@ class BottomBar extends Component { - @@ -270,7 +256,7 @@ class BottomBar extends Component { badInvoice:false, showRewardsSpinner: true, }); - + const requestOptions = { method: 'POST', headers: {'Content-Type':'application/json', 'X-CSRFToken': getCookie('csrftoken'),}, @@ -281,7 +267,7 @@ class BottomBar extends Component { fetch('/api/reward/', requestOptions) .then((response) => response.json()) .then((data) => console.log(data) & this.setState({ - badInvoice:data.bad_invoice, + badInvoice:data.bad_invoice, openClaimRewards: data.successful_withdrawal ? false : true, earned_rewards: data.successful_withdrawal ? 0 : this.state.earned_rewards, withdrawn: data.successful_withdrawal ? true : false, @@ -289,7 +275,7 @@ class BottomBar extends Component { })); } - getHost(){ + getHost(){ var url = (window.location != window.parent.location) ? this.getHost(document.referrer) : document.location.href; return url.split('/')[2] } @@ -310,7 +296,7 @@ class BottomBar extends Component { - {this.props.nickname ? + {this.props.nickname ?
{this.props.nickname} @@ -320,19 +306,19 @@ class BottomBar extends Component { - - {this.state.active_order_id ? + {this.state.active_order_id ? - + @@ -344,13 +330,13 @@ class BottomBar extends Component { } - + - {this.props.token ? + {this.props.token ? , }} /> - : + : t("Cannot remember")} - + this.setState({showRewards: !this.state.showRewards})}/>} + checked={this.state.showRewards} + onChange={()=> this.setState({showRewards: !this.state.showRewards})}/>} label={t("Rewards and compensations")} /> - +
@@ -403,7 +389,7 @@ class BottomBar extends Component { /> - + @@ -447,7 +433,7 @@ class BottomBar extends Component {
:""} - + {this.state.withdrawn?
{t("There it goes, thank you!🥇")} @@ -457,7 +443,7 @@ class BottomBar extends Component {
- + ) } @@ -473,7 +459,7 @@ bottomBarDesktop =()=>{ -
+
0 ? true: false} title={t("You can claim satoshis!")}> 0 & !this.state.profileShown & this.props.avatarLoaded) ? true: false} @@ -481,10 +467,10 @@ bottomBarDesktop =()=>{ 0 & !this.state.profileShown) ? "": null} color="primary"> this.props.setAppState({avatarLoaded: true}), - }} + }} src={this.props.nickname ? window.location.origin +'/static/assets/avatars/' + this.props.nickname + '.png' : null} /> @@ -501,10 +487,10 @@ bottomBarDesktop =()=>{ - @@ -514,10 +500,10 @@ bottomBarDesktop =()=>{ - @@ -527,10 +513,10 @@ bottomBarDesktop =()=>{ - @@ -540,10 +526,10 @@ bottomBarDesktop =()=>{ - @@ -553,10 +539,10 @@ bottomBarDesktop =()=>{ < IconButton onClick={this.handleClickOpenExchangeSummary}> - @@ -567,18 +553,18 @@ bottomBarDesktop =()=>{ - - + - @@ -598,13 +584,13 @@ bottomBarDesktop =()=>{ LangSelect = () => { const { i18n} = this.props; return( - ) } - + handleClickOpenExchangeSummary = () => { this.getInfo(); this.setState({openExchangeSummary: true}); @@ -638,10 +624,10 @@ bottomBarDesktop =()=>{ - @@ -650,10 +636,10 @@ bottomBarDesktop =()=>{ - @@ -662,10 +648,10 @@ bottomBarDesktop =()=>{ - @@ -674,10 +660,10 @@ bottomBarDesktop =()=>{ - @@ -686,10 +672,10 @@ bottomBarDesktop =()=>{ - @@ -700,17 +686,17 @@ bottomBarDesktop =()=>{ - {(this.state.maker_fee*100).toFixed(3)}% - {(this.state.taker_fee*100).toFixed(3)}% @@ -718,7 +704,7 @@ bottomBarDesktop =()=>{ - + ) @@ -742,12 +728,12 @@ bottomBarPhone =()=>{ 0 & !this.state.profileShown) ? "": null} color="primary"> this.props.setAppState({avatarLoaded: true}), - }} - src={this.props.nickname ? window.location.origin +'/static/assets/avatars/' + this.props.nickname + '.png' : null} + }} + src={this.props.nickname ? window.location.origin +'/static/assets/avatars/' + this.props.nickname + '.png' : null} /> @@ -757,13 +743,13 @@ bottomBarPhone =()=>{ - + - + @@ -787,7 +773,7 @@ bottomBarPhone =()=>{ - + @@ -802,9 +788,9 @@ bottomBarPhone =()=>{ - @@ -812,8 +798,8 @@ bottomBarPhone =()=>{ - @@ -840,4 +826,4 @@ bottomBarPhone =()=>{ ) } } -export default withTranslation()(BottomBar); \ No newline at end of file +export default withTranslation()(BottomBar); diff --git a/frontend/src/components/MakerPage.js b/frontend/src/components/MakerPage.js index 4d077745..46f45ffe 100644 --- a/frontend/src/components/MakerPage.js +++ b/frontend/src/components/MakerPage.js @@ -14,22 +14,7 @@ import LockIcon from '@mui/icons-material/Lock'; import HourglassTopIcon from '@mui/icons-material/HourglassTop'; import currencyDict from '../../static/assets/currencies.json'; -function getCookie(name) { - let cookieValue = null; - if (document.cookie && document.cookie !== '') { - const cookies = document.cookie.split(';'); - for (let i = 0; i < cookies.length; i++) { - const cookie = cookies[i].trim(); - // Does this cookie string begin with the name we want? - if (cookie.substring(0, name.length + 1) === (name + '=')) { - cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); - break; - } - } - } - return cookieValue; -} -const csrftoken = getCookie('csrftoken'); +import { getCookie } from "../utils/cookies"; // pretty numbers function pn(x) { @@ -38,7 +23,7 @@ function pn(x) { } var parts = x.toString().split("."); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); - return parts.join("."); + return parts.join("."); } class MakerPage extends Component { @@ -55,7 +40,7 @@ class MakerPage extends Component { constructor(props) { super(props); this.state={ - is_explicit: false, + is_explicit: false, type: 0, currency: this.defaultCurrency, currencyCode: this.defaultCurrencyCode, @@ -80,9 +65,9 @@ class MakerPage extends Component { fetch('/api/limits/') .then((response) => response.json()) .then((data) => this.setState({ - limits:data, + limits:data, loadingLimits:false, - minAmount: this.state.amount ? parseFloat((this.state.amount/2).toPrecision(2)) : parseFloat(Number(data[this.state.currency]['max_amount']*0.25).toPrecision(2)), + minAmount: this.state.amount ? parseFloat((this.state.amount/2).toPrecision(2)) : parseFloat(Number(data[this.state.currency]['max_amount']*0.25).toPrecision(2)), maxAmount: this.state.amount ? this.state.amount : parseFloat(Number(data[this.state.currency]['max_amount']*0.75).toPrecision(2)), })); } @@ -96,7 +81,7 @@ class MakerPage extends Component { handleTypeChange=(e)=>{ this.setState({ - type: e.target.value, + type: e.target.value, }); } handleCurrencyChange=(e)=>{ @@ -106,24 +91,24 @@ class MakerPage extends Component { }); if(this.state.enableAmountRange){ this.setState({ - minAmount: parseFloat(Number(this.state.limits[e.target.value]['max_amount']*0.25).toPrecision(2)), + minAmount: parseFloat(Number(this.state.limits[e.target.value]['max_amount']*0.25).toPrecision(2)), maxAmount: parseFloat(Number(this.state.limits[e.target.value]['max_amount']*0.75).toPrecision(2)), }) } } handleAmountChange=(e)=>{ this.setState({ - amount: e.target.value, + amount: e.target.value, }); } handleMinAmountChange=(e)=>{ this.setState({ - minAmount: parseFloat(Number(e.target.value).toPrecision(e.target.value < 100 ? 2 : 3)), + minAmount: parseFloat(Number(e.target.value).toPrecision(e.target.value < 100 ? 2 : 3)), }); } handleMaxAmountChange=(e)=>{ this.setState({ - maxAmount: parseFloat(Number(e.target.value).toPrecision(e.target.value < 100 ? 2 : 3)), + maxAmount: parseFloat(Number(e.target.value).toPrecision(e.target.value < 100 ? 2 : 3)), }); } @@ -155,7 +140,7 @@ class MakerPage extends Component { lowerValue = upperValue/maxRange } } - + this.setState({ minAmount: parseFloat(Number(lowerValue).toPrecision(lowerValue < 100 ? 2 : 3)), maxAmount: parseFloat(Number(upperValue).toPrecision(upperValue < 100 ? 2 : 3)), @@ -165,12 +150,12 @@ class MakerPage extends Component { handlePaymentMethodChange=(value)=>{ if (value.length > 50){ this.setState({ - badPaymentMethod: true, + badPaymentMethod: true, }); }else{ this.setState({ payment_method: value.substring(0,53), - badPaymentMethod: value.length > 50, + badPaymentMethod: value.length > 50, }); } } @@ -188,7 +173,7 @@ class MakerPage extends Component { this.setState({ premium: e.target.value, - badPremium: bad_premium, + badPremium: bad_premium, }); } @@ -203,20 +188,20 @@ class MakerPage extends Component { this.setState({ satoshis: e.target.value, - badSatoshis: bad_sats, + badSatoshis: bad_sats, }); } handleClickRelative=(e)=>{ this.setState({ - is_explicit: false, + is_explicit: false, }); this.handlePremiumChange(); } - + handleClickExplicit=(e)=>{ if(!this.state.enableAmountRange){ this.setState({ - is_explicit: true, + is_explicit: true, }); this.handleSatoshisChange(); } @@ -268,14 +253,14 @@ class MakerPage extends Component { {t("Buy or Sell Bitcoin?")} - } label={t("Buy")} labelPlacement="Top" /> - } label={t("Sell")} labelPlacement="Top" @@ -284,21 +269,21 @@ class MakerPage extends Component {
- +