Skip to content

Commit 0dbd2c9

Browse files
authored
Replace giphy API with dog API in CatGifReactHooks and CatGifHalogenHooks recipes (#304)
* Use dog.ceo API in stead of giphy.com * Update filenames, README * Replace giphy API with dog.ceo in Halogen recipe * Remove links to Elm example in DogImages recipes --------- Co-authored-by: Peter Murphy <[email protected]>
1 parent 7992e8e commit 0dbd2c9

File tree

18 files changed

+105
-114
lines changed

18 files changed

+105
-114
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ Running a web-compatible recipe:
9898
| :heavy_check_mark: | | [CapabilityPatternNode](recipes/CapabilityPatternNode) | A skeletal version of an application structuring pattern |
9999
| | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/CardsHalogenHooks/src/Main.purs)) | [CardsHalogenHooks](recipes/CardsHalogenHooks) | A Halogen port of the ["Random - Cards" Elm Example](https://elm-lang.org/examples/cards). |
100100
| | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/CardsReactHooks/src/Main.purs)) | [CardsReactHooks](recipes/CardsReactHooks) | A React port of the ["Random - Cards" Elm Example](https://elm-lang.org/examples/cards). |
101-
| | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/CatGifsHalogenHooks/src/Main.purs)) | [CatGifsHalogenHooks](recipes/CatGifsHalogenHooks) | A Halogen port of the ["HTTP - Cat GIFs" Elm Example](https://elm-lang.org/examples/cat-gifs). |
102-
| | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/CatGifsReactHooks/src/Main.purs)) | [CatGifsReactHooks](recipes/CatGifsReactHooks) | A React port of the ["HTTP - Cat GIFs" Elm Example](https://elm-lang.org/examples/cat-gifs). |
103101
| | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/ClockReactHooks/src/Main.purs)) | [ClockReactHooks](recipes/ClockReactHooks) | A React port of the ["User Interface - Clock" Elm Example](https://elm-lang.org/examples/clock). |
104102
| | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/ComponentsHalogenHooks/src/Main.purs)) | [ComponentsHalogenHooks](recipes/ComponentsHalogenHooks) | Demonstrates how to nest one Halogen-Hooks-based component inside another and send/receive queries between the two. |
105103
| | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/ComponentsInputHalogenHooks/src/Main.purs)) | [ComponentsInputHalogenHooks](recipes/ComponentsInputHalogenHooks) | Each time a parent re-renders, it will pass a new input value into the child, and the child will update accordingly. |
@@ -111,6 +109,8 @@ Running a web-compatible recipe:
111109
| :heavy_check_mark: | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/DebuggingLog/src/Main.purs)) | [DebuggingLog](recipes/DebuggingLog) | This recipe shows how to do print-debugging using the `Debug` module's `spy` and `traceM` functions. The compiler will emit warnings to remind you to remove these debug functions before you ship production code. |
112110
| :heavy_check_mark: | | [DiceCLI](recipes/DiceCLI) | This recipe shows how to create an interactive command line prompt that repeatedly generates a random number between 1 and 6. |
113111
| :heavy_check_mark: | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/DiceLog/src/Main.purs)) | [DiceLog](recipes/DiceLog) | This recipe shows how to log a random integer between 1 and 6 (representing a roll of a die) in either the node.js or web browser console. |
112+
| | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/DogImagesHalogenHooks/src/Main.purs)) | [DogImagesHalogenHooks](recipes/DogImagesHalogenHooks) | Fetches random dog images from the [Dog API](https://dog.ceo/dog-api/). |
113+
| | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/DogImagesReactHooks/src/Main.purs)) | [DogImagesReactHooks](recipes/DogImagesReactHooks) | Fetches random dog images from the [Dog API](https://dog.ceo/dog-api/). |
114114
| | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/DragAndDropHalogenHooks/src/Main.purs)) | [DragAndDropHalogenHooks](recipes/DragAndDropHalogenHooks) | A Halogen port of the ["Files - Drag-and-Drop" Elm Example](https://elm-lang.org/examples/drag-and-drop). |
115115
| | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/DragAndDropReactHooks/src/Main.purs)) | [DragAndDropReactHooks](recipes/DragAndDropReactHooks) | A React port of the ["Files - Drag-and-Drop" Elm Example](https://elm-lang.org/examples/drag-and-drop). |
116116
| | :heavy_check_mark: ([try](https://try.purescript.org/?github=/JordanMartinez/purescript-cookbook/master/recipes/FileUploadHalogenHooks/src/Main.purs)) | [FileUploadHalogenHooks](recipes/FileUploadHalogenHooks) | A Halogen port of the ["Files - Upload" Elm Example](https://elm-lang.org/examples/upload). |

recipes/CatGifsHalogenHooks/README.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

recipes/CatGifsHalogenHooks/web/index.html

Lines changed: 0 additions & 13 deletions
This file was deleted.

recipes/CatGifsHalogenHooks/web/index.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

recipes/CatGifsReactHooks/README.md

Lines changed: 0 additions & 16 deletions
This file was deleted.

recipes/CatGifsReactHooks/web/index.js

Lines changed: 0 additions & 4 deletions
This file was deleted.
File renamed without changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# CatGifsHalogenHooks
2+
3+
Fetches random dog images from the [Dog API](https://dog.ceo/dog-api/).
4+
5+
## Expected Behavior:
6+
7+
### Browser
8+
9+
The browser will display "Loading..." and then display a dog image with a button that will display the next random dog image if the HTTP request succeeds or an error message along with the same button if it fails.

recipes/CatGifsHalogenHooks/spago.dhall renamed to recipes/DogImagesHalogenHooks/spago.dhall

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ name = "CatGifsHalogenHooks"
1+
{ name = "DogImagesHalogenHooks"
22
, dependencies =
33
[ "aff"
44
, "affjax"
@@ -19,5 +19,5 @@
1919
, "tuples"
2020
]
2121
, packages = ../../packages.dhall
22-
, sources = [ "recipes/CatGifsHalogenHooks/src/**/*.purs" ]
22+
, sources = [ "recipes/DogImagesHalogenHooks/src/**/*.purs" ]
2323
}

recipes/CatGifsHalogenHooks/src/Main.purs renamed to recipes/DogImagesHalogenHooks/src/Main.purs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
module CatGifsHalogenHooks.Main where
1+
module DogImagesHalogenHooks.Main where
22

33
import Prelude
44

55
import Affjax.ResponseFormat as AXRF
66
import Affjax.StatusCode (StatusCode(..))
77
import Affjax.Web as AX
8-
import CSS (block, display)
8+
import CSS (block, display, marginBottom, maxWidth, px, rem)
99
import Data.Argonaut.Core (Json)
1010
import Data.Codec (decode)
1111
import Data.Codec.Argonaut (JsonDecodeError)
@@ -41,64 +41,66 @@ hookComponent = Hooks.component \_ _ -> Hooks.do
4141
content /\ contentIdx <- Hooks.useState RD.NotAsked
4242

4343
let
44-
getNextGif = do
44+
getNextImage = do
4545
Hooks.put contentIdx RD.Loading
4646
errorOrResponse <- liftAff $ AX.request $ AX.defaultRequest
47-
{ url = "https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=cat"
47+
{ url = "https://dog.ceo/api/breeds/image/random"
4848
, method = Left GET
4949
, responseFormat = AXRF.json
5050
}
5151
let
5252
httpResult = case errorOrResponse of
5353
Right jsonRec | jsonRec.status == StatusCode 200 ->
5454
case decodeJson jsonRec.body of
55-
Right rec -> RD.Success rec.data.images.downsized.url
55+
Right rec -> RD.Success rec.message
5656
_ -> RD.Failure "decode error"
5757
_ ->
5858
RD.Failure "http error"
5959

6060
Hooks.put contentIdx httpResult
6161

6262
Hooks.useLifecycleEffect do
63-
getNextGif
63+
getNextImage
6464
pure Nothing
6565

6666
Hooks.pure $
6767
HH.div_
68-
[ HH.h2_ [ HH.text "Random Cats" ]
68+
[ HH.h2_ [ HH.text "Random Dogs" ]
6969
, case content of
7070
RD.NotAsked ->
7171
HH.text "Loading page..."
7272
RD.Loading ->
7373
HH.text "Loading..."
74-
RD.Failure _ ->
74+
RD.Failure reason ->
7575
HH.div_
76-
[ HH.text "I could not load a random cat for some reason. "
76+
[ HH.text "I could not load a random dog for some reason. "
77+
, HH.pre_ [ HH.text reason ]
7778
, HH.button
78-
[ HE.onClick \_ -> getNextGif
79-
]
79+
[ HE.onClick \_ -> getNextImage ]
8080
[ HH.text "Try Again!" ]
8181
]
8282

8383
RD.Success url ->
8484
HH.div_
8585
[ HH.button
86-
[ HE.onClick \_ -> getNextGif
87-
, HC.style $ display block
86+
[ HE.onClick \_ -> getNextImage
87+
, HC.style do
88+
display block
89+
marginBottom (1.0 # rem)
8890
]
8991
[ HH.text "More Please!" ]
90-
, HH.img [ HP.src url ]
92+
, HH.img
93+
[ HP.src url
94+
, HC.style do
95+
maxWidth (400.0 # px)
96+
]
9197
]
9298
]
9399

94-
decodeJson :: Json -> Either JsonDecodeError { data :: { images :: { downsized :: { url :: String } } } }
100+
decodeJson :: Json -> Either JsonDecodeError { message :: String, status :: String }
95101
decodeJson = decode
96-
$ CA.object "data"
102+
$ CA.object "response"
97103
$ CAR.record
98-
{ data: CA.object "images" $ CAR.record
99-
{ images: CA.object "downsized" $ CAR.record
100-
{ downsized: CA.object "url" $ CAR.record
101-
{ url: CA.string }
102-
}
103-
}
104+
{ message: CA.string
105+
, status: CA.string
104106
}

0 commit comments

Comments
 (0)