driebit/elm-ginger/2.2.1

NOTE: the latest version is 5.0.1

Ginger.Resource

Definitions

type alias ResourceWith a =
{ a
| id : ResourceId
, title : Translation
, body : Translation
, subtitle : Translation
, summary : Translation
, path : String
, category : NonEmpty Category
, properties : Value
, publicationDate : Maybe Posix
, media : Media
, blocks : List Block
}

An Elm representation of a Ginger resource.

Note the a in the record definition, this an extensible record. This means it includes at least all of these fields but may have others as well. This lets us reason about whether the edges are included in the data, compile time. The Ginger REST API includes edges nested only one level deep, but since the edges are also resources we can re-use this datatype like ResourceWith {}. This tells use there are no other fields besides the ones here.

So you'll see this used in function signatures like:

ResourceWith Edges -- has edges


ResourceWith {} -- does not have the edges


ResourceWith a -- might have them but the code that's using this doesn't really care

Note: the ResourceWith {} might actually have edges, they are just not fetched.

type alias Edges =
{ edges : List Edge }

The record we use to extend ResourceWith a.

You can render a list of resource depictions like so:

viewDepictions : ResourceWith Edges -> List (Html msg)
viewDepictions resource =
    List.map viewImage <|
        depictions Media.Medium resource

This next example won't compile because you need a ResourceWith Edges and this signature indicates they are missing.

viewDepictions : ResourceWith {} -> List (Html msg)
viewDepictions resource =
    List.map viewImage <|
        depictions Media.Medium resource
type alias Edge =
{ predicate : Predicate
, resource : ResourceWith {}
}

A connection to a resource named by Predicate

type alias Block =
{ body : Translation
, name : String
, type_ : BlockType
, relatedRscId : Maybe ResourceId
, properties : Value
}
= Text
| Header
| Page
| Custom String

Access data

category : ResourceWith a -> Category

The category of a ResourceWith.

Every resource has one category, but can be part of a hierarchy of other categories. For example news is part of text > article > news. This function will always return the Category stored with the ResourceWith.

Note: there hasn't been the need to expose any of the parent categories so far, if there is please file an issue.

depiction : MediaClass -> ResourceWith Edges -> Maybe String

The image url of the ResourceWith depiction.

Returns the image url if there is a depiction and the mediaclass exists.

depictions : MediaClass -> ResourceWith Edges -> List String

The image urls of the ResourceWith depictions

Returns a list of image urls if there is a depiction and the mediaclass exists.

edgesWithPredicate : Predicate -> ResourceWith Edges -> List (ResourceWith {})

Return all edges with a given predicate.

The returned resources won't have any edges themselves, indicated by the {} in ResourceWith {}.

Decode

fromJsonWithEdges : Decoder (ResourceWith Edges)

Decode a ResourceWith that has edges.

fromJsonWithoutEdges : Decoder (ResourceWith {})

Decode a ResourceWith that does not have edges.