Skip to content

Instantly share code, notes, and snippets.

@zkessin
Created November 9, 2015 09:37
Show Gist options
  • Save zkessin/fe7264fb5a25173533db to your computer and use it in GitHub Desktop.
Save zkessin/fe7264fb5a25173533db to your computer and use it in GitHub Desktop.
module CRMBox where
import Debug exposing (watch)
import Html exposing (..)
import Html.Events exposing (onBlur, targetValue, on)
import Html.Attributes exposing (style, selected)
import Effects
import Json.Encode
import Json.Decode
-- model
type Crm = Unknown| SalesForce| Streak
type alias CRMName = String
type alias Model = Crm
init: Model
init = Unknown
-- UPDATE
type Action = UpdateCrm Model
update : Action -> Model -> (Model, Effects.Effects Action)
update action model =
case watch "Action " action of
UpdateCrm new ->
(Debug.log "New" new, Effects.none)
-- View
view : Signal.Address Action -> Model -> Html
view address model =
let
menu = makeOptions crmNames model
in
div [ style []]
[
label [
] [text "Select your CRM"
,
select [
on "input" targetValue (Signal.message address << UpdateCrm << lookup)
]
menu
]
]
encode : Model -> (String,Json.Decode.Value)
encode model =
("crm", Json.Encode.string (Maybe.withDefault "" ( lookupAction model )))
--**********************************************************************
(=>) = (,)
crmNames : List (Crm, String)
crmNames =
[
Unknown => ""
, SalesForce => "SalesForce"
, Streak => "Streak"
]
lookupAction : Crm -> Maybe String
lookupAction action =
Maybe.map snd (lookupItter (\(key,_) -> key == action) crmNames)
lookup: String -> Crm
lookup string =
Maybe.withDefault Unknown ( Maybe.map fst(lookupItter (\(_,key) -> key == string) crmNames))
lookupItter: (a -> Bool) -> List a -> Maybe a
lookupItter check actions =
case actions of
[] ->
Nothing
first :: rest ->
if check first then
Just first
else
lookupItter check rest
makeOptions : List ( Crm , String) -> Model -> List Html
makeOptions options model =
List.map (\(action, name) -> option [ selected (model == action)] [text name]) options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment