|
| 1 | +import React, {Component} from 'react'; |
| 2 | +import axios from 'axios'; |
| 3 | + |
| 4 | +class ExchangeRates extends Component { |
| 5 | + constructor(props) { |
| 6 | + super(props); |
| 7 | + this.state = { |
| 8 | + rates: [], |
| 9 | + loading: true, |
| 10 | + errorMessage: '', |
| 11 | + userDate: '', |
| 12 | + latestDate: '' |
| 13 | + }; |
| 14 | + } |
| 15 | + |
| 16 | + getBaseUrl() { |
| 17 | + return 'http://telemedi-zadanie.localhost'; |
| 18 | + } |
| 19 | + |
| 20 | + componentDidMount() { |
| 21 | + this.fetchData(); |
| 22 | + } |
| 23 | + |
| 24 | + getTodaysDate = () => { |
| 25 | + return (new Date()).toISOString().split('T')[0]; |
| 26 | + }; |
| 27 | + |
| 28 | + adjustDate(date) { |
| 29 | + const inputDate = new Date(date); |
| 30 | + const currentHour = new Date().getHours(); |
| 31 | + if (currentHour < 12) { |
| 32 | + inputDate.setDate(inputDate.getDate() - 1); |
| 33 | + } |
| 34 | + |
| 35 | + const year = inputDate.getFullYear(); |
| 36 | + const month = String(inputDate.getMonth() + 1).padStart(2, '0'); |
| 37 | + const day = String(inputDate.getDate()).padStart(2, '0'); |
| 38 | + |
| 39 | + return `${year}-${month}-${day}`; |
| 40 | + } |
| 41 | + |
| 42 | + getQueryParamDate(param) { |
| 43 | + const searchParams = new URLSearchParams(this.props.location.search); |
| 44 | + |
| 45 | + return searchParams.get(param); |
| 46 | + } |
| 47 | + |
| 48 | + handleDateChange = (event) => { |
| 49 | + const selectedDate = event.target.value; |
| 50 | + this.setState({userDate: selectedDate}, () => { |
| 51 | + this.props.history.push({ |
| 52 | + pathname: '/exchange-rates', |
| 53 | + search: `?date=${selectedDate}`, |
| 54 | + }); |
| 55 | + this.fetchData(); |
| 56 | + }); |
| 57 | + }; |
| 58 | + |
| 59 | + fetchData() { |
| 60 | + const todaysDate = this.getTodaysDate(); |
| 61 | + const ratesDate = this.adjustDate(this.state.userDate || this.getQueryParamDate('date') || todaysDate); |
| 62 | + |
| 63 | + axios.get(`${this.getBaseUrl()}/api/exchange-rates?userDate=${ratesDate}&latestDate=${this.adjustDate(todaysDate)}`) |
| 64 | + .then(response => { |
| 65 | + if (200 === response.status) { |
| 66 | + this.setState({ |
| 67 | + rates: response.data.rates, |
| 68 | + userDate: response.data.userDate, |
| 69 | + latestDate: response.data.latestDate |
| 70 | + }); |
| 71 | + } else { |
| 72 | + this.setState({userDate: ratesDate, errorMessage: 'Something went wrong.'}); |
| 73 | + } |
| 74 | + }) |
| 75 | + .catch(error => { |
| 76 | + if (error.response.headers && error.response.headers.has('X-Validation-Errors')) { |
| 77 | + this.setState({ |
| 78 | + loading: false, |
| 79 | + errorMessage: `Fix following errors and try again: ${error.response.headers.get('X-Validation-Errors')}` |
| 80 | + }); |
| 81 | + } else { |
| 82 | + this.setState({userDate: ratesDate, errorMessage: 'Something went wrong.'}); |
| 83 | + } |
| 84 | + }) |
| 85 | + .finally(() => { |
| 86 | + this.setState({loading: false}); |
| 87 | + }) |
| 88 | + } |
| 89 | + |
| 90 | + renderTableRows() { |
| 91 | + return this.state.rates.map((rate, index) => ( |
| 92 | + <tr key={index}> |
| 93 | + <td><b>{rate.currencyCode}</b> ({rate.currencyName})</td> |
| 94 | + <td className="highlighted-rate">{rate.userDateBidRate ?? 'N/A'}</td> |
| 95 | + <td className="highlighted-rate">{rate.userDateAskRate ?? 'N/A'}</td> |
| 96 | + <td className="highlighted-rate">{rate.userDateNbpRate ?? 'N/A'}</td> |
| 97 | + <td>{rate.latestBidRate ?? 'N/A'}</td> |
| 98 | + <td>{rate.latestAskRate ?? 'N/A'}</td> |
| 99 | + <td>{rate.latestNbpRate ?? 'N/A'}</td> |
| 100 | + </tr> |
| 101 | + )); |
| 102 | + } |
| 103 | + |
| 104 | + render() { |
| 105 | + const {rates, loading, userDate, latestDate, errorMessage} = this.state; |
| 106 | + |
| 107 | + return ( |
| 108 | + <div className="container mt-5"> |
| 109 | + <h2 className="text-center">Exchange Rates @ Telemedi by Adam Guła</h2> |
| 110 | + {loading ? ( |
| 111 | + <div className="text-center"> |
| 112 | + <span className="fa fa-spin fa-spinner fa-4x"></span> |
| 113 | + </div> |
| 114 | + ) : ( |
| 115 | + <> |
| 116 | + <div className="text-center mb-4 date-picker-container"> |
| 117 | + <label htmlFor="date-picker">Selected date</label> |
| 118 | + <input |
| 119 | + id="date-picker" |
| 120 | + type="date" |
| 121 | + value={userDate} |
| 122 | + min="2023-01-01" |
| 123 | + max={this.getTodaysDate()} |
| 124 | + onChange={this.handleDateChange} |
| 125 | + /> |
| 126 | + </div> |
| 127 | + <table className="table table-bordered"> |
| 128 | + <thead> |
| 129 | + <tr> |
| 130 | + <th rowSpan="1"></th> |
| 131 | + <th colSpan="3" className="text-center highlighted-rate">Rates for selected date |
| 132 | + ({userDate}) |
| 133 | + </th> |
| 134 | + <th colSpan="3" className="text-center">Latest rates ({latestDate})</th> |
| 135 | + </tr> |
| 136 | + <tr> |
| 137 | + <th>Currency</th> |
| 138 | + <th className="highlighted-rate">Bid</th> |
| 139 | + <th className="highlighted-rate">Ask</th> |
| 140 | + <th className="highlighted-rate">NBP</th> |
| 141 | + <th>Bid</th> |
| 142 | + <th>Ask</th> |
| 143 | + <th>NBP</th> |
| 144 | + </tr> |
| 145 | + </thead> |
| 146 | + <tbody> |
| 147 | + {rates.length > 0 ? this.renderTableRows() : ( |
| 148 | + <tr> |
| 149 | + {errorMessage.length > 0 ? ( |
| 150 | + <td colSpan="6" className="text-center error-message">{errorMessage}</td>) : ( |
| 151 | + <td colSpan="6" className="text-center">No data available</td>)} |
| 152 | + </tr> |
| 153 | + )} |
| 154 | + |
| 155 | + </tbody> |
| 156 | + </table> |
| 157 | + </> |
| 158 | + )} |
| 159 | + </div> |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +export default ExchangeRates; |
0 commit comments