Last active
July 10, 2020 23:35
-
-
Save bchase/cb5713b670ed072fb3404034f3e936fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Types.Scratch exposing (HasComments) | |
import RemoteData as RD exposing (RemoteData(..), WebData) | |
import Types.Clip.Comment as Comment exposing (Comment) | |
type alias HasComments m = | |
{ m | |
| commentsCount : Int | |
, comments : WebData (List Comment) | |
} | |
addComment : Comment -> HasComments clip -> HasComments clip | |
addComment comment ({comments} as clip) = | |
let | |
comments_ = | |
comments |> RD.map (\cs -> comment::cs |> List.sortWith Comment.comparison) | |
in | |
{ clip | comments = comments_ } | |
|> updateCommentCount ((+) 1) | |
removeComment : Comment -> HasComments clip -> HasComments clip | |
removeComment comment ({comments} as clip) = | |
let | |
comments_ = | |
comments |> RD.map (List.filter (\c -> c.id /= comment.id)) | |
in | |
{ clip | comments = comments_ } | |
|> updateCommentCount (\n -> n - 1) | |
addComment_ : Comment -> HasComments clip -> HasComments clip | |
addComment_ comment clip = | |
clip | |
|> changeComments (\cs -> comment::cs |> List.sortWith Comment.comparison) | |
|> updateCommentCount ((+) 1) | |
removeComment_ : Comment -> HasComments clip -> HasComments clip | |
removeComment_ comment clip = | |
clip | |
|> changeComments (List.filter (\c -> c.id /= comment.id)) | |
|> updateCommentCount ((-) 1) | |
changeComments : (List Comment -> List Comment) -> HasComments clip -> HasComments clip | |
changeComments f ({comments} as clip) = | |
{ clip | comments = comments |> RD.map f } | |
updateCommentCount : (Int -> Int) -> HasComments clip -> HasComments clip | |
updateCommentCount f ({comments, commentsCount} as clip) = | |
let | |
count = | |
case comments of | |
Success cs -> List.length cs | |
_ -> f commentsCount | |
in | |
{ clip | commentsCount = count } | |
type RemoteHas meta err a | |
= NotReady meta | |
| Error err meta | |
| Ready a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment