Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor for better composability with custom types in sites #30

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions .travis.yml

This file was deleted.

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

# v5.0.0
* Add decoding for a resource's unique name

# v4.1.1
* Add extra check to `Translation.isEmpty`

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Create Elm websites backed by the [Ginger CMS](https://github.com/driebit/ginger

**Ginger.Id**: A tagged version of a Ginger resource id.

**Ginger.Category**: Ginger categories with option to define a custom one.
**Ginger.Category**: Ginger categories.

**Ginger.Predicate**: Ginger predicates with option to define a custom one.
**Ginger.Predicate**: Ginger predicates.

**Ginger.Translation**: Translate text and markup.

Expand Down
4 changes: 2 additions & 2 deletions elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "driebit/elm-ginger",
"summary": "Ginger CMS integration",
"license": "BSD-3-Clause",
"version": "4.1.1",
"version": "5.0.0",
"exposed-modules": [
"Ginger.Resource",
"Ginger.Resource.Extra",
Expand Down Expand Up @@ -32,6 +32,6 @@
"rtfeldman/elm-iso8601-date-strings": "1.1.2 <= v < 2.0.0"
},
"test-dependencies": {
"elm-explorations/test": "1.2.2 <= v < 2.0.0"
"elm-explorations/test": "2.1.0 <= v < 3.0.0"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"test": "elm-format --validate src && elm-test"
},
"dependencies": {
"elm": "0.19.0-bugfix6",
"elm": "^0.19.1-5",
"elm-format": "^0.8.1",
"elm-test": "^0.19.0"
"elm-test": "^0.19.1-revision11"
}
}
5 changes: 2 additions & 3 deletions src/Ginger/Auth.elm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ module Ginger.Auth exposing

-}

import Ginger.Resource exposing (Edges, ResourceWith)
import Http
import Iso8601
import Json.Decode as Decode
Expand All @@ -53,7 +52,7 @@ import Time

{-| -}
type Status
= Authenticated Identity (Maybe (ResourceWith Edges))
= Authenticated Identity (Maybe Decode.Value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waarom heb je hier gekozen omdat decode.value mee te geven ipv een type parameter van resource?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ik dacht eigenlijk dat het te veel impact zou hebben om daar een type variabele aan toe te voegen, maar nu ik er nog een keer naar kijk valt het eigenlijk wel mee. Ik was eerder aan de Data.Status aan het denken die ook door Fetch gebruikt wordt denk ik.

Zal ik er dan maar een type parameter van maken?

| Anonymous
| Loading
| Error Http.Error
Expand Down Expand Up @@ -180,7 +179,7 @@ fromJson : Decode.Decoder Status
fromJson =
Decode.succeed Authenticated
|> Pipeline.required "identity" identityFromJson
|> Pipeline.optional "resource" (Decode.maybe Ginger.Resource.fromJsonWithEdges) Nothing
|> Pipeline.optional "resource" (Decode.map Just Decode.value) Nothing


{-| Decode a Ginger identity json value to an Identity record
Expand Down
85 changes: 24 additions & 61 deletions src/Ginger/Category.elm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Ginger.Category exposing
-}

import Json.Decode as Decode
import List.NonEmpty exposing (NonEmpty)
import Json.Decode.Extra as Decode



Expand All @@ -51,74 +51,40 @@ type Category
| Text
| Video
| Website
| Custom String



-- DECODE


{-| -}
fromJson : Decode.Decoder (NonEmpty Category)
fromJson : Decode.Decoder Category
fromJson =
Decode.map (List.NonEmpty.reverse << List.NonEmpty.map fromString) <|
List.NonEmpty.fromJson Decode.string
Decode.oneOf <|
[ Decode.constant Text "text"
, Decode.constant Person "person"
, Decode.constant Location "location"
, Decode.constant Website "website"
, Decode.constant Event "event"
, Decode.constant Artifact "artifact"
, Decode.constant Media "media"
, Decode.constant Image "image"
, Decode.constant Video "video"
, Decode.constant Audio "audio"
, Decode.constant Document "document"
, Decode.constant Collection "collection"
, Decode.constant Meta "meta"
, Decode.constant Agenda "agenda"
, Decode.constant Article "article"
, Decode.constant News "news"
]


{-| -}
fromString : String -> Category
fromString category =
case category of
"text" ->
Text

"person" ->
Person

"location" ->
Location

"website" ->
Website

"event" ->
Event

"artifact" ->
Artifact

"media" ->
Media

"image" ->
Image

"video" ->
Video

"audio" ->
Audio

"document" ->
Document

"collection" ->
Collection

"meta" ->
Meta

"agenda" ->
Agenda

"article" ->
Article

"news" ->
News

custom ->
Custom custom
fromString : String -> Maybe Category
fromString =
Result.toMaybe
<< Decode.decodeString fromJson


{-| -}
Expand Down Expand Up @@ -172,6 +138,3 @@ toString category =

News ->
"news"

Custom custom ->
custom
45 changes: 14 additions & 31 deletions src/Ginger/Predicate.elm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Ginger.Predicate exposing
-}

import Json.Decode as Decode
import Json.Decode.Extra as Decode



Expand All @@ -41,7 +42,6 @@ type Predicate
| HasBanner
| HasPart
| HasRelation
| Custom String



Expand All @@ -51,36 +51,22 @@ type Predicate
{-| -}
fromJson : Decode.Decoder Predicate
fromJson =
Decode.map fromString Decode.string
Decode.oneOf
[ Decode.constant IsAbout "about"
, Decode.constant HasAuthor "author"
, Decode.constant HasDepiction "depiction"
, Decode.constant HasBanner "hasbanner"
, Decode.constant HasDocument "hasdocument"
, Decode.constant HasPart "haspart"
, Decode.constant HasRelation "relation"
]


{-| -}
fromString : String -> Predicate
fromString predicate =
case predicate of
"about" ->
IsAbout

"author" ->
HasAuthor

"depiction" ->
HasDepiction

"hasbanner" ->
HasBanner

"hasdocument" ->
HasDocument

"haspart" ->
HasPart

"relation" ->
HasRelation

x ->
Custom x
fromString : String -> Maybe Predicate
fromString =
Result.toMaybe
<< Decode.decodeString fromJson


{-| -}
Expand All @@ -107,6 +93,3 @@ toString predicate =

HasRelation ->
"relation"

Custom x ->
x
47 changes: 30 additions & 17 deletions src/Ginger/Request.elm
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ import File exposing (File)
import Ginger.Category as Category exposing (Category)
import Ginger.Id as Id exposing (ResourceId)
import Ginger.Predicate as Predicate exposing (Predicate)
import Ginger.Resource as Resource exposing (Edges, ResourceWith)
import Ginger.Resource.Extra as Extra exposing (Location)
import Http
import Internal.Request as Request
import Json.Decode as Decode
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline as Pipeline
import Json.Encode as Encode
import Task exposing (Task)
Expand Down Expand Up @@ -103,31 +102,43 @@ absolute path queryParams =

{-| Request a resource by `ResourceId`
-}
resourceById : (Result Http.Error (ResourceWith Edges) -> msg) -> ResourceId -> Cmd msg
resourceById msg id =
resourceById :
Decoder resource
-> (Result Http.Error resource -> msg)
-> ResourceId
-> Cmd msg
resourceById resourceFromJson msg id =
Http.get
{ url = absolute [ Id.toString id ] []
, expect = Http.expectJson msg Resource.fromJsonWithEdges
, expect = Http.expectJson msg resourceFromJson
}


{-| Request a resource by its `page_path`
-}
resourceByPath : (Result Http.Error (ResourceWith Edges) -> msg) -> String -> Cmd msg
resourceByPath msg path =
resourceByPath :
Decoder resource
-> (Result Http.Error resource -> msg)
-> String
-> Cmd msg
resourceByPath resourceFromJson msg path =
Http.get
{ url = absolute [ "path", Url.percentEncode path ] []
, expect = Http.expectJson msg Resource.fromJsonWithEdges
, expect = Http.expectJson msg resourceFromJson
}


{-| Request a resource by its unique name
-}
resourceByName : (Result Http.Error (ResourceWith Edges) -> msg) -> String -> Cmd msg
resourceByName msg id =
resourceByName :
Decoder resource
-> (Result Http.Error resource -> msg)
-> String
-> Cmd msg
resourceByName resourceFromJson msg id =
Http.get
{ url = absolute [ id ] []
, expect = Http.expectJson msg Resource.fromJsonWithEdges
, expect = Http.expectJson msg resourceFromJson
}


Expand All @@ -141,17 +152,18 @@ resourceByName msg id =

-}
search :
(Result Http.Error (SearchResult (ResourceWith Edges)) -> msg)
Decoder resource
-> (Result Http.Error (SearchResult resource) -> msg)
-> List QueryParam
-> Cmd msg
search msg queryParams =
search resourceFromJson msg queryParams =
Http.get
{ url =
Url.Builder.absolute [ "data", "search" ] <|
queryParamsToBuilder queryParams
, expect =
Http.expectJson msg <|
fromJson (Decode.list Resource.fromJsonWithEdges)
fromJson (Decode.list resourceFromJson)
}


Expand Down Expand Up @@ -206,14 +218,15 @@ uploadFileAndPostEdge :
{ from : ResourceId
, predicate : Predicate
, file : File
, resourceFromJson : Decoder resource
}
-> Task Http.Error (ResourceWith Edges)
uploadFileAndPostEdge { from, file, predicate } =
-> Task Http.Error resource
uploadFileAndPostEdge { from, file, predicate, resourceFromJson } =
let
get id =
Request.getTask
(Url.Builder.absolute [ "data", "resources", Id.toString id ] [])
Resource.fromJsonWithEdges
resourceFromJson

post fileId =
Task.map2 (\a _ -> a)
Expand Down