/
Context.purs
67 lines (58 loc) · 1.61 KB
/
Context.purs
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
module ReactHocs.Context
( withContext
, getFromContext
, getContext
, accessContext
, readContext
, CONTEXT
) where
import Prelude (id, flip, const, ($))
import Control.Monad.Eff (kind Effect, Eff)
import Data.Lens (Lens', view, lens)
import React (ReactClass, ReactThis)
import Type.Proxy (Proxy)
import ReactHocs.Class (class WithContextProps, setCtx)
foreign import data CONTEXT :: Effect
foreign import withContext
:: forall props ctx
. ReactClass props
-> ctx
-> ReactClass props
foreign import getFromContext_
:: forall props props' ctx ctx'
. (ctx' -> props' -> props)
-> (ctx -> ctx')
-> ReactClass props
-> ReactClass props'
getFromContext
:: forall props props' ctx ctx'
. (WithContextProps props' props ctx')
=> Lens' ctx ctx'
-> ReactClass props
-> ReactClass props'
getFromContext _lens cls = getFromContext_ setCtx_ (view _lens) cls
where
setCtx_ :: ctx' -> props' -> props
setCtx_ = setCtx
getContext
:: forall props props' ctx
. (WithContextProps props' props ctx)
-- proxy is needed to resolve WithContextProps
=> Proxy ctx
-> ReactClass props
-> ReactClass props'
getContext p = getFromContext _id
where
_id :: Lens' ctx ctx
_id = lens id (flip $ const id)
-- | This function mutates the component by adding `contextTypes` property.
foreign import accessContext
:: forall props
. ReactClass props
-> ReactClass props
-- | You can use it with components that were passed through `accessContext`.
foreign import readContext
:: forall props state eff ctx
. Proxy ctx
-> ReactThis props state
-> Eff (context :: CONTEXT | eff) ctx