module View.Page ( renderPage ) where import Html exposing (..) import Html as H import Html.Attributes exposing (..) import Html.Attributes as A import Html.Events exposing (..) import Date import Date exposing (Date) import String exposing (append) import Model exposing (Model) import Model.Payment exposing (Payments, Payment) import Model.View exposing (..) import Update exposing (..) import ServerCommunication as SC import ServerCommunication exposing (serverCommunications) import View.Icon exposing (renderIcon) renderPage : Model -> Html renderPage model = div [] [ renderHeader model , renderMain model ] renderHeader : Model -> Html renderHeader model = header [] [ h1 [] [ text "Payments" ] , case model.view of LoadingView -> text "" SignInView _ -> text "" PaymentView _ -> button [ class "signOut" , onClick serverCommunications.address SC.SignOut ] [ renderIcon "power-off" ] ] renderMain : Model -> Html renderMain model = case model.view of LoadingView -> loadingView SignInView login -> signInView login PaymentView payments -> paymentsView payments loadingView : Html loadingView = text "" signInView : String -> Html signInView login = H.form [ class "signIn" ] [ input [ value login , on "input" targetValue (Signal.message actions.address << UpdateLogin) ] [] , button [ onClick serverCommunications.address (SC.SignIn login) ] [ renderIcon "sign-in" ] ] paymentsView : Payments -> Html paymentsView payments = table [] ([ tr [] [ th [] [ renderIcon "user" ] , th [] [ renderIcon "shopping-cart" ] , th [] [ renderIcon "euro" ] , th [] [ renderIcon "calendar" ] ] ] ++ (paymentLines payments)) paymentLines : Payments -> List Html paymentLines payments = payments |> List.sortBy (Date.toTime << .creation) |> List.reverse |> List.map paymentLine paymentLine : Payment -> Html paymentLine payment = tr [] [ td [] [ text payment.userName ] , td [] [ text payment.name ] , td [] [ text ((toString payment.cost) ++ " €") ] , td [] [ text (renderDate payment.creation) ] ] renderDate : Date -> String renderDate date = toString (Date.day date) |> flip append (" " ++ (toString (Date.month date))) |> flip append (" " ++ (toString (Date.year date)))