Skip to content

Commit

Permalink
Use WebData for all respones
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Simmerl committed Jan 4, 2017
1 parent f816cc1 commit d9b39d9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 44 deletions.
99 changes: 65 additions & 34 deletions cmd/console/App.elm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type alias Model =
{ apps : WebData (List App)
, description : String
, name : String
, new : WebData App
, selected : WebData App
}

Expand All @@ -60,7 +61,7 @@ initApp =

initModel : WebData (List App) -> WebData App -> Model
initModel apps app =
Model apps "" "" app
Model apps "" "" NotAsked app


-- UPDATE
Expand All @@ -70,13 +71,12 @@ type Msg
= FetchApp (WebData App)
| FetchApps (WebData (List App))
| Description String
| List
| ListApps
| Name String
| New (Result Http.Error App)
| New (WebData App)
| Select String
| Submit


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Expand All @@ -89,25 +89,20 @@ update msg model =
Description description ->
( { model | description = description }, Cmd.none )

List ->
ListApps ->
( model, Navigation.newUrl (Route.construct Route.Apps) )

Name name ->
( { model | name = name }, Cmd.none )

New (Ok app) ->
-- TODO optimistic append with WebData.
--( { model | apps = model.apps ++ [ app ], description = "", name = "" }, Cmd.none )
( { model | description = "", name = "" }, Cmd.none )

New (Err _) ->
( model, Cmd.none )
New response ->
( { model | apps = (combineApps model.apps response), new = NotAsked, description = "", name = "" }, Cmd.none )

Select id ->
( model, Navigation.newUrl (Route.construct (Route.App id)) )

Submit ->
( model, create model.name model.description )
( { model | new = Loading }, create model.name model.description )


-- VIEW
Expand Down Expand Up @@ -170,7 +165,7 @@ viewContext app =
in
Container.view (section [ class sectionClass, id "context" ])
[ h2 []
[ a [ onClick List ]
[ a [ onClick ListApps ]
[ span [ class "icon nc-icon-glyph ui-2_layers" ] []
, span [] [ text "Apps" ]
]
Expand All @@ -181,25 +176,39 @@ viewContext app =

viewForm : Model -> Html Msg
viewForm model =
form [ onSubmit Submit ]
[ input
[ onInput Name
, placeholder "Name"
, type_ "text"
, value model.name
]
[]
, input
[ class "description"
, onInput Description
, placeholder "Description"
, type_ "text"
, value model.description
]
[]
, button [ type_ "submit" ] [ text "Create" ]
]
let
createForm =
form [ onSubmit Submit ]
[ input
[ onInput Name
, placeholder "Name"
, type_ "text"
, value model.name
]
[]
, input
[ class "description"
, onInput Description
, placeholder "Description"
, type_ "text"
, value model.description
]
[]
, button [ type_ "submit" ] [ text "Create" ]
]
in
case model.new of
NotAsked ->
createForm

Loading ->
text "Creating..."

Failure err ->
text ("Failed: " ++ toString err)

Success _ ->
createForm

viewItem : App -> Html Msg
viewItem app =
Expand Down Expand Up @@ -263,7 +272,8 @@ viewSelected app =
create : String -> String -> Cmd Msg
create name description =
Http.post "/api/apps" (encode name description) decode
|> Http.send New
|> sendRequest
|> Cmd.map New


decode : Decode.Decoder App
Expand All @@ -284,7 +294,11 @@ decodeList =

encode : String -> String -> Http.Body
encode name description =
Http.jsonBody (Encode.object [ ( "name", Encode.string name ), ( "description", Encode.string description ) ])
Encode.object
[ ( "name", Encode.string name )
, ( "description", Encode.string description )
]
|> Http.jsonBody


getApp : String -> Cmd Msg
Expand All @@ -298,3 +312,20 @@ getApps =
Http.get "/api/apps" decodeList
|> sendRequest
|> Cmd.map FetchApps


-- HELPER


combineApps apps app =
case (RemoteData.toMaybe app) of
Nothing ->
apps

Just app ->
case (RemoteData.toMaybe apps) of
Nothing ->
RemoteData.succeed [ app ]

Just apps ->
RemoteData.succeed (apps ++ [ app ])
6 changes: 3 additions & 3 deletions cmd/console/Console.elm
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ view model =
[ Html.map AppMsg (App.view (App.Context model.appModel model.route.current))
]

Just (Route.Apps) ->
Just Route.Apps ->
[ Html.map AppMsg (App.view (App.Context model.appModel model.route.current))
]

Just (Route.Dashboard) ->
Just Route.Dashboard ->
[ viewDashboard model.zone ]

Just (Route.Members) ->
Just Route.Members ->
[ viewNotFound ]
in
div [ class "content" ]
Expand Down
11 changes: 4 additions & 7 deletions handler/http/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func AppCreate(fn core.AppCreateFunc) Handler {
return
}

time.Sleep(500 * time.Millisecond)

respondJSON(w, http.StatusOK, &payloadApp{app: a})
}
}
Expand All @@ -41,12 +43,7 @@ func AppList(fn core.AppListFunc) Handler {
return
}

if len(as) == 0 {
respondJSON(w, http.StatusNoContent, nil)
return
}

time.Sleep(1 * time.Second)
time.Sleep(500 * time.Millisecond)

respondJSON(w, http.StatusOK, &payloadApps{apps: as})
}
Expand All @@ -67,7 +64,7 @@ func AppRetrieve(fn core.AppFetchFunc) Handler {
return
}

time.Sleep(1 * time.Second)
time.Sleep(500 * time.Millisecond)

respondJSON(w, http.StatusOK, &payloadApp{app: a})
}
Expand Down

0 comments on commit d9b39d9

Please sign in to comment.