Review fixes.

This commit is contained in:
Dillon Kearns 2022-02-03 20:17:00 -08:00
parent 1ed1d8bb8a
commit 38cd1c35c6
3 changed files with 23 additions and 213 deletions

View File

@ -42,13 +42,6 @@ mainView :
-> Model userModel pageData sharedData
-> { title : String, body : Html userMsg }
mainView config model =
let
urls : { currentUrl : Url, basePath : List String }
urls =
{ currentUrl = model.url
, basePath = config.basePath
}
in
case model.notFound of
Just info ->
Pages.Internal.NotFoundReason.document config.pathPatterns info
@ -56,6 +49,13 @@ mainView config model =
Nothing ->
case model.pageData of
Ok pageData ->
let
urls : { currentUrl : Url, basePath : List String }
urls =
{ currentUrl = model.url
, basePath = config.basePath
}
in
(config.view
{ path = ContentCache.pathForUrl urls |> Path.join
, route = config.urlToRoute model.url
@ -305,12 +305,6 @@ update config appMsg model =
navigatingToSamePage : Bool
navigatingToSamePage =
(url.path == model.url.path) && (url /= model.url)
urls : { currentUrl : Url, basePath : List String }
urls =
{ currentUrl = url
, basePath = config.basePath
}
in
if navigatingToSamePage then
-- this saves a few CPU cycles, but also
@ -322,6 +316,12 @@ update config appMsg model =
|> Result.map
(\pageData ->
let
urls : { currentUrl : Url, basePath : List String }
urls =
{ currentUrl = url
, basePath = config.basePath
}
updatedPageData : Result String { userModel : userModel, sharedData : sharedData, pageData : pageData }
updatedPageData =
Ok
@ -482,15 +482,16 @@ update config appMsg model =
route =
model.url
|> config.urlToRoute
newThing : Maybe (ResponseSketch pageData sharedData)
newThing =
pageDataBytes
|> Bytes.Decode.decode config.decodeResponse
in
model.pageData
|> Result.map
(\pageData ->
let
newThing : Maybe (ResponseSketch pageData sharedData)
newThing =
pageDataBytes
|> Bytes.Decode.decode config.decodeResponse
in
case newThing of
Just (ResponseSketch.RenderPage newPageData) ->
( { model

View File

@ -3,23 +3,14 @@ module Pages.ContentCache exposing
, ContentJson
, Entry(..)
, Path
, contentJsonDecoder
, eagerLoad
, init
, is404
, lazyLoad
, notFoundReason
, pathForUrl
)
import Codec
import Dict exposing (Dict)
import Http
import Json.Decode as Decode
import Pages.Internal.NotFoundReason
import Pages.Internal.String as String
import RequestsAndPending exposing (RequestsAndPending)
import Task exposing (Task)
import Url exposing (Url)
@ -28,7 +19,7 @@ type alias ContentCache =
type Entry
= Parsed ContentJson
= Parsed
type alias Path =
@ -43,87 +34,8 @@ init maybeInitialPageContent =
Nothing ->
Dict.empty
Just ( urls, contentJson ) ->
Dict.singleton urls (Parsed contentJson)
{-| Get from the Cache... if it's not already parsed, it will
parse it before returning it and store the parsed version in the Cache
-}
lazyLoad :
{ currentUrl : Url, basePath : List String }
-> ContentCache
-> Task Http.Error ( Url, ContentJson, ContentCache )
lazyLoad urls cache =
case Dict.get (pathForUrl urls) cache of
Just (Parsed contentJson) ->
Task.succeed
( urls.currentUrl
, contentJson
, cache
)
Nothing ->
eagerLoad urls cache
{-| -}
eagerLoad :
{ currentUrl : Url, basePath : List String }
-> ContentCache
-> Task Http.Error ( Url, ContentJson, ContentCache )
eagerLoad urls cache =
urls.currentUrl
|> httpTask
|> Task.map
(\downloadedContent ->
( urls.currentUrl
, downloadedContent
, update
cache
urls
downloadedContent
)
)
httpTask : Url -> Task Http.Error ContentJson
httpTask url =
Http.task
{ method = "GET"
, headers = []
, url =
url.path
|> String.chopForwardSlashes
|> String.split "/"
|> List.filter ((/=) "")
|> (\l -> l ++ [ "content.json" ])
|> String.join "/"
|> String.append "/"
, body = Http.emptyBody
, resolver =
Http.stringResolver
(\response ->
case response of
Http.BadUrl_ url_ ->
Err (Http.BadUrl url_)
Http.Timeout_ ->
Err Http.Timeout
Http.NetworkError_ ->
Err Http.NetworkError
Http.BadStatus_ metadata _ ->
Err (Http.BadStatus metadata.statusCode)
Http.GoodStatus_ _ body ->
body
|> Decode.decodeString contentJsonDecoder
|> Result.mapError (\err -> Http.BadBody (Decode.errorToString err))
)
, timeout = Nothing
}
Just ( urls, _ ) ->
Dict.singleton urls Parsed
type alias ContentJson =
@ -134,71 +46,6 @@ type alias ContentJson =
}
contentJsonDecoder : Decode.Decoder ContentJson
contentJsonDecoder =
Decode.field "is404" Decode.bool
|> Decode.andThen
(\is404Value ->
if is404Value then
Decode.map4 ContentJson
(Decode.succeed Dict.empty)
(Decode.succeed is404Value)
(Decode.field "path" Decode.string |> Decode.map Just)
(Decode.at [ "staticData", "notFoundReason" ]
(Decode.string
|> Decode.andThen
(\jsonString ->
case
Decode.decodeString
(Codec.decoder Pages.Internal.NotFoundReason.codec
|> Decode.map Just
)
jsonString
of
Ok okValue ->
Decode.succeed okValue
Err error ->
Decode.fail
(Decode.errorToString error)
)
)
)
else
Decode.map4 ContentJson
(Decode.field "staticData" RequestsAndPending.decoder)
(Decode.succeed is404Value)
(Decode.succeed Nothing)
(Decode.succeed Nothing)
)
update :
ContentCache
-> { currentUrl : Url, basePath : List String }
-> ContentJson
-> ContentCache
update cache urls rawContent =
Dict.update
(pathForUrl urls)
(\entry ->
case entry of
Just (Parsed _) ->
entry
Nothing ->
{ staticData = rawContent.staticData
, is404 = rawContent.is404
, path = rawContent.path
, notFoundReason = rawContent.notFoundReason
}
|> Parsed
|> Just
)
cache
pathForUrl : { currentUrl : Url, basePath : List String } -> Path
pathForUrl { currentUrl, basePath } =
currentUrl.path
@ -206,35 +53,3 @@ pathForUrl { currentUrl, basePath } =
|> String.split "/"
|> List.filter ((/=) "")
|> List.drop (List.length basePath)
is404 :
ContentCache
-> { currentUrl : Url, basePath : List String }
-> Bool
is404 dict urls =
dict
|> Dict.get (pathForUrl urls)
|> Maybe.map
(\entry ->
case entry of
Parsed data ->
data.is404
)
|> Maybe.withDefault True
notFoundReason :
ContentCache
-> { currentUrl : Url, basePath : List String }
-> Maybe Pages.Internal.NotFoundReason.Payload
notFoundReason dict urls =
dict
|> Dict.get (pathForUrl urls)
|> Maybe.map
(\entry ->
case entry of
Parsed data ->
data.notFoundReason
)
|> Maybe.withDefault Nothing

View File

@ -1,7 +1,6 @@
module RequestsAndPending exposing (RequestsAndPending, decoder, get)
module RequestsAndPending exposing (RequestsAndPending, get)
import Dict exposing (Dict)
import Json.Decode as Decode
type alias RequestsAndPending =
@ -13,8 +12,3 @@ get key requestsAndPending =
requestsAndPending
|> Dict.get key
|> Maybe.andThen identity
decoder : Decode.Decoder RequestsAndPending
decoder =
Decode.dict (Decode.string |> Decode.map Just)