Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 21 react examples to v18 #298

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update CatGifsReactHooks
  • Loading branch information
andys8 committed Aug 13, 2022
commit 224069a721b809d0a6aad320b1b21b7245c77bff
1 change: 1 addition & 0 deletions recipes/CatGifsReactHooks/spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
, "react-basic"
, "react-basic-dom"
, "react-basic-hooks"
, "web-dom"
, "web-html"
]
, packages = ../../packages.dhall
Expand Down
44 changes: 28 additions & 16 deletions recipes/CatGifsReactHooks/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import Data.Maybe (Maybe(..), maybe)
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Exception (throw)
import React.Basic.DOM (css, render)
import React.Basic.DOM (css)
import React.Basic.DOM as R
import React.Basic.DOM.Client (createRoot, renderRoot)
import React.Basic.Events (handler_)
import React.Basic.Hooks (Component, component, (/\))
import React.Basic.Hooks as React
import React.Basic.Hooks.Aff (useAff)
import React.Basic.Hooks.ResetToken (useResetToken)
import Web.DOM.NonElementParentNode (getElementById)
import Web.HTML (window)
import Web.HTML.HTMLDocument (body)
import Web.HTML.HTMLElement (toElement)
import Web.HTML.HTMLDocument (toNonElementParentNode)
import Web.HTML.Window (document)

data GifState
Expand All @@ -30,33 +31,38 @@ data GifState

main :: Effect Unit
main = do
body <- body =<< document =<< window
case body of
Nothing -> throw "Could not find body."
Just b -> do
catGifs <- mkCatGifs
render (catGifs {}) (toElement b)
doc <- document =<< window
root <- getElementById "root" $ toNonElementParentNode doc
case root of
Nothing -> throw "Could not find root."
Just container -> do
reactRoot <- createRoot container
app <- mkApp
renderRoot reactRoot (app {})

mkCatGifs :: Component {}
mkCatGifs = do
mkApp :: Component {}
mkApp = do
component "CatGifs" \_ -> React.do
(resetToken /\ reset) <- useResetToken
gifState <- toGifState <$> useAff resetToken getRandomCatGif
let
onClick = handler_ reset
let onClick = handler_ reset
pure
$ R.div_
[ R.h2_ [ R.text "Random Cats" ]
, case gifState of
Loading -> R.text "Loading..."
Failure ->
R.div_
[ R.text "I could not load a random cat for some reason. "
[ R.text "I could not load a random cat for some reason."
, R.button { onClick, children: [ R.text "Try Again!" ] }
]
Success url ->
R.div_
[ R.button { onClick, style: css { display: "block" }, children: [ R.text "More Please!" ] }
[ R.button
{ onClick
, style: css { display: "block" }
, children: [ R.text "More Please!" ]
}
, R.img { src: url }
]
]
Expand All @@ -74,4 +80,10 @@ getRandomCatGif = do
pure do
-- Using `hush` to ignore the possible error messages
{ body } <- hush response
hush (decodeJson body >>= (_ .: "data") >>= (_ .: "images") >>= (_ .: "downsized") >>= (_ .: "url"))
hush
( decodeJson body
>>= (_ .: "data")
>>= (_ .: "images")
>>= (_ .: "downsized")
>>= (_ .: "url")
)