Skip to content

Commit

Permalink
Merge pull request #45 from tapglue/console-user
Browse files Browse the repository at this point in the history
Add user search and update to Console
  • Loading branch information
xla authored Jun 13, 2017
2 parents dff044c + cf3dc64 commit b366a08
Show file tree
Hide file tree
Showing 14 changed files with 645 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# Folders
_obj
_test
elm-stuff
node_modules

# Architecture specific extensions/prefixes
*.[568vq]
Expand All @@ -22,5 +24,3 @@ _testmain.go
*.exe
*.test
*.prof

elm-stuff
25 changes: 25 additions & 0 deletions cmd/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,31 @@ func main() {
),
)

router.Methods("GET").Path("/api/apps/{appID:[0-9]+}/users/{userID:[0-9]+}").Name("userFetch").HandlerFunc(
handler.Wrap(
withConstraints,
handler.UserFetchConsole(
core.UserFetchConsole(apps, users),
),
),
)

router.Methods("GET").Path("/api/apps/{appID:[0-9]+}/users/search").Name("userSearch").HandlerFunc(
handler.Wrap(
withConstraints,
handler.UserSearchConsole(
core.UserSearchConsole(apps, users),
),
),
)

router.Methods("PUT").Path(`/api/apps/{appID:[0-9]+}/users/{userID:[0-9]+}`).Name("userUpdate").HandlerFunc(
handler.Wrap(
withConstraints,
handler.UserUpdateConsole(core.UserUpdateConsole(apps, users)),
),
)

router.Methods("GET").PathPrefix("/fonts").Name("fonts").Handler(
http.FileServer(FS(*staticLocal)),
)
Expand Down
14 changes: 14 additions & 0 deletions cmd/console/src/Action.elm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import App.Model exposing (App)
import Member.Model exposing (Member)
import Route exposing (Route)
import Rule.Model exposing (Rule)
import User.Model exposing (User)


type Msg
Expand All @@ -33,3 +34,16 @@ type Msg
| RuleDelete (Result Http.Error ())
| Tick Time
| TokenPersist String
| UserFetch (WebData User)
| UserSearch (WebData (List User))
| UserSearchFormBlur String
| UserSearchFormClear
| UserSearchFormFocus String
| UserSearchFormSubmit
| UserSearchFormUpdate String String
| UserUpdate (WebData User)
| UserUpdateFormBlur String
| UserUpdateFormClear
| UserUpdateFormFocus String
| UserUpdateFormSubmit
| UserUpdateFormUpdate String String
21 changes: 21 additions & 0 deletions cmd/console/src/Error.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Error exposing (decodeList)

import Json.Decode as Decode


type alias Error =
{ code : Int
, message : String
}


decode : Decode.Decoder Error
decode =
Decode.map2 Error
(Decode.field "code" Decode.int)
(Decode.field "message" Decode.string)


decodeList : Decode.Decoder (List Error)
decodeList =
Decode.at [ "errors" ] (Decode.list decode)
48 changes: 40 additions & 8 deletions cmd/console/src/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import Member.Model exposing (Member)
import Route exposing (Route, parse)
import Rule.Api exposing (getRule, listRules)
import Rule.Model exposing (Rule)
import User.Api exposing (getUser)
import User.Model exposing (User, initUserSearchForm, initUserUpdateForm)


type alias Flags =
Expand All @@ -35,6 +37,11 @@ type alias Model =
, rules : WebData (List Rule)
, startTime : Time
, time : Time
, user : WebData User
, userId : String
, users : WebData (List User)
, userSearchForm : Form
, userUpdateForm : Form
, zone : String
}

Expand All @@ -57,7 +64,7 @@ init { loginUrl, zone } location =
Just code ->
( model, Cmd.map MemberLogin (loginMember code) )

Just (Route.Login) ->
Just Route.Login ->
( model, Cmd.none )

_ ->
Expand All @@ -71,7 +78,7 @@ init { loginUrl, zone } location =

initModel : String -> String -> Maybe Route -> Model
initModel loginUrl zone route =
Model NotAsked NotAsked initAppForm "" "" loginUrl Loading NotAsked route NotAsked NotAsked 0 0 zone
Model NotAsked NotAsked initAppForm "" "" loginUrl Loading NotAsked route NotAsked NotAsked 0 0 NotAsked "" NotAsked initUserSearchForm (initUserUpdateForm NotAsked) zone


initRoute : Model -> ( Model, Cmd Msg )
Expand All @@ -80,9 +87,26 @@ initRoute model =
Just (Route.App id) ->
( { model | app = Loading, appId = id }, Cmd.map FetchApp (getApp id) )

Just (Route.Apps) ->
Just Route.Apps ->
( { model | apps = Loading }, Cmd.map FetchApps getApps )

Just (Route.Rule appId ruleId) ->
case model.app of
Success _ ->
( { model | appId = appId, rule = Loading }
, Cmd.batch
[ Cmd.map FetchRule (getRule appId ruleId)
]
)

_ ->
( { model | app = Loading, appId = appId, rule = Loading }
, Cmd.batch
[ Cmd.map FetchApp (getApp appId)
, Cmd.map FetchRule (getRule appId ruleId)
]
)

Just (Route.Rules appId) ->
case model.app of
Success _ ->
Expand All @@ -100,23 +124,31 @@ initRoute model =
]
)

Just (Route.Rule appId ruleId) ->
Just (Route.User appId userId) ->
case model.app of
Success _ ->
( { model | appId = appId, rule = Loading }
( { model | appId = appId, user = Loading, userId = userId }
, Cmd.batch
[ Cmd.map FetchRule (getRule appId ruleId)
[ Cmd.map UserFetch (getUser appId userId)
]
)

_ ->
( { model | app = Loading, appId = appId, rule = Loading }
( { model | app = Loading, appId = appId, user = Loading, userId = userId }
, Cmd.batch
[ Cmd.map FetchApp (getApp appId)
, Cmd.map FetchRule (getRule appId ruleId)
, Cmd.map UserFetch (getUser appId userId)
]
)

Just (Route.Users appId) ->
case model.app of
Success _ ->
( model, Cmd.none )

_ ->
( { model | app = Loading, appId = appId }, Cmd.map FetchApp (getApp appId) )

_ ->
( model, Cmd.none )

Expand Down
5 changes: 5 additions & 0 deletions cmd/console/src/Route.elm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Route
| OAuthCallback (Maybe String) (Maybe String)
| Rule String String
| Rules String
| User String String
| Users String


Expand Down Expand Up @@ -50,6 +51,9 @@ construct route =
Rules appId ->
"/apps/" ++ appId ++ "/rules"

User appId uesrId ->
"/apps/" ++ appId ++ "/users/" ++ uesrId

Users appId ->
"/apps/" ++ appId ++ "/users"

Expand All @@ -75,5 +79,6 @@ routes =
, map OAuthCallback (s "oauth2callback" <?> stringParam "code" <?> stringParam "state")
, map Rule (s "apps" </> string </> s "rules" </> string)
, map Rules (s "apps" </> string </> s "rules")
, map User (s "apps" </> string </> s "users" </> string)
, map Users (s "apps" </> string </> s "users")
]
59 changes: 59 additions & 0 deletions cmd/console/src/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import App.Api exposing (createApp)
import App.Model exposing (initAppForm)
import Route
import Rule.Api exposing (activateRule, deactivateRule, deleteRule)
import User.Api exposing (searchUser, updateUser)
import User.Model exposing (initUserSearchForm, initUserUpdateForm)


update : Msg -> Model -> ( Model, Cmd Msg )
Expand Down Expand Up @@ -131,6 +133,63 @@ update msg model =
in
( { model | startTime = startTime, time = time }, Cmd.none )

UserFetch response ->
( { model | user = response, userUpdateForm = (initUserUpdateForm response) }, Cmd.none )

UserSearch response ->
( { model | users = response }, Cmd.none )

UserSearchFormBlur field ->
( { model | userSearchForm = blurElement model.userSearchForm field }, Cmd.none )

UserSearchFormClear ->
( { model | userSearchForm = initUserSearchForm }, Cmd.none )

UserSearchFormFocus field ->
( { model | userSearchForm = focusElement model.userSearchForm field }, Cmd.none )

UserSearchFormSubmit ->
let
( form, isValid ) =
validateForm model.userSearchForm
in
case isValid of
True ->
( { model | users = Loading }, Cmd.map UserSearch (searchUser model.appId (elementValue model.userSearchForm "query")) )

False ->
( { model | userSearchForm = form }, Cmd.none )

UserSearchFormUpdate field value ->
( { model | userSearchForm = updateElementValue model.userSearchForm field value }, Cmd.none )

UserUpdate response ->
( { model | user = response }, Cmd.none )

UserUpdateFormBlur field ->
( { model | userUpdateForm = blurElement model.userUpdateForm field }, Cmd.none )

UserUpdateFormClear ->
( { model | userUpdateForm = (initUserUpdateForm model.user) }, Cmd.none )

UserUpdateFormFocus field ->
( { model | userUpdateForm = focusElement model.userUpdateForm field }, Cmd.none )

UserUpdateFormSubmit ->
let
( form, isValid ) =
validateForm model.userUpdateForm
in
case isValid of
True ->
( { model | user = Loading }, Cmd.map UserUpdate (updateUser model.appId model.userId (elementValue model.userUpdateForm "username")) )

False ->
( { model | userUpdateForm = form }, Cmd.none )

UserUpdateFormUpdate field value ->
( { model | userUpdateForm = updateElementValue model.userUpdateForm field value }, Cmd.none )



-- HELPER
Expand Down
36 changes: 36 additions & 0 deletions cmd/console/src/User/Api.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module User.Api exposing (getUser, searchUser, updateUser)

import Http
import RemoteData exposing (WebData, sendRequest)
import User.Model exposing (User, decode, decodeList, encode)


getUser : String -> String -> Cmd (WebData User)
getUser appId userId =
Http.get ("/api/apps/" ++ appId ++ "/users/" ++ userId) decode
|> sendRequest


searchUser : String -> String -> Cmd (WebData (List User))
searchUser appId query =
Http.get ("/api/apps/" ++ appId ++ "/users/search?q=" ++ query) decodeList
|> sendRequest


updateUser : String -> String -> String -> Cmd (WebData User)
updateUser appId userId username =
Http.request
{ body = (encode username |> Http.jsonBody)
, expect = Http.expectJson decode
, headers = []
, method = "PUT"
, timeout = Nothing
, url = (userUrl appId userId)
, withCredentials = False
}
|> sendRequest


userUrl : String -> String -> String
userUrl appId userId =
"/api/apps/" ++ appId ++ "/users/" ++ userId
Loading

0 comments on commit b366a08

Please sign in to comment.