Skip to content

Commit

Permalink
refactor: type Story and createEventDecoder. Add createEventJournalTy…
Browse files Browse the repository at this point in the history
…peItemDecoder and nonEmptyStoryDecoder.
  • Loading branch information
RalfBarkow committed Jun 8, 2023
1 parent 1bb582b commit 4251bf7
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions src/Wiki.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@ type alias Page =
}


type alias Item =
{ title : String
, story : Story
}



-- The "story" is a collection of paragraphs and paragraph like items.


type Story
= NonEmptyStory (List Item)
= NonEmptyStory NonEmptyStoryAlias
| EmptyStory {}
| Future { id : String, type_ : String, text : String, title : String }
| Paragraph { type_ : String, id : String, text : String }
| UnknownStory Decode.Value


type alias Item =
{ type_ : String, id : String, text : String }
type alias NonEmptyStoryAlias =
{ type_ : String
, id : String
, text : String
}



Expand All @@ -34,28 +43,13 @@ type Journal
= EmptyJournal
| NonEmptyJournal
| Create CreateEvent
| Add AddEvent
| Edit EditEvent
| Move MoveEvent
| UnknownJournal Decode.Value


type alias AddEvent =
{ id : String, title : String }


type alias CreateEvent =
{ type_ : String, item : { title : String, story : List Item }, date : Int }


type alias EditEvent =
{ id : String, text : String }


type alias MoveEvent =
{ id : String, destination : String }


pageDecoder : Decode.Decoder Page
pageDecoder =
Decode.map3 Page
Expand All @@ -68,7 +62,7 @@ storyDecoder : Decode.Decoder Story
storyDecoder =
Decode.oneOf
[ Decode.map NonEmptyStory nonEmptyStoryDecoder
, Decode.map EmptyStory {} -- Decoder for EmptyStory
, Decode.succeed EmptyStory
]


Expand All @@ -86,9 +80,28 @@ createEventDecoder : Decode.Decoder CreateEvent
createEventDecoder =
Decode.map3 CreateEvent
(Decode.field "type" Decode.string)
(Decode.field "item" (Decode.map2 ItemDecoder (Decode.field "title" Decode.string) storyDecoder))
-- journal type create item decoder
(Decode.field "item" createEventJournalTypeItemDecoder)
(Decode.field "date" Decode.int)


type alias ItemDecoder =
{ title : String, story : {} }
createEventJournalTypeItemDecoder : Decode.Decoder { title : String, story : List Item }
createEventJournalTypeItemDecoder =
Decode.map2 (\title _ -> { title = title, story = [] })
(Decode.field "title" Decode.string)
(Decode.field "story" (Decode.list itemDecoder))


itemDecoder : Decode.Decoder Item
itemDecoder =
Decode.map2 Item
(Decode.field "title" Decode.string)
storyDecoder


nonEmptyStoryDecoder : Decode.Decoder NonEmptyStoryAlias
nonEmptyStoryDecoder =
Decode.map3 NonEmptyStoryAlias
(Decode.field "type" Decode.string)
(Decode.field "id" Decode.string)
(Decode.field "text" Decode.string)

0 comments on commit 4251bf7

Please sign in to comment.