Skip to content

Instantly share code, notes, and snippets.

@mrmurphy
Created November 17, 2016 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmurphy/202e2ff5e24457c23dc05b61b0cab1ee to your computer and use it in GitHub Desktop.
Save mrmurphy/202e2ff5e24457c23dc05b61b0cab1ee to your computer and use it in GitHub Desktop.
module Model.Lenses exposing (..)
-- other imports omitted
import Monocle.Optional as MO
import RemoteData
entryInList : String -> MO.Optional (List Entry) Entry
entryInList id =
{ getOption = List.Extra.find (\entry -> entry.id == id)
, set =
(\newEntry entries ->
List.map
(\entry ->
if entry.id == id then
newEntry
else
entry
)
entries
)
}
entriesInModel : MO.Optional Model (List Entry)
entriesInModel =
{ getOption = .entries >> RemoteData.toMaybe
, set =
(\newEntries model ->
{ model | entries = RemoteData.Success newEntries }
)
}
entryInModel : String -> MO.Optional Model Entry
entryInModel id =
MO.compose entriesInModel (entryInList id)
-- Stuff up here
type alias Model =
{ route : Router.Route
, showSidebar : Bool
, journals : WebData (List Journal)
, entries : WebData (List Entry)
, editor : Savable String
, notification : Notification.Model
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment