2022-02-19 12:17:02 +00:00
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
2022-02-20 22:18:40 +00:00
import net.dankito.banking.client.model.AccountTransaction
import net.dankito.banking.fints.model.TanChallenge
2022-02-23 00:43:41 +00:00
import react.*
2022-02-17 00:19:37 +00:00
import react.dom.*
import styled.styledDiv
external interface AccountTransactionsViewProps : Props {
2022-02-19 14:15:23 +00:00
var presenter : Presenter
2022-02-17 00:19:37 +00:00
}
2022-02-23 00:43:41 +00:00
data class AccountTransactionsViewState ( val balance : String , val transactions : Collection < AccountTransaction > , var showTransferMoneyView : Boolean = false ,
val enterTanChallenge : TanChallenge ? = null ) : State
2022-02-17 00:19:37 +00:00
@JsExport
class AccountTransactionsView ( props : AccountTransactionsViewProps ) : RComponent < AccountTransactionsViewProps , AccountTransactionsViewState > ( props ) {
init {
state = AccountTransactionsViewState ( " " , listOf ( ) )
2022-02-23 00:43:41 +00:00
props . presenter . enterTanCallback = { setState ( AccountTransactionsViewState ( state . balance , state . transactions , state . showTransferMoneyView , it ) ) }
2022-02-19 14:15:23 +00:00
2022-02-17 00:19:37 +00:00
// due to CORS your bank's servers can not be requested directly from browser -> set a CORS proxy url in main.kt
// TODO: set your credentials here
2022-02-19 12:17:02 +00:00
GlobalScope . launch {
2022-02-21 00:07:00 +00:00
props . presenter . retrieveAccountData ( " " , " " , " " ) { response ->
2022-02-20 22:18:40 +00:00
response . customerAccount ?. let { customer ->
val balance = customer . accounts . sumOf { it . balance ?. amount ?. string ?. replace ( ',' , '.' ) ?. toDoubleOrNull ( ) ?: 0.0 } // i know, double is not an appropriate data type for amounts
2022-02-17 00:19:37 +00:00
2022-02-20 22:18:40 +00:00
setState ( AccountTransactionsViewState ( balance . toString ( ) + " " + ( customer . accounts . firstOrNull ( ) ?. balance ?. currency ?: " " ) , customer . accounts . flatMap { it . bookedTransactions } , state . enterTanChallenge ) )
2022-02-19 14:15:23 +00:00
}
2022-02-17 00:19:37 +00:00
}
}
}
override fun RBuilder . render ( ) {
2022-02-19 14:15:23 +00:00
state . enterTanChallenge ?. let { challenge ->
child ( EnterTanView :: class ) {
attrs {
presenter = props . presenter
tanChallenge = challenge
}
}
}
2022-02-23 00:43:41 +00:00
button {
span { + " Transfer Money " }
attrs {
onMouseUp = { setState { showTransferMoneyView = ! showTransferMoneyView } }
}
}
if ( state . showTransferMoneyView ) {
child ( TransferMoneyView :: class ) {
attrs {
presenter = props . presenter
}
}
}
2022-02-17 00:19:37 +00:00
p {
+ " Saldo: ${state.balance} "
}
div {
state . transactions . forEach { transaction ->
div {
styledDiv {
if ( transaction . showOtherPartyName ) {
div { transaction . otherPartyName }
}
div {
+ transaction . reference
}
}
}
}
}
}
}