module Dialog.AddPayment.Model exposing ( Model , init , initialAdd , initialClone , initialEdit ) import Date exposing (Date) import View.Date as Date import Form exposing (Form) import Form.Field as Field exposing (Field) import Form.Validate as Validate exposing (Validation) import Validation import Model.Payment as Payment exposing (Payment, Frequency, PaymentId) import Model.Translations exposing (Translations) type alias Model = { id : Maybe PaymentId , name : String , cost : Int , date : Date , frequency : Frequency } init : Form String Model init = Form.initial [] validation initialAdd : Translations -> Date -> Frequency -> List (String, Field) initialAdd translations date frequency = [ ("date", Field.Text (Date.shortView date translations)) , ("frequency", Field.Radio (toString frequency)) ] initialClone : Translations -> Date -> Payment -> List (String, Field) initialClone translations date payment = [ ("name", Field.Text payment.name) , ("cost", Field.Text (toString payment.cost)) , ("date", Field.Text (Date.shortView date translations)) , ("frequency", Field.Radio (toString payment.frequency)) ] initialEdit : Translations -> Payment -> List (String, Field) initialEdit translations payment = [ ("id", Field.Text (toString payment.id)) , ("name", Field.Text payment.name) , ("cost", Field.Text (toString payment.cost)) , ("date", Field.Text (Date.shortView payment.date translations)) , ("frequency", Field.Radio (toString payment.frequency)) ] validation : Validation String Model validation = Validate.form5 Model (Validate.get "id" (Validate.maybe Validate.int)) (Validate.get "name" (Validate.string `Validate.andThen` (Validate.nonEmpty))) (Validate.get "cost" (Validate.int `Validate.andThen` (Validate.minInt 1))) (Validate.get "date" Validation.date) (Validate.get "frequency" Payment.validateFrequency)