-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implementation of crypto currency calculator
- Loading branch information
Showing
19 changed files
with
1,067 additions
and
76 deletions.
There are no files selected for viewing
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
Binary file not shown.
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 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
Empty file.
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,2 @@ | ||
export const API_BASE_PATH = "https://api.coinlore.com/api"; | ||
export const CURRENCY_LIST_URL = "tickers/"; |
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,31 @@ | ||
import axios from "axios"; | ||
import {API_BASE_PATH} from '../config/service'; | ||
export const WEB_REQUEST_TYPE = { POST: "POST", GET: "GET", DELETE: "DELETE", PUT: "PUT" }; | ||
/** | ||
* Method for handling all api call to the server | ||
* @param method | ||
* @param url | ||
* @param params | ||
*/ | ||
axios.interceptors.response.use((response) => { | ||
return response; | ||
}, error => { | ||
return Promise.reject(error.response); | ||
}); | ||
|
||
export const getApiService = ( method, url) => { | ||
return axios({ | ||
method: method, | ||
url: url, | ||
baseURL: API_BASE_PATH, | ||
headers: { | ||
"Content-Type": "application/json" | ||
} | ||
}) | ||
.then(response => { | ||
return response; | ||
}) | ||
.catch(error => { | ||
throw JSON.stringify(error); | ||
}); | ||
}; |
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 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const GET_CURRENCY_LIST_REQUEST = "GET_CURRENCY_LIST_REQUEST"; | ||
export const GET_CURRENCY_LIST_SUCCESS = "GET_CURRENCY_LIST_SUCCESS"; | ||
export const GET_CURRENCY_LIST_FAILURE = "GET_CURRENCY_LIST_FAILURE"; |
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,27 @@ | ||
import { | ||
GET_CURRENCY_LIST_REQUEST, | ||
GET_CURRENCY_LIST_SUCCESS, | ||
GET_CURRENCY_LIST_FAILURE | ||
} from "../actionTypes/currencies"; | ||
|
||
import { getAllCurrencies } from "../services/currencies"; | ||
|
||
export const getCurrencies = () => dispatch => { | ||
const params = "start=0&limit=30"; | ||
dispatch({ | ||
type: GET_CURRENCY_LIST_REQUEST | ||
}); | ||
getAllCurrencies(params) | ||
.then(data => { | ||
dispatch({ | ||
type: GET_CURRENCY_LIST_SUCCESS, | ||
payload: data.data | ||
}); | ||
}) | ||
.catch(err => { | ||
dispatch({ | ||
type: GET_CURRENCY_LIST_FAILURE, | ||
payload: err | ||
}); | ||
}); | ||
}; |
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,67 @@ | ||
import React, { useEffect, useState, useRef } from "react"; | ||
import { Badge, FormGroup, Label, Input, InputGroup } from "reactstrap"; | ||
|
||
function Calculator(props) { | ||
|
||
const [count, setCount] = useState(); | ||
const [price, setPrice] = useState(); | ||
|
||
const {currency} = props; | ||
|
||
const handleCountChange = e => { | ||
setCount(e.target.value); | ||
const price = (e.target.value * currency.priceUSD).toFixed(2); | ||
setPrice(price); | ||
} | ||
|
||
const handlePriceChange = (e) => { | ||
setPrice(e.target.value); | ||
const count = (e.target.value / currency.priceUSD).toFixed(2); | ||
setCount(count); | ||
} | ||
|
||
const ref = useRef(); | ||
|
||
useEffect(() => { | ||
setPrice((ref.current.props.value * currency.priceUSD).toFixed(2)); | ||
}, [currency]); | ||
|
||
return ( | ||
<React.Fragment> | ||
<h5 className="mt-5">{currency.symbol} to USD Converter</h5> | ||
<FormGroup className="mt-4"> | ||
<Label for="coin-count-input">{currency.symbol}</Label> | ||
<Badge className="ml-2" color="info"> | ||
PRICE - ${currency.priceUSD} | ||
</Badge> | ||
<InputGroup> | ||
<Input | ||
ref={ref} | ||
value={count} | ||
onChange={handleCountChange} | ||
type="number" | ||
name="count" | ||
id="coin-count-input" | ||
placeholder="Amount" | ||
/> | ||
</InputGroup> | ||
</FormGroup> | ||
<FormGroup className="mt-4"> | ||
<Label for="usd-input">USD</Label> | ||
<InputGroup> | ||
<Input | ||
value={price} | ||
onChange={handlePriceChange} | ||
type="number" | ||
name="usd" | ||
id="usd-input" | ||
placeholder="Price" | ||
/> | ||
</InputGroup> | ||
</FormGroup> | ||
</React.Fragment> | ||
); | ||
|
||
} | ||
|
||
export default Calculator; |
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,7 @@ | ||
.SelectBox { | ||
width: 20em; | ||
} | ||
|
||
.Container { | ||
padding: 5em 10em; | ||
} |
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,89 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { connect } from "react-redux"; | ||
import { getCurrencies } from "../actions/currencies"; | ||
import { Row, Col, Badge } from "reactstrap"; | ||
import Select from "react-select"; | ||
import Calculator from "./Calculator"; | ||
import s from "./Calculator.module.scss"; | ||
|
||
function Currencies(props) { | ||
const { currencyList, isFetchingCurrencies } = props; | ||
|
||
const currencies = currencyList.map(currency => { | ||
return { | ||
value: currency.id, | ||
label: currency.name, | ||
priceUSD: currency.price_usd, | ||
currentChange24Hours: currency.percent_change_24h, | ||
symbol: currency.symbol | ||
}; | ||
}); | ||
|
||
const [currency, setCurrency] = useState(0); | ||
|
||
useEffect(() => { | ||
if (!currencyList.length) { | ||
props.getCurrencies(); | ||
} | ||
}); | ||
|
||
function handleChange(selectedOption) { | ||
setCurrency(selectedOption); | ||
} | ||
|
||
return ( | ||
<div className={s.Container}> | ||
<h3 className="mt-2"> Crypto Currency Calculator</h3> | ||
<Row className="mt-3"> | ||
<Col xl={4}> | ||
<Select | ||
className={s.SelectBox} | ||
value={currency} | ||
onChange={handleChange} | ||
isDisabled={isFetchingCurrencies} | ||
options={currencies} | ||
placeholder={ | ||
isFetchingCurrencies ? "Loading, please wait" : "Select Currency" | ||
} | ||
/> | ||
</Col> | ||
<Col> | ||
{currency.value && ( | ||
<div> | ||
<span className="mr-1">Price</span> | ||
<Badge color="primary" pill> | ||
${currency.priceUSD} | ||
</Badge> | ||
<span className="ml-4">Current Change(24 Hours)</span> | ||
<Badge className="ml-1" color="primary" pill> | ||
{currency.currentChange24Hours} % | ||
</Badge> | ||
</div> | ||
)} | ||
</Col> | ||
</Row> | ||
{currency.value && ( | ||
<Row> | ||
<Col xl={4}> | ||
<Calculator currency={currency} /> | ||
</Col> | ||
</Row> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
const mapStateToProps = ({ currencies }) => { | ||
const { isFetchingCurrencies, currencyList, errorMessage } = currencies; | ||
|
||
return { | ||
isFetchingCurrencies, | ||
currencyList, | ||
errorMessage | ||
}; | ||
}; | ||
|
||
export default connect( | ||
mapStateToProps, | ||
{ getCurrencies } | ||
)(Currencies); |
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,37 @@ | ||
import { | ||
GET_CURRENCY_LIST_REQUEST, | ||
GET_CURRENCY_LIST_SUCCESS, | ||
GET_CURRENCY_LIST_FAILURE | ||
} from "../actionTypes/currencies"; | ||
|
||
const initialState = { | ||
isFetchingCurrencies: false, | ||
currencyList: [], | ||
errorMessage: "" | ||
}; | ||
|
||
const currencies = (state = initialState, action) => { | ||
switch (action.type) { | ||
case GET_CURRENCY_LIST_REQUEST: | ||
return { | ||
...state, | ||
isFetchingCurrencies: true | ||
}; | ||
case GET_CURRENCY_LIST_SUCCESS: | ||
return { | ||
...state, | ||
isFetchingCurrencies: false, | ||
currencyList: action.payload | ||
}; | ||
case GET_CURRENCY_LIST_FAILURE: | ||
return { | ||
...state, | ||
isFetchingCurrencies: false, | ||
errorMessage: action.payload | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default currencies; |
Oops, something went wrong.