React.purs 2.05 KB
Newer Older
1 2 3
module Gargantext.Utils.React where

import Prelude
4 5 6 7 8 9
import Data.Array ( (!!) )
import Data.FoldableWithIndex ( foldMapWithIndex )
import Data.Maybe ( fromMaybe )
import React ( ReactElement, Children )
import Reactix as R
import Unsafe.Coerce ( unsafeCoerce )
10 11 12 13 14 15 16

-- TODO: Upgrade thermite and reapply our changes or upstream them and get rid of this
type WithChildren props = { children :: Children | props }

wrap :: (Array ReactElement -> ReactElement) -> ReactElement -> ReactElement
wrap f e = f [e]

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
-- many ::
--   forall props extra state action.
--      Spec state { extra :: extra | WithIndex props } action
--   -> Spec state { props :: (Array props), extra :: extra } action
-- many items spec = Spec { performAction, render }
--   where
--     -- performAction :: PerformAction
--     render d p s c = foldMapWithIndex childSpec p.props
--     childSpec props i = cmapProps (\_ -> props { index = i }) spec

-- many
--   :: forall props state action
--    . (Int -> Spec state props action)
--   -> Spec state (List props) (Tuple Int action)
-- foreach f = Spec  { performAction: performAction
--     , render: render
--     }
--   where
--     performAction :: PerformAction (List state) props (Tuple Int action)
--     performAction (Tuple i a) p sts =
--         for_ (sts !! i) \st ->
--           case f i of
--             Spec s -> forever (transform (_ >>= (_ !! i)))
--                       `transformCoTransformL` s.performAction a p st
--                       `transformCoTransformR` forever (transform (modifying i))
--       where
--         modifying :: Int -> (state -> state) -> List state -> List state
--         modifying j g sts' = fromMaybe sts' (modifyAt j g sts')

--     render :: Render (List state) props (Tuple Int action)
--     render k p sts _ = foldWithIndex (\i st els -> case f i of Spec s -> els <> s.render (k <<< Tuple i) p st []) sts []

--     foldWithIndex :: forall a r. (Int -> a -> r -> r) -> List a -> r -> r
--     foldWithIndex g = go 0
--       where
--       go _ Nil         r = r
--       go i (Cons x xs) r = go (i + 1) xs (g i x r)