ebook-manager/frontend/src/Main.hs

94 lines
3.2 KiB
Haskell
Raw Normal View History

2019-01-22 22:40:44 +02:00
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
2018-11-12 22:58:50 +02:00
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
2019-01-22 22:40:44 +02:00
{-# LANGUAGE ScopedTypeVariables #-}
2018-11-12 22:58:50 +02:00
{-# LANGUAGE RecordWildCards #-}
2019-01-22 22:40:44 +02:00
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE GADTs #-}
2018-11-12 22:58:50 +02:00
module Main where
2019-01-22 22:40:44 +02:00
import Control.Lens (over, set)
2018-11-12 22:58:50 +02:00
import Control.Monad.Trans (liftIO)
2019-01-22 22:40:44 +02:00
import Data.Generics.Product
import GHC.Generics (Generic)
2018-11-12 22:58:50 +02:00
import Language.Javascript.JSaddle.Warp
2019-01-22 22:40:44 +02:00
import Miso hiding (set)
2018-11-12 22:58:50 +02:00
import Miso.String
2019-01-22 22:40:44 +02:00
import Servant.API
import Servant.Links
import Data.Proxy (Proxy(..))
2019-01-22 23:35:26 +02:00
type API = Home :<|> Login :<|> Register
2019-01-22 22:40:44 +02:00
type Home = View Action
type Login = "login" :> View Action
2019-01-22 23:35:26 +02:00
type Register = "register" :> View Action
2018-11-12 22:58:50 +02:00
data Action = Add
| Subtract
| SayHello
2019-01-22 22:40:44 +02:00
| HandleURI URI
| ChangeURI URI
2018-11-12 22:58:50 +02:00
| NoOp
2019-01-22 22:40:44 +02:00
data Model = Model { counter :: Int
, uri :: URI }
deriving (Eq, Generic)
2018-11-12 22:58:50 +02:00
2019-01-22 22:40:44 +02:00
updateModel :: Model -> Action -> Effect Action Model
updateModel m = \case
Add -> noEff (over (field @"counter") (+1) m)
Subtract -> noEff (over (field @"counter") (\x -> x - 1) m)
SayHello -> m <# (liftIO (putStrLn "Hello world") >> pure NoOp)
HandleURI uri -> noEff (set (field @"uri") uri m)
2019-01-22 23:35:26 +02:00
ChangeURI uri -> m <# do
liftIO $ putStrLn $ "Pushing uri " <> show uri
pushURI uri
return $ HandleURI uri
2019-01-22 22:40:44 +02:00
NoOp -> noEff m
2018-11-12 22:58:50 +02:00
viewModel :: Model -> View Action
2019-01-22 22:40:44 +02:00
viewModel model = view
where
view = either (const the404) id $ runRoute @API Proxy handlers uri model
2019-01-22 23:35:26 +02:00
handlers = home :<|> login :<|> register
2019-01-22 22:40:44 +02:00
home _ = div_ [] [ button_ [ onClick Add ] [ text "+" ]
, text (ms (counter model))
, button_ [ onClick Subtract ] [ text "-" ]
, button_ [ onClick goLogin ] [ text "go login" ]
2019-01-22 23:35:26 +02:00
, button_ [ onClick goRegister ] [ text "go register" ]
2019-01-22 22:40:44 +02:00
]
login _ = div_ [] []
2019-01-22 23:35:26 +02:00
register _ = div_ [] [
h3_ [] [text "register"]
, label_ [] [text "Username"], input_ [id_ "username", name_ "username"]
, label_ [] [text "Email"], input_ [id_ "email", name_ "email"]
, label_ [] [text "Password"], input_ [id_ "password", name_ "password"]
, label_ [] [text "Password again"], input_ [id_ "passwordAgain", name_ "passwordAgain"]
, button_ [] [text "Register"]
]
2019-01-22 22:40:44 +02:00
the404 = div_ [] []
2019-01-22 23:35:26 +02:00
goLogin, goHome, goRegister :: Action
2019-01-22 22:40:44 +02:00
goLogin = goto @Login @API Proxy Proxy
goHome = goto @Home @API Proxy Proxy
2019-01-22 23:35:26 +02:00
goRegister = goto @Register @API Proxy Proxy
2019-01-22 22:40:44 +02:00
goto :: (IsElem endpoint api, HasLink endpoint, MkLink endpoint Link ~ Link) => Proxy api -> Proxy endpoint -> Action
goto a b = ChangeURI (linkURI (safeLink a b))
2018-11-12 22:58:50 +02:00
main :: IO ()
2019-01-22 22:40:44 +02:00
main = run 8081 $ do
model <- mkModel
startApp App{..}
2018-11-12 22:58:50 +02:00
where
2019-01-22 22:40:44 +02:00
mkModel = Model <$> pure 0 <*> getCurrentURI
2018-11-12 22:58:50 +02:00
initialAction = SayHello
2019-01-22 22:40:44 +02:00
update = flip updateModel
2018-11-12 22:58:50 +02:00
view = viewModel
2019-01-22 22:40:44 +02:00
subs = [ uriSub HandleURI ]
2018-11-12 22:58:50 +02:00
events = defaultEvents
mountPoint = Nothing