Commit e04dba79 authored by Cédric F.'s avatar Cédric F.

Add address model

parent 22b6d96e
module Components.Address exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Http exposing (Error)
import Json.Encode as Encode
import Json.Decode as Decode
import Lib.JsonRpc as JsonRpc
import Lib.Decred exposing (..)
import Components.Transaction as Transaction
type alias Model =
{ address : String
, transactions : List Transaction.Model
, tickets : List String
, fetching : Bool
, error : Maybe String
}
initialModel : Model
initialModel =
{ address = ""
, transactions = []
, tickets = []
, fetching = False
, error = Nothing
}
modelFromJson : JsonModel -> Model
modelFromJson jsonModel =
{ address = jsonModel.address
, transactions = List.map Transaction.modelFromJson jsonModel.transactions
, tickets = []
, fetching = False
, error = Nothing
}
type alias JsonModel =
{ address : String
, transactions : List Transaction.JsonModel
}
type Msg
= SearchRawTransactions String
| SearchRawTransactionsResult (Result Http.Error JsonModel)
| TicketsForAddress String
| TicketsForAddressResult (Result Http.Error (List String))
view : Model -> Html a
view model =
let
formatFoo foo =
span [ class "badge badge badge-success" ] [ text foo ]
in
div [ class "row" ]
[ div [ class "col-6 offset-3" ]
[ div [ class "card bg-dark" ]
[ h5 [ class "card-header" ]
[ span [] [ text <| "Address " ++ model.address ]
, a
[ class "float-right"
, target "_blank"
, title "Open on dcrdata.org"
, href <| "https://explorer.dcrdata.org/explorer/address/" ++ model.address
]
[ span [ class "oi oi-external-link" ] [] ]
]
, div [ class "card-body" ]
[ p [ class "card-text" ]
[ dl [ class "row" ]
[ dt [ class "col-3 text-right" ] [ text "foo" ]
, dd [ class "col-9" ] [ text "?" ]
, dt [ class "col-3 text-right" ] [ text "last time" ]
, dd [ class "col-9" ] [ text "?" ]
, dt [ class "col-3 text-right" ] [ text "balance" ]
, dd [ class "col-9" ] [ text <| dcrAmount (balance model) ]
]
]
, hr [] []
, div [ class "row" ]
[ div [ class "col" ]
[ h4 [ class "text-center" ]
[ span [ class "badge badge-pill badge-info" ] [ text "transactions" ]
]
, ul [ class "list-group list-group-flush" ] <|
List.map
(\transaction ->
li [ class "list-group-item bg-secondary" ]
[ span [ class "badge badge-info" ] [ text "foo" ] ]
)
model.transactions
]
]
]
]
]
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SearchRawTransactions address ->
let
updatedModel =
{ model | fetching = True }
in
( updatedModel, searchRawTransactions updatedModel address )
SearchRawTransactionsResult result ->
case result of
Ok jsonModel ->
( modelFromJson jsonModel, Cmd.none )
Err error ->
( { model
| error = JsonRpc.parseError error
, fetching = False
}
, Cmd.none
)
_ ->
-- TODO: tickets
( model, Cmd.none )
searchRawTransactions : Model -> String -> Cmd Msg
searchRawTransactions model address =
let
params =
Encode.list
[ Encode.string address
-- verbose
, Encode.int 1
-- skip
, Encode.int 0
-- count
, Encode.int 100
-- vinextra
, Encode.int 0
-- reverse
, Encode.bool True
]
in
JsonRpc.post "searchrawtransactions"
params
SearchRawTransactionsResult
(decodeSearchRawTransactions address)
decodeSearchRawTransactions : String -> Decode.Decoder JsonModel
decodeSearchRawTransactions address =
Decode.map2 JsonModel
(Decode.succeed address)
(Decode.field "result"
(Decode.list (Transaction.decodeGetRawTransaction Nothing))
)
-- "methods" to get info from Model
balance : Model -> Float
balance model =
-- TODO:
0
This diff is collapsed.
......@@ -5,6 +5,7 @@ import Window
import Keyboard
import Time exposing (Time)
import Components.Status as StatusComponent
import Components.Address as AddressComponent
import Components.Block as BlockComponent
import Components.Transaction as TransactionComponent
......@@ -20,6 +21,7 @@ type alias Model =
{ keys : Keys
, window : Window.Size
, statusModel : StatusComponent.Model
, addressModel : AddressComponent.Model
, blockModel : BlockComponent.Model
, transactionModel : TransactionComponent.Model
, query : String
......@@ -36,6 +38,7 @@ initialModel =
{ keys = Keys False False False False False False
, window = Window.Size 0 0
, statusModel = StatusComponent.initialModel
, addressModel = AddressComponent.initialModel
, blockModel = BlockComponent.initialModel
, transactionModel = TransactionComponent.initialModel
, query = ""
......@@ -50,6 +53,7 @@ initialModel =
type Msg
= NewUrl Navigation.Location
| StatusMsg StatusComponent.Msg
| AddressMsg AddressComponent.Msg
| BlockMsg BlockComponent.Msg
| TransactionMsg TransactionComponent.Msg
| JsMsg String
......
......@@ -7,6 +7,7 @@ import Time exposing (Time)
import Window
import Trappisto.Model exposing (..)
import Components.Status as StatusComponent
import Components.Address as AddressComponent
import Components.Block as BlockComponent
import Components.Transaction as TransactionComponent
......@@ -144,6 +145,20 @@ update action model =
)
|> updateUrl
AddressMsg addressMsg ->
let
( updatedModel, cmd ) =
AddressComponent.update addressMsg model.addressModel
in
( { model
| template = Address
, query = updatedModel.address
, addressModel = updatedModel
}
, Cmd.map AddressMsg cmd
)
|> updateUrl
BlockMsg blockMsg ->
let
( updatedModel, cmd ) =
......@@ -217,7 +232,7 @@ update action model =
fetchAddress : String -> Model -> ( Model, Cmd Msg )
fetchAddress address model =
( model, Cmd.none )
update (AddressMsg (AddressComponent.SearchRawTransactions address)) model
fetchBlockByHash : String -> Model -> ( Model, Cmd Msg )
......
......@@ -5,6 +5,7 @@ import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Trappisto.Model exposing (..)
import Components.Status as StatusComponent exposing (view)
import Components.Address as AddressComponent exposing (view)
import Components.Block as BlockComponent exposing (view)
import Components.Transaction as TransactionComponent exposing (view)
......@@ -28,9 +29,8 @@ getError model =
Transaction ->
model.transactionModel.error
-- FIXME:
Address ->
Nothing
model.addressModel.error
Block ->
model.blockModel.error
......@@ -94,6 +94,11 @@ statusView model =
Html.map StatusMsg (StatusComponent.view model.statusModel)
addressView : Model -> Html Msg
addressView model =
Html.map AddressMsg (AddressComponent.view model.addressModel)
blockView : Model -> Html Msg
blockView model =
Html.map BlockMsg (BlockComponent.view model.blockModel)
......@@ -130,9 +135,6 @@ view model =
status model =
div [ class "row" ] [ statusView model ]
transaction model =
div [ class "row" ] [ div [ class "col-6 offset-3" ] [ transactionView model ] ]
content =
if model.error /= Nothing then
[ searchView model, errorView model ]
......@@ -147,6 +149,12 @@ view model =
else
[ searchView model, errorView model, status model ]
Address ->
if isError model then
[ searchView model, errorView model ]
else
[ searchView model, addressView model ]
Block ->
if isError model then
[ searchView model, errorView model ]
......@@ -157,10 +165,7 @@ view model =
if isError model then
[ searchView model, errorView model ]
else
[ searchView model, transaction model ]
_ ->
Debug.crash <| toString model.template
[ searchView model, transactionView model ]
debug =
[ hr [] []
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment