module LoggedIn.Income.View ( view ) where import Dict import Date import Time exposing (Time) import Html exposing (..) import Html.Events exposing (..) import Html.Attributes exposing (..) import Form exposing (Form) import Form.Input as Input import LoggedData exposing (LoggedData) import Model.Income exposing (IncomeId, Income, userCumulativeIncomeSince) import Model.Translations exposing (getMessage, getParamMessage) import Model.Payer exposing (useIncomesFrom) import Model.User exposing (UserId, User) import LoggedIn.Income.Model as IncomeModel import Mailbox import Action import LoggedIn.Action as LoggedInAction import LoggedIn.Income.Action as IncomeAction import LoggedIn.View.Date exposing (renderShortDate) import LoggedIn.View.Format as Format import Utils.Maybe exposing (isJust) import LoggedIn.View.Date exposing (renderLongDate) import View.Events exposing (onSubmitPrevDefault) view : LoggedData -> IncomeModel.Model -> Html view loggedData incomeModel = div [ class "income" ] [ case useIncomesFrom loggedData.users loggedData.incomes loggedData.payments of Just since -> cumulativeIncomesView loggedData since Nothing -> text "" , h1 [] [ text <| getMessage "AddIncome" loggedData.translations ] , addIncomeView loggedData incomeModel.addIncome , h1 [] [ text <| getMessage "MonthlyNetIncomes" loggedData.translations ] , incomesView loggedData ] cumulativeIncomesView : LoggedData -> Time -> Html cumulativeIncomesView loggedData since = let longDate = renderLongDate (Date.fromTime since) loggedData.translations in div [] [ h1 [] [ text <| getParamMessage [longDate] "CumulativeIncomesSince" loggedData.translations ] , ul [] ( Dict.toList loggedData.users |> List.map (\(userId, user) -> (user.name, userCumulativeIncomeSince loggedData.currentTime since loggedData.incomes userId) ) |> List.sortBy snd |> List.map (\(userName, cumulativeIncome) -> li [] [ text userName , text " − " , text <| Format.price loggedData.conf cumulativeIncome ] ) ) ] addIncomeView : LoggedData -> Form () IncomeModel.AddIncome -> Html addIncomeView loggedData addIncome = let formAddress = Signal.forwardTo Mailbox.address (Action.UpdateLoggedIn << LoggedInAction.IncomeAction << IncomeAction.AddIncomeAction) errorFor error field = if isJust field.liveError then div [ class "error" ] [ text (getMessage error loggedData.translations) ] else text "" creation = Form.getFieldAsString "creation" addIncome amount = Form.getFieldAsString "amount" addIncome in Html.form [ onSubmitPrevDefault Mailbox.address Action.NoOp ] [ label [] [ text "Creation" ] , Input.textInput creation formAddress [] , errorFor "DateValidationError" creation , label [] [ text "amount" ] , Input.textInput amount formAddress [] , errorFor "IncomeValidationError" amount , button [ case Form.getOutput addIncome of Just data -> onClick Mailbox.address (Action.UpdateLoggedIn <| LoggedInAction.AddIncome data.creation data.amount) Nothing -> onClick formAddress Form.Submit ] [ text (getMessage "Add" loggedData.translations) ] ] incomesView : LoggedData -> Html incomesView loggedData = ul [] ( loggedData.incomes |> Dict.toList |> List.filter ((==) loggedData.me << .userId << snd) |> List.sortBy (.creation << snd) |> List.reverse |> List.map (incomeView loggedData) ) incomeView : LoggedData -> (IncomeId, Income) -> Html incomeView loggedData (incomeId, income) = li [] [ text <| renderShortDate (Date.fromTime income.creation) loggedData.translations , text " − " , text <| Format.price loggedData.conf income.amount , text " − " , button [ onClick Mailbox.address (Action.UpdateLoggedIn <| LoggedInAction.DeleteIncome incomeId) ] [ text "x" ] ]