From d9b39d903bebd046f17ee9c8da406071c3177bd1 Mon Sep 17 00:00:00 2001 From: Alexander Simmerl Date: Wed, 4 Jan 2017 22:33:14 +0100 Subject: [PATCH] Use WebData for all respones --- cmd/console/App.elm | 99 +++++++++++++++++++++++++++-------------- cmd/console/Console.elm | 6 +-- handler/http/app.go | 11 ++--- 3 files changed, 72 insertions(+), 44 deletions(-) diff --git a/cmd/console/App.elm b/cmd/console/App.elm index e744f4e..d69d109 100644 --- a/cmd/console/App.elm +++ b/cmd/console/App.elm @@ -36,6 +36,7 @@ type alias Model = { apps : WebData (List App) , description : String , name : String + , new : WebData App , selected : WebData App } @@ -60,7 +61,7 @@ initApp = initModel : WebData (List App) -> WebData App -> Model initModel apps app = - Model apps "" "" app + Model apps "" "" NotAsked app -- UPDATE @@ -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 @@ -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 @@ -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" ] ] @@ -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 = @@ -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 @@ -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 @@ -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 ]) diff --git a/cmd/console/Console.elm b/cmd/console/Console.elm index a3dbca4..994e6aa 100644 --- a/cmd/console/Console.elm +++ b/cmd/console/Console.elm @@ -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" ] diff --git a/handler/http/app.go b/handler/http/app.go index e0e8ee9..c414243 100644 --- a/handler/http/app.go +++ b/handler/http/app.go @@ -28,6 +28,8 @@ func AppCreate(fn core.AppCreateFunc) Handler { return } + time.Sleep(500 * time.Millisecond) + respondJSON(w, http.StatusOK, &payloadApp{app: a}) } } @@ -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}) } @@ -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}) }