Skip to content

Instantly share code, notes, and snippets.

@skinnyjames
Last active July 24, 2017 14:01
Show Gist options
  • Save skinnyjames/c8fcebded81a70e2a3eded701d68a622 to your computer and use it in GitHub Desktop.
Save skinnyjames/c8fcebded81a70e2a3eded701d68a622 to your computer and use it in GitHub Desktop.
Elm Subs
main =
Navigation.program Types.UrlChange
{ init = Update.init
, view = Views.view
, update = Update.update
, subscriptions = subscriptions
}
-- Subscriptions
subscriptions : Model -> Sub Msg
subscriptions model =
let debug = Debug.log "current request" model
in
case model.currentRequest of
Nothing ->
Sub.none
Just Home ->
let url = "/app/account"
request =
Http.getString url
in
Progress.track (toString model.currentRequestNumber) Types.OnLoadAccountProgress request
Just (Profiles id) ->
let url = "/app/profiles"
request =
Http.getString url
in
Progress.track (toString model.currentRequestNumber) (\x -> Types.OnLoadProfilesProgress x id) request
Just _ ->
Sub.none
update : Types.Msg -> Model -> ( Model, Cmd Types.Msg )
update msg model =
case msg of
...
UrlChange location ->
let
page = Url.parsePath route location
model_ = { model | history = page :: model.history }
debug = Debug.log "history" model.history
in
( model_, Cmd.none )
...
OnLoadProfilesProgress progress mid ->
case progress of
Progress.None ->
({ model | currentRequestNumber = 0 }, Cmd.none )
Progress.Some { bytes, bytesExpected } ->
let
debugbytes = Debug.log "bytes" bytes
debugexpect = Debug.log "expected" bytesExpected
num =
round
<| (*) 100
<| toFloat bytes / toFloat bytesExpected
in
({ model | currentRequestNumber = num}, Cmd.none)
Progress.Done response ->
case decodeString Api.profilesDecoder response of
-- Redirect to login page on successful install
Ok body ->
let debug = Debug.log "profiles" body
model_ = { model | profiles = Just body, activeProfile = (getActiveProfile body mid), currentRequest = Nothing }
--active = (List.head body.profiles)
in
(model_, Cmd.none)
Err err ->
let debug = Debug.log "profile err" err
in
( model, Cmd.none )
profilesForm : Types.Model -> Html Msg
profilesForm model =
case model.activeProfile of
Nothing ->
div [ ] [ text "No profile Selected" ]
Just profile ->
div []
[ div [ Attr.class "section-header" ]
[ h1 [] [ text ("EDIT PROFILE " ++ profile.name ) ]
, Button.render Types.Mdl
[ 0 ]
model.mdl
[ Button.ripple
, Options.onClick Types.NoOp
]
[ text "Delete" ]
]
, div [ Attr.class "stage__permissions" ]
[ h3 [] [ text "Users Permissions" ]
, div []
[ Toggles.switch Types.Mdl [1] model.mdl
[ Toggles.ripple
, Toggles.value profile.usersView
, Options.onToggle Types.ProfileChangeViewUsers
]
[ text "View Users"]
, Toggles.switch Types.Mdl [2] model.mdl
[ Toggles.ripple
, Toggles.value profile.usersEdit
, Options.onToggle Types.ProfileChangeEditUsers
]
[ text "Edit Users"]
, Toggles.switch Types.Mdl [3] model.mdl
[ Toggles.ripple
, Toggles.value profile.usersCreate
, Options.onToggle Types.ProfileChangeCreateUsers
]
[ text "Create Users"]
, Toggles.switch Types.Mdl [4] model.mdl
[ Toggles.ripple
, Toggles.value profile.usersDelete
, Options.onToggle Types.ProfileChangeDeleteUsers
]
[ text "Delete Users"]
]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment