summaryrefslogtreecommitdiff
path: root/Hero/Node.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Hero/Node.hs')
-rw-r--r--Hero/Node.hs136
1 files changed, 74 insertions, 62 deletions
diff --git a/Hero/Node.hs b/Hero/Node.hs
index 70b8217..38f540d 100644
--- a/Hero/Node.hs
+++ b/Hero/Node.hs
@@ -26,13 +26,13 @@ import qualified Data.Set as Set
import qualified GHC.Show as Legacy
import GHCJS.Types (JSVal)
import Hero.Core
- ( Move (..),
- AudioState (..),
+ ( AudioState (..),
Comic (..),
ComicReaderState (..),
ComicReaderView (..),
- LoginForm (..),
Form (..),
+ LoginForm (..),
+ Move (..),
User (..),
audioId,
chooseExperienceLink,
@@ -57,7 +57,7 @@ import Protolude
-- | Entry point for a miso application
main :: IO ()
-main = miso $ \currentURI -> App {model = initForm currentURI, ..}
+main = miso <| \currentURI -> App {model = initForm currentURI, ..}
where
update = move
view = see
@@ -97,38 +97,42 @@ foreign import javascript unsafe "$1.value"
-- | Updates form, optionally introduces side effects
move :: Move -> Form -> Effect Move Form
move NoOp form = noEff form
-move Dumpform form = form <# do
- jslog $ ms $ Legacy.show form
- pure NoOp
-move (SelectExperience comic) form = form {cpState = ChooseExperience (comicId comic) 1}
- <# do pure $ ChangeURI $ chooseExperienceLink (comicId comic) 1
-move (StartReading comic) form = form {cpState = Reading Spread (comicId comic) 1}
- <# do pure $ ChangeURI $ comicReaderSpreadLink (comicId comic) 1
-move (StartWatching comic) form = form {cpState = Watching (comicId comic)}
- <# do pure $ ChangeURI $ comicVideoLink (comicId comic) 1
+move Dumpform form =
+ form <# do
+ jslog <| ms <| Legacy.show form
+ pure NoOp
+move (SelectExperience comic) form =
+ form {cpState = ChooseExperience (comicId comic) 1}
+ <# do pure <| ChangeURI <| chooseExperienceLink (comicId comic) 1
+move (StartReading comic) form =
+ form {cpState = Reading Spread (comicId comic) 1}
+ <# do pure <| ChangeURI <| comicReaderSpreadLink (comicId comic) 1
+move (StartWatching comic) form =
+ form {cpState = Watching (comicId comic)}
+ <# do pure <| ChangeURI <| comicVideoLink (comicId comic) 1
move NextPage form = case cpState form of
Reading Spread id pg ->
form {cpState = Reading Spread id (pg + 2)} <# do
- pure $ ChangeURI $ comicReaderSpreadLink id (pg + 2)
+ pure <| ChangeURI <| comicReaderSpreadLink id (pg + 2)
Reading Full id pg ->
form {cpState = Reading Full id (pg + 1)} <# do
- pure $ ChangeURI $ comicReaderFullLink id (pg + 1)
+ pure <| ChangeURI <| comicReaderFullLink id (pg + 1)
Cover id ->
form {cpState = Reading Spread id 1} <# do
- pure $ ChangeURI $ comicReaderSpreadLink id 1
+ pure <| ChangeURI <| comicReaderSpreadLink id 1
_ -> noEff form
move PrevPage form = case cpState form of
Reading Spread id pg ->
form {cpState = Reading Spread id (pg -2)} <# do
- pure $ ChangeURI $ comicReaderSpreadLink id (pg -2)
+ pure <| ChangeURI <| comicReaderSpreadLink id (pg -2)
Reading Full id pg ->
form {cpState = Reading Full id (pg -1)} <# do
- pure $ ChangeURI $ comicReaderFullLink id (pg -1)
+ pure <| ChangeURI <| comicReaderFullLink id (pg -1)
Cover _ -> noEff form
_ -> noEff form
move (ToggleZoom c pg) m = m {cpState = newState} <# pure act
where
- goto lnk = ChangeURI $ lnk (comicId c) pg
+ goto lnk = ChangeURI <| lnk (comicId c) pg
reading v = Reading v (comicId c) pg
(newState, act) = case cpState m of
Reading Full _ _ -> (reading Spread, goto comicReaderSpreadLink)
@@ -138,45 +142,50 @@ move (ToggleInLibrary c) form = form {user = newUser} <# pure NoOp
where
newUser = (user form) {userLibrary = newLib}
newLib
- | c `elem` (userLibrary $ user form) =
- Protolude.filter (/= c) $ userLibrary $ user form
- | otherwise = c : (userLibrary $ user form)
+ | c `elem` (userLibrary <| user form) =
+ Protolude.filter (/= c) <| userLibrary <| user form
+ | otherwise = c : (userLibrary <| user form)
move (HandleURI u) form = form {uri = u} <# pure NoOp
-move (ChangeURI u) form = form <# do
- pushURI u
- pure NoOp
+move (ChangeURI u) form =
+ form <# do
+ pushURI u
+ pure NoOp
move FetchComics form = form <# (SetComics <$> fetchComics)
move (SetComics cs) form = noEff form {appComics = cs}
-move (ToggleAudio i) form = form {cpAudioState = newState} <# do
- el <- Document.getElementById i
- toggle el
- pure NoOp
+move (ToggleAudio i) form =
+ form {cpAudioState = newState} <# do
+ el <- Document.getElementById i
+ toggle el
+ pure NoOp
where
(newState, toggle) = case cpAudioState form of
Playing -> (Paused, Audio.pause)
Paused -> (Playing, Audio.play)
-move ToggleFullscreen form = form {cpState = newState} <# do
- el <- Document.querySelector "body"
- -- TODO: check Document.fullscreenEnabled
- -- https://developer.mozilla.org/en-US/docs/Web/API/Document/fullscreenEnabled
- _ <- toggle el
- pure NoOp
+move ToggleFullscreen form =
+ form {cpState = newState} <# do
+ el <- Document.querySelector "body"
+ -- TODO: check Document.fullscreenEnabled
+ -- https://developer.mozilla.org/en-US/docs/Web/API/Document/fullscreenEnabled
+ _ <- toggle el
+ pure NoOp
where
(toggle, newState) = case cpState form of
Reading Full c n -> (const Fullscreen.exit, Reading Full c n)
Reading Spread c n -> (Fullscreen.request, Reading Spread c n)
-- otherwise, do nothing:
x -> (pure, x)
-move (SetMediaInfo x) form = form {dMediaInfo = x}
- <# case x of
- Just Comic {comicId = id} ->
- pure $ ScrollIntoView $ "comic-" <> ms id
- Nothing ->
- pure NoOp
-move (ScrollIntoView id) form = form <# do
- jslog $ ms $ Legacy.show id
- scrollIntoView id
- pure NoOp
+move (SetMediaInfo x) form =
+ form {dMediaInfo = x}
+ <# case x of
+ Just Comic {comicId = id} ->
+ pure <| ScrollIntoView <| "comic-" <> ms id
+ Nothing ->
+ pure NoOp
+move (ScrollIntoView id) form =
+ form <# do
+ jslog <| ms <| Legacy.show id
+ scrollIntoView id
+ pure NoOp
move ValidateUserPassword form =
batchEff
form
@@ -193,15 +202,16 @@ move ValidateUserPassword form =
Network.NotAsked -> pure NoOp
fetchComics :: IO (Network.RemoteData MisoString [Comic])
-fetchComics = Ajax.xhrByteString req /> Ajax.contents >>= \case
- Nothing ->
- pure $ Network.Failure "Could not fetch comics from server."
- Just json ->
- json
- |> Aeson.eitherDecodeStrict
- |> either (Left . ms) pure
- |> Network.fromEither
- |> pure
+fetchComics =
+ Ajax.xhrByteString req /> Ajax.contents >>= \case
+ Nothing ->
+ pure <| Network.Failure "Could not fetch comics from server."
+ Just json ->
+ json
+ |> Aeson.eitherDecodeStrict
+ |> either (Left <. ms) pure
+ |> Network.fromEither
+ |> pure
where
req =
Ajax.Request
@@ -217,16 +227,18 @@ sendLogin ::
Auth.Username ->
Auth.Password ->
IO
- ( Network.RemoteData MisoString
+ ( Network.RemoteData
+ MisoString
User
)
-sendLogin u p = Ajax.xhrByteString req /> Ajax.contents >>= \case
- Nothing ->
- pure $ Network.Failure "Could not send login request."
- Just json ->
- pure $ Network.fromEither
- $ either (Left . ms) pure
- $ Aeson.eitherDecodeStrict json
+sendLogin u p =
+ Ajax.xhrByteString req /> Ajax.contents >>= \case
+ Nothing ->
+ pure <| Network.Failure "Could not send login request."
+ Just json ->
+ pure <| Network.fromEither
+ <| either (Left <. ms) pure
+ <| Aeson.eitherDecodeStrict json
where
req =
Ajax.Request