backend and BookPage.js use currencies.json

This commit is contained in:
fieryfrank 2022-01-07 18:48:23 -05:00
parent 5640b11e6f
commit 4516f1974a
6 changed files with 39 additions and 13 deletions

6
api/currencies.json Normal file
View File

@ -0,0 +1,6 @@
{
"1":"USD",
"2":"EUR",
"3":"ETH",
"4":"ABC"
}

View File

@ -7,6 +7,7 @@ from django.dispatch import receiver
from django.utils.html import mark_safe
from pathlib import Path
import json
#############################
# TODO
@ -65,11 +66,6 @@ class Order(models.Model):
BUY = 0, 'BUY'
SELL = 1, 'SELL'
class Currencies(models.IntegerChoices):
USD = 1, 'USD'
EUR = 2, 'EUR'
ETH = 3, 'ETH'
class Status(models.IntegerChoices):
WFB = 0, 'Waiting for bond'
PUB = 1, 'Published in order book'
@ -92,6 +88,9 @@ class Order(models.Model):
TLD = 18, 'Taker lost dispute'
EXP = 19, 'Expired'
currency_dict = json.load(open('./api/currencies.json'))
currency_choices = [(int(val), label) for val, label in list(currency_dict.items())]
print(currency_choices)
# order info
status = models.PositiveSmallIntegerField(choices=Status.choices, null=False, default=Status.WFB)
created_at = models.DateTimeField(auto_now_add=True)
@ -99,7 +98,7 @@ class Order(models.Model):
# order details
type = models.PositiveSmallIntegerField(choices=Types.choices, null=False)
currency = models.PositiveSmallIntegerField(choices=Currencies.choices, null=False)
currency = models.PositiveSmallIntegerField(choices=currency_choices, null=False)
amount = models.DecimalField(max_digits=9, decimal_places=4, validators=[MinValueValidator(0.00001)])
payment_method = models.CharField(max_length=30, null=False, default="not specified", blank=True)

View File

@ -1,9 +1,10 @@
from django.urls import path
from .views import OrderMakerView, OrderView, UserView, BookView
from .views import OrderMakerView, OrderView, UserView, BookView, get_currencies_json
urlpatterns = [
path('make/', OrderMakerView.as_view()),
path('order/', OrderView.as_view({'get':'get','post':'take_or_update'})),
path('usergen/', UserView.as_view()),
path('book/', BookView.as_view()),
path('currencies/', get_currencies_json),
]

View File

@ -21,6 +21,9 @@ from pathlib import Path
from datetime import timedelta
from django.utils import timezone
import json
from django.http import HttpResponse
# .env
expiration_time = 8
@ -263,6 +266,7 @@ class BookView(ListAPIView):
def get(self,request, format=None):
currency = request.GET.get('currency')
print("currency:", currency)
type = request.GET.get('type')
queryset = Order.objects.filter(currency=currency, type=type, status=int(Order.Status.PUB))
if len(queryset)== 0:
@ -281,7 +285,10 @@ class BookView(ListAPIView):
book_data.append(data)
return Response(book_data, status=status.HTTP_200_OK)
def get_currencies_json(request):
currency_dict = json.load(open('./api/currencies.json'))
return HttpResponse(json.dumps(currency_dict),content_type="application/json")

View File

@ -8,8 +8,10 @@ export default class BookPage extends Component {
orders: new Array(),
currency: 1,
type: 1,
currencies_dict: {"1":"USD"}
};
this.getOrderDetails()
this.getCurrencyDict()
this.state.currencyCode = this.getCurrencyCode(this.state.currency)
}
@ -24,6 +26,15 @@ export default class BookPage extends Component {
not_found: data.not_found,
}));
}
getCurrencyDict() {
fetch('/api/currencies')
.then((response) => response.json())
.then((data) =>
this.setState({
currencies_dict: data
}));
}
handleCardClick=(e)=>{
console.log(e)
@ -48,7 +59,7 @@ export default class BookPage extends Component {
// Gets currency code (3 letters) from numeric (e.g., 1 -> USD)
// Improve this function so currencies are read from json
getCurrencyCode(val){
return (val == 1 ) ? "USD": ((val == 2 ) ? "EUR":"ETH")
return this.state.currencies_dict[val.toString()]
}
// pretty numbers
@ -156,9 +167,10 @@ export default class BookPage extends Component {
}}
onChange={this.handleCurrencyChange}
>
<MenuItem value={1}>USD</MenuItem>
<MenuItem value={2}>EUR</MenuItem>
<MenuItem value={3}>ETH</MenuItem>
{
Object.entries(this.state.currencies_dict)
.map( ([key, value]) => <MenuItem value={parseInt(key)}>{value}</MenuItem> )
}
</Select>
</FormControl>
</Grid>

View File

@ -8,5 +8,6 @@ urlpatterns = [
path('make/', index),
path('book/', index),
path('order/<int:orderId>', index),
path('wait/', index),
path('wait/', index),
path('currencies/',index)
]