Skip to content

Commit

Permalink
(fix) Refactor all the decode functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dawehner committed Jul 9, 2017
1 parent 9fded12 commit 375436c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 63 deletions.
12 changes: 12 additions & 0 deletions src/App/ModelHttp.elm
Expand Up @@ -2,6 +2,7 @@ module App.ModelHttp exposing (..)

import App.Model exposing (..)
import App.Difficulty exposing (..)
import App.Utils exposing (removeErrorFromList)
import Json.Encode
import Json.Decode exposing (field, string, int, list, succeed, Decoder, map4, at, map, map2)
import JsonApi.Resources
Expand Down Expand Up @@ -331,6 +332,17 @@ decodeRecipe flags resource =
JsonApi.Resources.attributes (recipeDecoderWithValues id (Result.map .url file_image) tags category owner) resource


decodeRecipes : Flags -> RemoteData.WebData (List JsonApi.Resource) -> RemoteData.WebData (List Recipe)
decodeRecipes flags =
RemoteData.map
(\resources ->
List.map
(decodeRecipe flags)
resources
|> removeErrorFromList
)


resultToWebData : Result String a -> RemoteData.WebData a
resultToWebData result =
case result of
Expand Down
70 changes: 7 additions & 63 deletions src/App/Update.elm
@@ -1,6 +1,7 @@
module App.Update exposing (..)

import App.Model exposing (..)
import App.Utils exposing (removeErrorFromList)
import App.PageType exposing (..)
import App.ModelHttp exposing (..)
import JsonApi.Resources
Expand Down Expand Up @@ -177,15 +178,7 @@ update msg model =
PromotedRecipesLoaded remoteResponse ->
( { model
| pageHome =
{ promotedRecipes =
RemoteData.map
(\resources ->
List.map
(decodeRecipe model.flags)
resources
|> removeErrorFromList
)
remoteResponse
{ promotedRecipes = decodeRecipes model.flags remoteResponse
, promotedArticles = model.pageHome.promotedArticles
, recipes = model.pageHome.recipes
}
Expand All @@ -198,15 +191,7 @@ update msg model =
| pageHome =
{ promotedRecipes = model.pageHome.promotedRecipes
, promotedArticles = model.pageHome.promotedArticles
, recipes =
RemoteData.map
(\resources ->
List.map
(decodeRecipe model.flags)
resources
|> removeErrorFromList
)
remoteResponse
, recipes = decodeRecipes model.flags remoteResponse
}
}
, Cmd.none
Expand Down Expand Up @@ -235,14 +220,7 @@ update msg model =
RecipesPerCategoryLoaded category remoteResponse ->
let
additionalRecipes =
RemoteData.map
(\resources ->
List.map
(decodeRecipe model.flags)
resources
|> removeErrorFromList
)
remoteResponse
decodeRecipes model.flags remoteResponse
in
case model.currentPage of
RecipesPerCategoryList ->
Expand All @@ -262,42 +240,21 @@ update msg model =
RecipesPerTagLoaded tag remoteResponse ->
let
recipes =
RemoteData.map
(\resources ->
List.map
(decodeRecipe model.flags)
resources
|> removeErrorFromList
)
remoteResponse
decodeRecipes model.flags remoteResponse
in
( { model | pageRecipesPerTag = ( tag, recipes ) }, Cmd.none )

RecipesPerDifficultyLoaded difficulty remoteResponse ->
let
recipes =
RemoteData.map
(\resources ->
List.map
(decodeRecipe model.flags)
resources
|> removeErrorFromList
)
remoteResponse
decodeRecipes model.flags remoteResponse
in
( { model | pageRecipesPerDifficulty = ( difficulty, recipes ) }, Cmd.none )

RecipesShorterThanLoaded minutes remoteResponse ->
let
recipes =
RemoteData.map
(\resources ->
List.map
(decodeRecipe model.flags)
resources
|> removeErrorFromList
)
remoteResponse
decodeRecipes model.flags remoteResponse
in
( { model | pageRecipesShorterThan = ( minutes, recipes ) }, Cmd.none )

Expand Down Expand Up @@ -351,19 +308,6 @@ joinListMaybe list =
Just filteredList


removeErrorFromList : List (Result a b) -> List b
removeErrorFromList list =
case (List.reverse list) of
(Ok a) :: xs ->
a :: removeErrorFromList xs

(Err b) :: xs ->
removeErrorFromList xs

[] ->
[]


updateError : String -> Http.Error -> Model -> ( Model, Cmd Msg )
updateError debugMessage error model =
let
Expand Down
14 changes: 14 additions & 0 deletions src/App/Utils.elm
@@ -0,0 +1,14 @@
module App.Utils exposing (removeErrorFromList)

removeErrorFromList : List (Result a b) -> List b
removeErrorFromList list =
case (List.reverse list) of
(Ok a) :: xs ->
a :: removeErrorFromList xs

(Err b) :: xs ->
removeErrorFromList xs

[] ->
[]

0 comments on commit 375436c

Please sign in to comment.