module Hypermedia.Datastar.PatchElements where
import Data.Text (Text)
import Data.Text qualified as T
import Hypermedia.Datastar.Types
data PatchElements = PatchElements
{ PatchElements -> Maybe Text
peElements :: Maybe Text
, PatchElements -> Maybe Text
peSelector :: Maybe Text
, PatchElements -> ElementPatchMode
peMode :: ElementPatchMode
, PatchElements -> Bool
peUseViewTransition :: Bool
, PatchElements -> ElementNamespace
peNamespace :: ElementNamespace
, PatchElements -> Maybe Text
peEventId :: Maybe Text
, PatchElements -> Int
peRetryDuration :: Int
}
deriving (PatchElements -> PatchElements -> Bool
(PatchElements -> PatchElements -> Bool)
-> (PatchElements -> PatchElements -> Bool) -> Eq PatchElements
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PatchElements -> PatchElements -> Bool
== :: PatchElements -> PatchElements -> Bool
$c/= :: PatchElements -> PatchElements -> Bool
/= :: PatchElements -> PatchElements -> Bool
Eq, Int -> PatchElements -> ShowS
[PatchElements] -> ShowS
PatchElements -> String
(Int -> PatchElements -> ShowS)
-> (PatchElements -> String)
-> ([PatchElements] -> ShowS)
-> Show PatchElements
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PatchElements -> ShowS
showsPrec :: Int -> PatchElements -> ShowS
$cshow :: PatchElements -> String
show :: PatchElements -> String
$cshowList :: [PatchElements] -> ShowS
showList :: [PatchElements] -> ShowS
Show)
patchElements :: Text -> PatchElements
patchElements :: Text -> PatchElements
patchElements Text
html =
PatchElements
{ peElements :: Maybe Text
peElements = if Text -> Bool
T.null Text
html then Maybe Text
forall a. Maybe a
Nothing else Text -> Maybe Text
forall a. a -> Maybe a
Just Text
html
, peSelector :: Maybe Text
peSelector = Maybe Text
forall a. Maybe a
Nothing
, peMode :: ElementPatchMode
peMode = ElementPatchMode
defaultPatchMode
, peUseViewTransition :: Bool
peUseViewTransition = Bool
defaultUseViewTransition
, peNamespace :: ElementNamespace
peNamespace = ElementNamespace
defaultNamespace
, peEventId :: Maybe Text
peEventId = Maybe Text
forall a. Maybe a
Nothing
, peRetryDuration :: Int
peRetryDuration = Int
defaultRetryDuration
}
removeElements :: Text -> PatchElements
removeElements :: Text -> PatchElements
removeElements Text
sel =
PatchElements
{ peElements :: Maybe Text
peElements = Maybe Text
forall a. Maybe a
Nothing
, peSelector :: Maybe Text
peSelector = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
sel
, peMode :: ElementPatchMode
peMode = ElementPatchMode
Remove
, peUseViewTransition :: Bool
peUseViewTransition = Bool
defaultUseViewTransition
, peNamespace :: ElementNamespace
peNamespace = ElementNamespace
defaultNamespace
, peEventId :: Maybe Text
peEventId = Maybe Text
forall a. Maybe a
Nothing
, peRetryDuration :: Int
peRetryDuration = Int
defaultRetryDuration
}
toDatastarEvent :: PatchElements -> DatastarEvent
toDatastarEvent :: PatchElements -> DatastarEvent
toDatastarEvent PatchElements
pe =
DatastarEvent
{ eventType :: EventType
eventType = EventType
EventPatchElements
, eventId :: Maybe Text
eventId = PatchElements -> Maybe Text
peEventId PatchElements
pe
, retry :: Int
retry = PatchElements -> Int
peRetryDuration PatchElements
pe
, dataLines :: [Text]
dataLines =
[[Text]] -> [Text]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [Text] -> (Text -> [Text]) -> Maybe Text -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\Text
s -> [Text
"selector " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
s]) (PatchElements -> Maybe Text
peSelector PatchElements
pe)
, [ Text
"mode " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ElementPatchMode -> Text
patchModeToText (PatchElements -> ElementPatchMode
peMode PatchElements
pe)
| PatchElements -> ElementPatchMode
peMode PatchElements
pe ElementPatchMode -> ElementPatchMode -> Bool
forall a. Eq a => a -> a -> Bool
/= ElementPatchMode
defaultPatchMode
]
, [ Text
"useViewTransition true"
| PatchElements -> Bool
peUseViewTransition PatchElements
pe
]
, [ Text
"namespace " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ElementNamespace -> Text
namespaceToText (PatchElements -> ElementNamespace
peNamespace PatchElements
pe)
| PatchElements -> ElementNamespace
peNamespace PatchElements
pe ElementNamespace -> ElementNamespace -> Bool
forall a. Eq a => a -> a -> Bool
/= ElementNamespace
defaultNamespace
]
, [Text] -> (Text -> [Text]) -> Maybe Text -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] ((Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text
"elements " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) ([Text] -> [Text]) -> (Text -> [Text]) -> Text -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
T.lines) (PatchElements -> Maybe Text
peElements PatchElements
pe)
]
}