forked from gcanti/elm-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttp.tsx
94 lines (77 loc) · 2.26 KB
/
Http.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import * as E from 'fp-ts/lib/Either'
import * as O from 'fp-ts/lib/Option'
import { flow } from 'fp-ts/lib/function'
import { pipe } from 'fp-ts/lib/pipeable'
import * as t from 'io-ts'
import { failure } from 'io-ts/lib/PathReporter'
import { Lens } from 'monocle-ts'
import * as React from 'react'
import { cmd, http } from '../src'
import { Html } from '../src/React'
// original: https://guide.elm-lang.org/architecture/effects/http.html
export type Result = E.Either<http.HttpError, string>
// --- Flags
export type Flags = Model
export const flags: Flags = {
topic: 'cats',
gifUrl: O.none
}
// --- Model
export type Model = {
topic: string
gifUrl: O.Option<Result>
}
export function init(flags: Flags): [Model, cmd.Cmd<Msg>] {
return [flags, getRandomGif(flags.topic)]
}
// --- Messages
export type Msg = MorePlease | NewGif
export type MorePlease = { type: 'MorePlease' }
export type NewGif = { type: 'NewGif'; result: Result }
function newGif(result: Result): NewGif {
return { type: 'NewGif', result }
}
// --- Get random gif
const ApiPayloadSchema = t.interface({
data: t.interface({
image_url: t.string
})
})
const decoder = flow(
ApiPayloadSchema.decode,
E.mapLeft(errors => failure(errors).join('\n'))
)
function getRandomGif(topic: string): cmd.Cmd<Msg> {
const url = `https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=${topic}`
return pipe(
http.get(url, decoder),
http.send(e => newGif(E.either.map(e, a => a.data.image_url)))
)
}
// --- Update
const gifUrlLens = Lens.fromProp<Model>()('gifUrl')
export function update(msg: Msg, model: Model): [Model, cmd.Cmd<Msg>] {
switch (msg.type) {
case 'MorePlease':
return [gifUrlLens.set(O.none)(model), getRandomGif(model.topic)]
case 'NewGif':
return [gifUrlLens.set(O.some(msg.result))(model), cmd.none]
}
throw new Error('err')
}
// --- View
export function view(model: Model): Html<Msg> {
return dispatch => (
<div>
<h2>{model.topic}</h2>
{pipe(
model.gifUrl,
O.fold(
() => <span>loading...</span>,
E.fold(error => <span>Error: {error._tag}</span>, gifUrl => <img src={gifUrl} />)
)
)}
<button onClick={() => dispatch({ type: 'MorePlease' })}>New Gif</button>
</div>
)
}