Skip to content

Commit

Permalink
Better Url representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pamplemousse committed Nov 25, 2024
1 parent c4cbd2f commit c31db48
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 33 deletions.
16 changes: 9 additions & 7 deletions front/elm/CatGifs/Commands.elm
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
module CatGifs.Commands exposing (..)

import CatGifs.Models exposing (CatGif, CatGifsUrl)
import CatGifs.Models exposing (CatGif, cat500)
import Decoders exposing (decodeUrl)
import Http exposing (expectJson)
import Json.Decode as Decode
import Json.Decode.Pipeline exposing (requiredAt)
import Messages exposing (Msg)
import RemoteData
import Url exposing (Protocol(..), Url)


fetchCatGif : CatGifsUrl -> Cmd Msg
fetchCatGif : Url -> Cmd Msg
fetchCatGif url =
Http.get
{ url = url
, expect = expectJson (RemoteData.fromResult >> Messages.OnFetchCatGif) decodeCatGifUrl
{ url = url |> Url.toString
, expect = expectJson (RemoteData.fromResult >> Messages.OnFetchCatGif) decodeCatGif
}


decodeCatGifUrl : Decode.Decoder CatGif
decodeCatGifUrl =
decodeCatGif : Decode.Decoder CatGif
decodeCatGif =
Decode.succeed CatGif
|> requiredAt [ "data", "images", "original", "url" ] Decode.string
|> requiredAt [ "data", "images", "original", "url" ] (Decode.map (Maybe.withDefault cat500) decodeUrl)
16 changes: 11 additions & 5 deletions front/elm/CatGifs/Models.elm
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
module CatGifs.Models exposing (..)
module CatGifs.Models exposing (CatGif, cat500, catGifDefaultUrl)

-- URL of the source providing GIF URLs.
import Url exposing (Protocol(..), Url)


type alias CatGifsUrl =
String
cat500 : Url
cat500 =
Url Https "http.cat" Nothing "images/500.jpg" Nothing Nothing


catGifDefaultUrl : Url
catGifDefaultUrl =
Url Https "api.giphy.com" Nothing "v1/gifs/random" (Just "tag=cat") Nothing


type alias CatGif =
{ gifUrl : String -- URL of the GIF resource on the internet.
{ gifUrl : Url -- URL of the GIF resource on the internet.
}
3 changes: 2 additions & 1 deletion front/elm/CatGifs/View.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Html.Attributes exposing (class, src)
import HttpErrorWrapper exposing (buildErrorMessage)
import Messages exposing (Msg)
import RemoteData exposing (WebData)
import Url


view : WebData CatGif -> Html Msg
Expand All @@ -20,7 +21,7 @@ view currentCatGif =
RemoteData.Success catGif ->
div [ class "row" ]
[ div [ class "col-md-4 col-md-offset-4" ]
[ img [ src catGif.gifUrl ] []
[ img [ src (catGif.gifUrl |> Url.toString) ] []
]
]

Expand Down
5 changes: 2 additions & 3 deletions front/elm/Commands.elm
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module Commands exposing (fetchData)

import CatGifs.Commands exposing (fetchCatGif)
import CatGifs.Models exposing (CatGifsUrl)
import Messages exposing (Msg(..))
import Projects.Decoders
import Projects.Models
import TiledList
import Url exposing (Url)


fetchData : Projects.Models.Url -> CatGifsUrl -> Cmd Msg
fetchData : Url -> Url -> Cmd Msg
fetchData projectsUrl catGifsUrl =
Cmd.batch
[ Cmd.map ProjectsMsg (TiledList.fetch Projects.Decoders.decoder projectsUrl)
Expand Down
24 changes: 22 additions & 2 deletions front/elm/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@ module Main exposing (initialState, main, subscriptions)

import Browser exposing (application)
import Browser.Navigation exposing (Key)
import CatGifs.Models exposing (catGifDefaultUrl)
import Commands exposing (fetchData)
import Decoders exposing (decodeUrl)
import Json.Decode exposing (decodeString)
import Messages exposing (Msg(..))
import Models exposing (Flags, Model, initialModel)
import Projects.Models exposing (projectsDefaultUrl)
import Update exposing (update)
import Url exposing (Url)
import Url exposing (Protocol(..), Url)
import View exposing (view)


initialState : Flags -> Url -> Key -> ( Model, Cmd Msg )
initialState flags url key =
( initialModel key url flags.catGifsUrl, fetchData flags.projectsUrl flags.catGifsUrl )
let
wrap v =
[ "\"", v, "\"" ] |> String.concat

toUrl : Url -> String -> Url
toUrl default =
wrap >> decodeString decodeUrl >> Result.map (Maybe.withDefault default) >> Result.withDefault default

catGifUrl =
flags.catGifsUrl |> toUrl catGifDefaultUrl

projectsUrl =
flags.projectsUrl |> toUrl projectsDefaultUrl
in
( initialModel key url catGifUrl
, fetchData projectsUrl catGifUrl
)


subscriptions : Model -> Sub Msg
Expand Down
10 changes: 5 additions & 5 deletions front/elm/Models.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Models exposing (Flags, Model, initialModel)

import Browser.Navigation exposing (Key)
import CatGifs.Models exposing (CatGif, CatGifsUrl)
import CatGifs.Models exposing (CatGif)
import Projects.Models exposing (Project)
import RemoteData exposing (WebData)
import Routing exposing (Route)
Expand All @@ -13,7 +13,7 @@ import Url.Parser exposing (parse)

type alias Model =
{ socialMedia : List SocialMedium
, catGifsUrl : String
, catGifsUrl : Url
, currentCatGif : WebData CatGif
, key : Key
, route : Maybe Route
Expand All @@ -22,12 +22,12 @@ type alias Model =


type alias Flags =
{ projectsUrl : Projects.Models.Url
, catGifsUrl : CatGifsUrl
{ projectsUrl : String
, catGifsUrl : String
}


initialModel : Key -> Url -> String -> Model
initialModel : Key -> Url -> Url -> Model
initialModel key url catGifsUrl =
{ catGifsUrl = catGifsUrl
, currentCatGif = RemoteData.Loading
Expand Down
12 changes: 7 additions & 5 deletions front/elm/Projects/Models.elm
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
module Projects.Models exposing (Project, Url)
module Projects.Models exposing (Project, projectsDefaultUrl)

import Link
import Url exposing (Protocol(..), Url)


type alias Id =
Int


type alias Url =
String


type alias Project =
{ id : Id
, title : String
Expand All @@ -19,3 +16,8 @@ type alias Project =
, links : List Link.Model
, description : Maybe String
}


projectsDefaultUrl : Url
projectsDefaultUrl =
Url Https "www.xaviermaso.com" Nothing "api/projects" Nothing Nothing
7 changes: 2 additions & 5 deletions front/elm/TiledList.elm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import HttpErrorWrapper exposing (buildErrorMessage)
import Json.Decode as Decode
import Link
import RemoteData exposing (WebData)
import Url exposing (Url)


type alias Listable a =
Expand All @@ -34,10 +35,6 @@ type alias Model a =
}


type alias Url =
String


initialModel : Model a
initialModel =
{ all = RemoteData.Loading
Expand All @@ -48,7 +45,7 @@ initialModel =
fetch : Decode.Decoder (List (Listable a)) -> Url -> Cmd (Msg (Listable a))
fetch decoder url =
Http.get
{ url = url
{ url = url |> Url.toString
, expect = expectJson (RemoteData.fromResult >> OnFetch) decoder
}

Expand Down

0 comments on commit c31db48

Please sign in to comment.