module View.Payments.Add ( addPayment ) where import Html as H exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) import Reads exposing (readInt) import Result exposing (..) import ServerCommunication as SC exposing (serverCommunications) import Update exposing (..) import Update.Payment exposing (..) import Update.Payment.Add exposing (..) import Model exposing (Model) import Model.View.Payment.Add exposing (..) import Model.Translations exposing (getMessage) import Model.View.PaymentView exposing (PaymentView) import View.Events exposing (onSubmitPrevDefault) import View.Icon exposing (renderIcon) import Utils.Maybe exposing (isJust) import Utils.Either exposing (toMaybeError) addPayment : Model -> PaymentView -> Html addPayment model paymentView = H.form [ class "add" , case (validateName paymentView.add.name model.translations, validateCost paymentView.add.cost model.translations) of (Ok name, Ok cost) -> onSubmitPrevDefault serverCommunications.address (SC.AddPayment paymentView.userName name cost) (resName, resCost) -> onSubmitPrevDefault actions.address (UpdatePayment <| UpdateAdd <| AddError (toMaybeError resName) (toMaybeError resCost)) ] [ addPaymentName paymentView.add , addPaymentCost model paymentView.add , button [ type' "submit" ] [ text (getMessage "Add" model.translations)] ] addPaymentName : AddPayment -> Html addPaymentName addPayment = div [ class ("name " ++ (if isJust addPayment.nameError then "error" else "")) ] [ input [ id "nameInput" , value addPayment.name , on "input" targetValue (Signal.message actions.address << UpdatePayment << UpdateAdd << UpdateName) , maxlength 20 ] [] , label [ for "nameInput" ] [ renderIcon "shopping-cart" ] , case addPayment.nameError of Just error -> div [ class "errorMessage" ] [ text error ] Nothing -> text "" ] addPaymentCost : Model -> AddPayment -> Html addPaymentCost model addPayment = div [ class ("cost " ++ (if isJust addPayment.costError then "error" else "")) ] [ input [ id "costInput" , value addPayment.cost , on "input" targetValue (Signal.message actions.address << UpdatePayment << UpdateAdd << UpdateCost) , maxlength 7 ] [] , label [ for "costInput" ] [ text (getMessage "MoneySymbol" model.translations) ] , case addPayment.costError of Just error -> div [ class "errorMessage" ] [ text error ] Nothing -> text "" ]