Display collection statistics in footer
This commit is contained in:
parent
362bef913c
commit
f7277eb250
2 changed files with 55 additions and 1 deletions
|
@ -5,6 +5,7 @@ import Browser.Dom
|
|||
import Browser.Events
|
||||
import Browser.Navigation
|
||||
import Card
|
||||
import Collection
|
||||
import Color
|
||||
import Dict
|
||||
import Element as E
|
||||
|
@ -47,6 +48,7 @@ type alias Model =
|
|||
, criteria : Criteria
|
||||
, cardPage : CardPage
|
||||
, activeCard : Maybe Card.Card
|
||||
, collectionStatistics : Maybe Collection.Statistics
|
||||
}
|
||||
|
||||
|
||||
|
@ -58,6 +60,7 @@ type Msg
|
|||
| UpdateCriteria CriteriaMsg
|
||||
| Search
|
||||
| GetPage Url.Url
|
||||
| GotStatistics (Result Http.Error Collection.Statistics)
|
||||
| FoundCards (Result Http.Error (Paginated.Page Card.Card))
|
||||
| ShowCardDetails Card.Card
|
||||
| ClearCardDetails
|
||||
|
@ -148,6 +151,14 @@ loadPage url =
|
|||
}
|
||||
|
||||
|
||||
getCollectionStatistics : Cmd Msg
|
||||
getCollectionStatistics =
|
||||
Http.get
|
||||
{ url = Url.Builder.absolute [ "collection" ] []
|
||||
, expect = Http.expectJson GotStatistics Collection.decodeStatistics
|
||||
}
|
||||
|
||||
|
||||
parseUrl : Url.Parser.Parser (Criteria -> a) a
|
||||
parseUrl =
|
||||
let
|
||||
|
@ -204,9 +215,11 @@ init _ url key =
|
|||
, criteria = criteria
|
||||
, cardPage = Loading Paginated.empty
|
||||
, activeCard = Nothing
|
||||
, collectionStatistics = Nothing
|
||||
}
|
||||
, Cmd.batch
|
||||
[ search criteria
|
||||
, getCollectionStatistics
|
||||
, Task.perform
|
||||
(\x ->
|
||||
ViewportChanged
|
||||
|
@ -287,6 +300,12 @@ update msg model =
|
|||
GetPage url ->
|
||||
( { model | cardPage = toLoading model.cardPage }, loadPage (Url.toString url) )
|
||||
|
||||
GotStatistics (Ok statistics) ->
|
||||
( { model | collectionStatistics = Just statistics }, Cmd.none )
|
||||
|
||||
GotStatistics (Err _) ->
|
||||
( model, Cmd.none )
|
||||
|
||||
FoundCards (Ok cardPage) ->
|
||||
( { model | cardPage = Ready cardPage }, Cmd.none )
|
||||
|
||||
|
@ -755,11 +774,27 @@ view model =
|
|||
, E.el
|
||||
[ E.height (E.px 50)
|
||||
, E.width E.fill
|
||||
, E.padding 10
|
||||
, Font.color colors.text
|
||||
, Background.color colors.navBar
|
||||
, E.alignBottom
|
||||
]
|
||||
<|
|
||||
E.none
|
||||
case model.collectionStatistics of
|
||||
Just statistics ->
|
||||
E.el [ E.centerY, Font.size 16, Font.italic ] <|
|
||||
E.text <|
|
||||
String.concat
|
||||
[ String.fromInt statistics.cards
|
||||
, " cards in collection spanning "
|
||||
, String.fromInt statistics.sets
|
||||
, " sets (Estimated value: $"
|
||||
, statistics.value
|
||||
, ")"
|
||||
]
|
||||
|
||||
Nothing ->
|
||||
E.none
|
||||
]
|
||||
]
|
||||
}
|
||||
|
|
19
www/src/Collection.elm
Normal file
19
www/src/Collection.elm
Normal file
|
@ -0,0 +1,19 @@
|
|||
module Collection exposing (..)
|
||||
|
||||
import Json.Decode
|
||||
import Json.Decode.Pipeline as JDP
|
||||
|
||||
|
||||
type alias Statistics =
|
||||
{ cards : Int
|
||||
, sets : Int
|
||||
, value : String
|
||||
}
|
||||
|
||||
|
||||
decodeStatistics : Json.Decode.Decoder Statistics
|
||||
decodeStatistics =
|
||||
Json.Decode.succeed Statistics
|
||||
|> JDP.required "cards" Json.Decode.int
|
||||
|> JDP.required "sets" Json.Decode.int
|
||||
|> JDP.required "value" Json.Decode.string
|
Loading…
Reference in a new issue