Task.purs 1.13 KB
Newer Older
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
module Gargantext.Components.Forest.Tree.Node.Tools.Task
  where

import Data.Array as A
import Data.Map as Map
import Data.Maybe (Maybe(..), maybe)
import Data.Tuple.Nested ((/\))
import Effect (Effect)
import Gargantext.AsyncTasks as GAT
import Gargantext.Prelude (Unit, discard, identity, ($), (+))
import Gargantext.Types (Reload)
import Gargantext.Types as GT
import Reactix as R


type Tasks =
  ( onTaskAdd    :: GT.AsyncTaskWithType -> Effect Unit
  , onTaskFinish :: GT.AsyncTaskWithType -> Effect Unit
  , tasks        :: Array GT.AsyncTaskWithType
  )

tasksStruct :: Int
            -> R.State GAT.Storage
            -> R.State Reload
            -> Record Tasks
tasksStruct id (asyncTasks /\ setAsyncTasks) (_ /\ setReload) =
  { onTaskAdd, onTaskFinish, tasks }
    where
      tasks = maybe [] identity $ Map.lookup id asyncTasks

      onTaskAdd t = do
        setReload (_ + 1)
        setAsyncTasks $ Map.alter (maybe (Just [t])
                      $ (\ts -> Just $ A.cons t ts)) id

      onTaskFinish t = do
        setReload (_ + 1)
        setAsyncTasks $ Map.alter (maybe Nothing $ (\ts -> Just $ GAT.removeTaskFromList ts t)) id