aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/LoggedIn/User
diff options
context:
space:
mode:
authorJoris2016-04-04 01:27:36 +0200
committerJoris2016-04-04 01:27:36 +0200
commit8cd63a64abafe21378c35c2489d49f24c9ece3c9 (patch)
tree541145481d1492f3e388002d931cb3f8fec0acb2 /src/client/elm/LoggedIn/User
parent01e4ce0fa7c369996ec4ef3a033d16d6fa0eb715 (diff)
Add income list CRUD in user page
Diffstat (limited to 'src/client/elm/LoggedIn/User')
-rw-r--r--src/client/elm/LoggedIn/User/Action.elm9
-rw-r--r--src/client/elm/LoggedIn/User/Model.elm46
-rw-r--r--src/client/elm/LoggedIn/User/Update.elm25
-rw-r--r--src/client/elm/LoggedIn/User/View.elm90
4 files changed, 167 insertions, 3 deletions
diff --git a/src/client/elm/LoggedIn/User/Action.elm b/src/client/elm/LoggedIn/User/Action.elm
new file mode 100644
index 0000000..c5f8d47
--- /dev/null
+++ b/src/client/elm/LoggedIn/User/Action.elm
@@ -0,0 +1,9 @@
+module LoggedIn.User.Action
+ ( Action(..)
+ ) where
+
+import Form exposing (Form)
+
+type Action =
+ NoOp
+ | AddIncomeAction Form.Action
diff --git a/src/client/elm/LoggedIn/User/Model.elm b/src/client/elm/LoggedIn/User/Model.elm
new file mode 100644
index 0000000..4f96a80
--- /dev/null
+++ b/src/client/elm/LoggedIn/User/Model.elm
@@ -0,0 +1,46 @@
+module LoggedIn.User.Model
+ ( Model
+ , AddIncome
+ , init
+ ) where
+
+import String exposing (toInt, split)
+import Date exposing (Date)
+import Date.Utils exposing (dateFromFields)
+import Utils.Date exposing (numToMonth)
+
+import Form exposing (Form)
+import Form.Validate as Validate exposing (..)
+import Form.Error exposing (Error(InvalidString))
+
+type alias Model =
+ { addIncome : Form () AddIncome
+ }
+
+type alias AddIncome =
+ { creation : Date
+ , amount : Int
+ }
+
+init : Model
+init =
+ { addIncome = Form.initial [] validate
+ }
+
+validate : Validation () AddIncome
+validate =
+ form2 AddIncome
+ (get "creation" dateValidation)
+ (get "amount" (int `andThen` (minInt 1)))
+
+dateValidation : Validation () Date
+dateValidation =
+ customValidation string (\str ->
+ case split "/" str of
+ [day, month, year] ->
+ case (toInt day, toInt month, toInt year) of
+ (Ok dayNum, Ok monthNum, Ok yearNum) ->
+ Ok (dateFromFields yearNum (numToMonth monthNum) dayNum 0 0 0 0)
+ _ -> Err InvalidString
+ _ -> Err InvalidString
+ )
diff --git a/src/client/elm/LoggedIn/User/Update.elm b/src/client/elm/LoggedIn/User/Update.elm
new file mode 100644
index 0000000..f44fee4
--- /dev/null
+++ b/src/client/elm/LoggedIn/User/Update.elm
@@ -0,0 +1,25 @@
+module LoggedIn.User.Update
+ ( update
+ ) where
+
+import Effects exposing (Effects)
+import Form exposing (Form)
+
+import LoggedData exposing (LoggedData)
+
+import LoggedIn.User.Model as UserModel
+import LoggedIn.User.Action as UserAction
+
+update : LoggedData -> UserAction.Action -> UserModel.Model -> (UserModel.Model, Effects UserAction.Action)
+update loggedData action model =
+ case action of
+
+ UserAction.NoOp ->
+ ( model
+ , Effects.none
+ )
+
+ UserAction.AddIncomeAction formAction ->
+ ( { model | addIncome = Form.update formAction model.addIncome }
+ , Effects.none
+ )
diff --git a/src/client/elm/LoggedIn/User/View.elm b/src/client/elm/LoggedIn/User/View.elm
index 35ea940..74e2ae2 100644
--- a/src/client/elm/LoggedIn/User/View.elm
+++ b/src/client/elm/LoggedIn/User/View.elm
@@ -2,10 +2,94 @@ module LoggedIn.User.View
( view
) where
+import Dict
+import Date
+
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)
+import Model.Translations exposing (getMessage)
+import LoggedIn.User.Model as UserModel
+
+import Mailbox
+
+import Action
+import LoggedIn.Action as LoggedInAction
+import LoggedIn.User.Action as UserAction
+
+import LoggedIn.View.Date exposing (renderShortDate)
+import LoggedIn.View.Price exposing (price)
+
+import Utils.Maybe exposing (isJust)
-view : LoggedData -> Html
-view loggedData =
+view : LoggedData -> UserModel.Model -> Html
+view loggedData userModel =
div
[]
- [ text "Hey" ]
+ [ h1 [] [ text <| getMessage "AddIncome" loggedData.translations ]
+ , addIncomeView loggedData userModel.addIncome
+ , h1 [] [ text <| getMessage "Incomes" loggedData.translations ]
+ , incomesView loggedData
+ ]
+
+addIncomeView : LoggedData -> Form () UserModel.AddIncome -> Html
+addIncomeView loggedData addIncome =
+ let
+ formAddress = Signal.forwardTo Mailbox.address (Action.UpdateLoggedIn << LoggedInAction.UserAction << UserAction.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
+ div
+ []
+ [ 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 =
+ ol
+ []
+ ( 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 <| price loggedData.conf income.amount
+ , text " − "
+ , button
+ [ onClick Mailbox.address (Action.UpdateLoggedIn <| LoggedInAction.DeleteIncome incomeId) ]
+ [ text "x" ]
+ ]