import Html exposing (Html, div, text, input, p, form, label, fieldset, select, option, span, section, h1, h2)
import Html.Attributes exposing (name, value, selected, disabled, class)
import Html.App
import Html.Events exposing (onInput)
import String
import Maybe
import Array exposing (Array)
import Company exposing (Company)
import Advance exposing (Advance)
import Http
import Json.Decode as Decode
import Json.Encode as Encode
import Task
import Random
main =
Html.App.program { init = init, view = view, update = update, subscriptions = subscriptions }
-- Model
type alias Applicant = {}
type IntendedUse = Stock
type CardProvider = Elavon
type Decision = Accepted | Referred | Declined
type CompanyProvider = DueDil
type alias Tracking = {}
type alias Application =
{ applicationId : Maybe String
, sessionId : String
, applicants : Array Applicant
, intendedUse : Maybe IntendedUse
, cardProvider : Maybe CardProvider
, multipleCardProviders : Maybe Bool
, submitted : Bool
, timedOut : Bool
, decision : Maybe Decision
, company : Company
, companyProvider : Maybe CompanyProvider
, tracking : Maybe Tracking
, advance : Advance
, marketingOptOut : Bool
, worldpayReferralOptIn : Bool
, bankAccountPermission : Bool
, agreeToRepaymentAndSettleTime : Bool
, primaryConsent : Bool
, thirdPartyConsent : Bool
, ptrConsent : Bool
}
type alias Model =
{ application : Application
}
init : (Model, Cmd Msg)
init = (model, Random.generate SetSessionId uuid)
-- please delete this
uuid : Random.Generator String
uuid =
let
hex = Random.map (hexToStr) (Random.int 0 16)
arr n = Random.list n (hex)
join = Random.map (String.join "")
dash a b = Random.map (\(a,b) -> a ++ "-" ++ b) (Random.pair a b)
four = join (arr 4)
eight = join (arr 8)
twelve = join (arr 12)
hexToStr = \x -> case x of
0 -> "0"
1 -> "1"
2 -> "2"
3 -> "3"
4 -> "4"
5 -> "5"
6 -> "6"
7 -> "7"
8 -> "8"
9 -> "9"
10 -> "A"
11 -> "B"
12 -> "C"
13 -> "D"
14 -> "E"
_ -> "F"
in
dash (dash (dash eight four) (dash four four)) (dash four twelve)
model : Model
model =
{ application = { applicationId = Nothing
, sessionId = ""
, applicants = Array.fromList []
, intendedUse = Nothing
, cardProvider = Nothing
, multipleCardProviders = Nothing
, submitted = False
, timedOut = False
, decision = Nothing
, company = Company.init
, companyProvider = Nothing
, tracking = Nothing
, advance = Advance.init
, marketingOptOut = False
, worldpayReferralOptIn = False
, bankAccountPermission = False
, agreeToRepaymentAndSettleTime = False
, primaryConsent = False
, thirdPartyConsent = False
, ptrConsent = False
}
}
-- Update
type Msg = SetSessionId String | SetApplicationId String | SetCompany Company.Msg | SetAdvance Advance.Msg
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
SetSessionId id ->
{ model | application = setSessionId model.application id } ! []
SetApplicationId id ->
{ model | application = setApplicationId model.application id } ! []
SetCompany companyMsg ->
let
(application, cmd) = setCompany companyMsg model.application
in
{ model | application = application } ! [cmd]
SetAdvance advanceMsg ->
let
(application, cmd) = setAdvance advanceMsg model.application
in
{ model | application = application } ! [cmd]
setSessionId : Application -> String -> Application
setSessionId application id = { application | sessionId = id }
setApplicationId : Application -> String -> Application
setApplicationId application id = { application | applicationId = Just id }
setCompany : Company.Msg -> Application -> (Application, Cmd Msg)
setCompany msg application =
let
(updatedCompany, cmd) = Company.update msg application.company
in
{ application | company = updatedCompany } ! [Cmd.map (SetCompany) cmd]
setAdvance : Advance.Msg -> Application -> (Application, Cmd Msg)
setAdvance msg application =
let
(updatedAdvance, cmd) = Advance.update msg application.advance
in
{ application | advance = updatedAdvance } ! [Cmd.map (SetAdvance) cmd]
-- View
view : Model -> Html Msg
view model =
div []
[ section [ class "hero is-primary is-medium" ]
[ div [ class "hero-body" ]
[ div [ class "container has-text-centered" ]
[ h1 [ class "title" ] [ text "Some kind of form or whatever...." ]
, h2 [ class "subtitle" ] [ text model.application.sessionId ]
]
]
]
, section [ class "section" ]
[ div [ class "container" ]
[ div [ class "columns" ]
[ div [ class "column is-half is-offset-one-quarter" ]
[ Html.App.map (SetCompany) <| Company.view model.application.company
, Html.App.map (SetAdvance) <| Advance.view model.application.advance
]
]
]
]
]
-- Subscriptions
subscriptions : Model -> Sub Msg
subscriptions model = Sub.none