module Company exposing (Company
, Msg
, view
, update
, init)
import Html exposing (Html, text, form, input, label, fieldset, select, option, div, a, p)
import Html.Attributes exposing (name, value, selected, disabled, type', class)
import Html.Events exposing (on, onWithOptions)
import Json.Decode as Decode
import Json.Encode as Encode
import Regex
import String
import Validated exposing (Validated(..), Validator, and)
import Http
import Task
import MappedInput exposing (textInput, selectInput)
type alias Company =
{ name : Validated String String
, registrationNumber : Validated String String
, sector : Validated Int String
, line1 : Validated String String
, line2 : Validated String String
, townCity : Validated String String
, postcode : Validated String String
}
type Msg = SetName String
| SetRegistrationNumber String
| SetSector Int
| SetLine1 String
| SetLine2 String
| SetTownCity String
| SetPostcode String
| Submit
| SubmitFail Http.Error
| SubmitSucceed (Decode.Decoder String)
init : Company
init = { name = Clean
, registrationNumber = Clean
, sector = Clean
, line1 = Clean
, line2 = Clean
, townCity = Clean
, postcode = Clean
}
update : Msg -> Company -> (Company, Cmd Msg)
update msg model =
case msg of
SetName name -> { model | name = Validated.notEmpty name } ! []
SetRegistrationNumber number -> { model | registrationNumber = Validated.registrationNumber number } ! []
SetSector sector -> { model | sector = Valid sector } ! []
SetLine1 line1 -> { model | line1 = (Validated.notEmpty `and` Validated.maxLength 20) line1 } ! []
SetLine2 line2 -> { model | line2 = Validated.maxLength 20 line2 } ! []
SetTownCity townCity -> { model | townCity = Validated.maxLength 20 townCity } ! []
SetPostcode postcode -> { model | postcode = (Validated.notEmpty `and` Validated.postcode) postcode } ! []
Submit -> case validate model of
Ok _ -> model ! [Task.perform SubmitFail SubmitSucceed (submitCompany model)]
Err _ -> model ! []
SubmitFail error ->
Debug.log (toString error) (model ! [])
SubmitSucceed response ->
Debug.log "success" (model ! [])
submitCompany : Company -> Task.Task Http.Error (Decode.Decoder String)
submitCompany company =
let
url = "http://localhost:8090/company"
in
Http.post (Decode.succeed Decode.string) url (Http.string (encode company))
encode : Company -> String
encode company =
let
stringEncoder = Validated.encode Encode.string
intEncoder = Validated.encode Encode.int
in
Encode.encode 0 <|
Encode.object
[ ("name", stringEncoder company.name)
, ("registrationNumber", stringEncoder company.registrationNumber)
, ("sector", intEncoder company.sector)
, ("line1", stringEncoder company.line1)
, ("line2", stringEncoder company.line2)
, ("townCity", stringEncoder company.townCity)
, ("postcode", stringEncoder company.postcode)
]
validate : Company -> Result Company Company
validate company =
let
ok = List.all (Validated.valid) [ company.name
, company.registrationNumber
, company.line1
, company.line2
, company.townCity
, company.postcode
]
&& (Validated.valid company.sector)
in
if ok then Ok company else Err company
view : Company -> Html Msg
view model =
let
companyName = textInput (SetName) model.name
registrationNumber = textInput (SetRegistrationNumber) model.registrationNumber
sector = selectInput (SetSector) [ "Hair & Beauty", "Fashion Retail", "Car Sales/Garage", "Healthcare" ]
line1 = textInput (SetLine1) model.line1
line2 = textInput (SetLine2) model.line2
townCity = textInput (SetTownCity) model.townCity
postcode = textInput (SetPostcode) model.postcode
in
form [ name "company" ]
[ label [ class "label" ] [ text "Company name" ]
, p [ class "control" ] companyName
, label [ class "label" ] [ text "Registration number" ]
, p [ class "control" ] registrationNumber
, label [ class "label" ] [ text "Sector" ]
, p [ class "control" ] sector
, label [ class "label" ] [ text "Line 1" ]
, p [ class "control" ] line1
, label [ class "label" ] [ text "Line 2" ]
, p [ class "control" ] line2
, label [ class "label" ] [ text "Town/City" ]
, p [ class "control" ] townCity
, label [ class "label" ] [ text "Postcode" ]
, p [ class "control" ] postcode
, p [ class "control" ]
[ a [ class "button is-primary", Html.Events.onClick (Submit) ] [ text "Submit" ]
]
]