This commit is contained in:
orion 2023-10-06 18:40:38 -05:00
parent 430bab5282
commit d1d6a0d739
Signed by: orion
GPG Key ID: 6D4165AE4C928719

View File

@ -65,18 +65,25 @@ export const App = () => {
```haskell ```haskell
type Item = {id :: String, title :: String, description :: String} type Item = {id :: String, title :: String, description :: String}
data State = StateEmpty | StateErrored Error | StateGotItems (Array Item) data State
data Action = Init = StateEmpty
| StateErrored Error
| StateGotItems (Array Item)
renderItem item = div [] [ p [] [text item.title] data Action = ActionInit
, span [className "small"] [text item.description]
] renderItem item =
div [] [ p [] [text item.title]
, span
[className "small"]
[text item.description]
]
renderApp StateEmpty = p [] [text "Loading..."] renderApp StateEmpty = p [] [text "Loading..."]
renderApp (StateErrored e) = p [className "error"] [text (message e)] renderApp (StateErrored e) = p [className "error"] [text (message e)]
renderApp (StateGotItems items) = map renderItem items renderApp (StateGotItems items) = map renderItem items
action Init = do action ActionInit = do
response <- fetch (URL "/api/item") {method: Get} response <- fetch (URL "/api/item") {method: Get}
jsonString <- text response jsonString <- text response
itemsOrError <- readJSON jsonString itemsOrError <- readJSON jsonString
@ -87,10 +94,11 @@ action Init = do
app = mkComponent app = mkComponent
{ initialState: StateEmpty { initialState: StateEmpty
, render: renderApp , render: renderApp
, eval: mkEval ( defaultEval { handleAction = action , eval:
, initialize: Just Init mkEval ( defaultEval { handleAction = action
} , initialize: Just ActionInit
) }
)
} }
``` ```