From f7277eb250d60b699f23657c80a7c3d038c98957 Mon Sep 17 00:00:00 2001 From: Correl Date: Wed, 3 Aug 2022 18:07:56 -0400 Subject: [PATCH] Display collection statistics in footer --- www/src/App.elm | 37 ++++++++++++++++++++++++++++++++++++- www/src/Collection.elm | 19 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 www/src/Collection.elm diff --git a/www/src/App.elm b/www/src/App.elm index 64a89b2..aa73b97 100644 --- a/www/src/App.elm +++ b/www/src/App.elm @@ -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 ] ] } diff --git a/www/src/Collection.elm b/www/src/Collection.elm new file mode 100644 index 0000000..fa5648b --- /dev/null +++ b/www/src/Collection.elm @@ -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